Solved

VBA to ignore blank cells and continue loop

Posted on 2016-11-02
7
178 Views
Last Modified: 2016-11-02
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
Comment
Question by:Leonardo M
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 41870135
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
 
LVL 33

Expert Comment

by:Rob Henson
ID: 41870137
Replace line 7 with:

If c.Value <> "" And c.Offset(, 1) <> "" Then c.Offset(, 2) = c.Value & " " & c.Offset(, 1)
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 41870141
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!

 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 41870142
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
 

Author Comment

by:Leonardo M
ID: 41870302
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
 
LVL 51

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 41870330
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
 

Author Closing Comment

by:Leonardo M
ID: 41870383
Many thanks Rgonzo for your fantastic help!
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question