Solved

VBA to ignore blank cells and continue loop

Posted on 2016-11-02
7
76 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
7 Comments
 
LVL 50

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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 50

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 50

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
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 demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

828 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