?
Solved

VBA to ignore blank cells and continue loop

Posted on 2016-11-02
7
Medium Priority
?
337 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 52

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 52

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 52

Accepted Solution

by:
Rgonzo1971 earned 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

770 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