• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 678
  • Last Modified:

VBA to ignore blank cells and continue loop

Hello,

I have a vba code that combines first and last name from column A and B in Column C. I need to add a line in the code that ignores the blank rows and continues till the end. attached is the sample file.
vba-to-combine-first-and-last-name.xlsb
0
Leonardo M
Asked:
Leonardo M
1 Solution
 
Rgonzo1971Commented:
Hi,

pls try
Sub joinfirstandlastname()

        
        Sheets("Sheet1").Select
        Range("C2").Select
        For Each c In Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
            c.Offset(, 2) = c.Value & " " & c.Offset(, 1)
        Next

End Sub

Open in new window

Regards
0
 
Rob HensonIT & Database AssistantCommented:
Replace line 7 with:

If c.Value <> "" And c.Offset(, 1) <> "" Then c.Offset(, 2) = c.Value & " " & c.Offset(, 1)
0
 
MacroShadowCommented:
Try this:
Sub joinfirstandlastname()
    Dim rng As Range, cell As Range
    Dim lngLastRow As Long

    With ActiveSheet
        lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    Set rng = Range("A2:A" & lngLastRow)

    For Each cell In rng
        cell.Offset(0, 2).Value = cell.Value & " " & cell.Offset(0, 1)
    Next cell
End Sub

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Rgonzo1971Commented:
maybe better
Sub joinfirstandlastname()

        
        Sheets("Sheet1").Select
        Range("C2").Select
        For Each c In Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
            c.Offset(, 2) = Trim(c.Value & " " & c.Offset(, 1))
        Next

End Sub

Open in new window

0
 
Leonardo MAuthor Commented:
Thanks Rgonzo,


That works. I have a follow up question on this.

If I want to get the data for column A and B from another excel file, what code I can use to ask the user to select a file from a location and copy that into this range (A and B). After the data is pasted, then the joining code should run.

thank you for your help!
0
 
Rgonzo1971Commented:
then try
Sub joinfirstandlastname()

    strFilename = Application.GetOpenFilename("Excel Files, *.xl*")
    If strFilename = False Then Exit Sub
    Set wbk = Workbooks.Open(strFilename)
    Range(Range("A1"), Range("B" & Rows.Count).End(xlUp)).Copy ThisWorkbook.Sheets("Sheet1").Range("A1")
    ThisWorkbook.Activate
    For Each c In Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
        c.Offset(, 2) = Trim(c.Value & " " & c.Offset(, 1))
    Next
    wbk.Close False
End Sub

Open in new window

0
 
Leonardo MAuthor Commented:
Many thanks Rgonzo for your fantastic help!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now