+ Reply to Thread
Results 1 to 5 of 5

Transfer data from one sheet to another based on cell value not being empty

Hybrid View

  1. #1
    Registered User
    Join Date
    02-10-2016
    Location
    Anaheim, CA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Question Transfer data from one sheet to another based on cell value not being empty

    Hi there,

    I wrote this vba code that should transfer the data from one worksheet to another. I need it to transfer the adjacent data if the value on a cell is not empty (from the "Form" worksheet) on the last row of the "Email" worksheet. However, I don't know why VBA is only honoring the last If statement and completely ignoring (or maybe over writing the first to If statements. SO I basically need to have the "Email" worksheet to have a total of 3 entries but instead, I'm only getting one (the last if statement). Any help would be appreciated.

    Here's my code:

    Private Sub cmdEmail_Click()
    
    Dim lRow As Long
    Dim sht As Worksheet
    Set sht = Sheets("Email")
    lRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
    
    
    If Not IsEmpty(Sheets("Form").Range("K23").Value) Then
        Range("A" & lRow + 1).Value = Sheets("Form").Range("K23").Value
        Range("B" & lRow + 1).Value = Sheets("Form").Range("E23").Value
        Range("C" & lRow + 1).Value = Sheets("Form").Range("F23").Value
        Range("D" & lRow + 1).Value = Sheets("Form").Range("H23").Value
    End If
    
    If Not IsEmpty(Sheets("Form").Range("K27").Value) Then
        Range("A" & lRow + 1).Value = Sheets("Form").Range("K27").Value
        Range("B" & lRow + 1).Value = Sheets("Form").Range("E27").Value
        Range("C" & lRow + 1).Value = Sheets("Form").Range("F27").Value
        Range("D" & lRow + 1).Value = Sheets("Form").Range("H27").Value
    End If
    
    If Not IsEmpty(Sheets("Form").Range("K37").Value) Then
        Range("A" & lRow + 1).Value = Sheets("Form").Range("K37").Value
        Range("B" & lRow + 1).Value = Sheets("Form").Range("E37").Value
        Range("C" & lRow + 1).Value = Sheets("Form").Range("F37").Value
        Range("D" & lRow + 1).Value = Sheets("Form").Range("H37").Value
    End If
    
    
    
    End Sub

  2. #2
    Forum Contributor
    Join Date
    01-04-2013
    Location
    Tampa, Florida
    MS-Off Ver
    Excel 2013
    Posts
    142

    Re: Transfer data from one sheet to another based on cell value not being empty

    It looks like you're writing to the same cells in each of the 3 IF statements, so your last IF statement is overwriting the first two. Not sure if this will give you exactly what you're looking for, but try changing the +1 in the second IF statement to +2 and to +3 for the third if statement.

  3. #3
    Registered User
    Join Date
    02-10-2016
    Location
    Anaheim, CA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Transfer data from one sheet to another based on cell value not being empty

    Quote Originally Posted by mo4391 View Post
    It looks like you're writing to the same cells in each of the 3 IF statements, so your last IF statement is overwriting the first two. Not sure if this will give you exactly what you're looking for, but try changing the +1 in the second IF statement to +2 and to +3 for the third if statement.
    Hi mo4391,

    Thank you for your reply. That will actually work out if the mentioned cells are always not empty. However, there are instance that one or more will be empty. Your idea of changing the +1 will insert empty rows on the "Email" spreadsheet. I need it to be inserted on the last row. Any other idea?

    Thank you.

  4. #4
    Forum Contributor
    Join Date
    01-04-2013
    Location
    Tampa, Florida
    MS-Off Ver
    Excel 2013
    Posts
    142

    Re: Transfer data from one sheet to another based on cell value not being empty

    Then it sounds like you need to re-define your lRow variable if either of the first two IF statements are true. There are probably other people smarter than me on this message board that would have a more elegant solution, but see if this works for you:

    Private Sub cmdEmail_Click()
    
    Dim lRow As Long
    Dim sht As Worksheet
    Set sht = Sheets("Email")
    lRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
    
    
    If Not IsEmpty(Sheets("Form").Range("K23").Value) Then
        Range("A" & lRow + 1).Value = Sheets("Form").Range("K23").Value
        Range("B" & lRow + 1).Value = Sheets("Form").Range("E23").Value
        Range("C" & lRow + 1).Value = Sheets("Form").Range("F23").Value
        Range("D" & lRow + 1).Value = Sheets("Form").Range("H23").Value
        lRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
    End If
    
    If Not IsEmpty(Sheets("Form").Range("K27").Value) Then
        Range("A" & lRow + 1).Value = Sheets("Form").Range("K27").Value
        Range("B" & lRow + 1).Value = Sheets("Form").Range("E27").Value
        Range("C" & lRow + 1).Value = Sheets("Form").Range("F27").Value
        Range("D" & lRow + 1).Value = Sheets("Form").Range("H27").Value
        lRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
    End If
    
    If Not IsEmpty(Sheets("Form").Range("K37").Value) Then
        Range("A" & lRow + 1).Value = Sheets("Form").Range("K37").Value
        Range("B" & lRow + 1).Value = Sheets("Form").Range("E37").Value
        Range("C" & lRow + 1).Value = Sheets("Form").Range("F37").Value
        Range("D" & lRow + 1).Value = Sheets("Form").Range("H37").Value
    End If
    
    
    
    End Sub
    Last edited by mo4391; 07-08-2016 at 04:47 PM. Reason: moved the re-defining of the variable to inside the if statements

  5. #5
    Registered User
    Join Date
    02-10-2016
    Location
    Anaheim, CA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Transfer data from one sheet to another based on cell value not being empty

    That worked perfectly! Thank yo mo4391! Cheers!

    Quote Originally Posted by mo4391 View Post
    Then it sounds like you need to re-define your lRow variable if either of the first two IF statements are true. There are probably other people smarter than me on this message board that would have a more elegant solution, but see if this works for you:

    Private Sub cmdEmail_Click()
    
    Dim lRow As Long
    Dim sht As Worksheet
    Set sht = Sheets("Email")
    lRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
    
    
    If Not IsEmpty(Sheets("Form").Range("K23").Value) Then
        Range("A" & lRow + 1).Value = Sheets("Form").Range("K23").Value
        Range("B" & lRow + 1).Value = Sheets("Form").Range("E23").Value
        Range("C" & lRow + 1).Value = Sheets("Form").Range("F23").Value
        Range("D" & lRow + 1).Value = Sheets("Form").Range("H23").Value
        lRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
    End If
    
    If Not IsEmpty(Sheets("Form").Range("K27").Value) Then
        Range("A" & lRow + 1).Value = Sheets("Form").Range("K27").Value
        Range("B" & lRow + 1).Value = Sheets("Form").Range("E27").Value
        Range("C" & lRow + 1).Value = Sheets("Form").Range("F27").Value
        Range("D" & lRow + 1).Value = Sheets("Form").Range("H27").Value
        lRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
    End If
    
    If Not IsEmpty(Sheets("Form").Range("K37").Value) Then
        Range("A" & lRow + 1).Value = Sheets("Form").Range("K37").Value
        Range("B" & lRow + 1).Value = Sheets("Form").Range("E37").Value
        Range("C" & lRow + 1).Value = Sheets("Form").Range("F37").Value
        Range("D" & lRow + 1).Value = Sheets("Form").Range("H37").Value
    End If
    
    
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Transfer Cell Data From 1 Sheet To Another Based on Criteria
    By thecircularwriter in forum Excel General
    Replies: 10
    Last Post: 01-24-2016, 04:46 PM
  2. [SOLVED] Transfer data from one sheet to another based on value
    By rr23724 in forum Excel General
    Replies: 12
    Last Post: 11-04-2015, 12:50 PM
  3. Transfer data from Master sheet to another sheet based on value.
    By TylerLuk1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-12-2015, 12:00 PM
  4. [SOLVED] Transfer Userform data on 1 worksheet to different worksheet (empty row = wrong sheet)
    By HeyInKy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-26-2014, 10:08 AM
  5. [SOLVED] Transfer cells range to another sheet if first cell of destination range is empty
    By kopapa in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 11-04-2013, 04:32 PM
  6. Transfer cell value from sheet to sheet based on user input (SOLVED)
    By Terrydorset in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-18-2013, 01:28 PM
  7. Transfer Data To Different Sheet Based on Condition
    By Strugggler in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-27-2008, 08:42 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1