Solved

VBA to ignore blank cells and continue loop

Posted on 2016-11-02
7
28 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 48

Expert Comment

by:Rgonzo1971
Comment Utility
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 31

Expert Comment

by:Rob Henson
Comment Utility
Replace line 7 with:

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

Expert Comment

by:MacroShadow
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 48

Expert Comment

by:Rgonzo1971
Comment Utility
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
Comment Utility
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 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
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
Comment Utility
Many thanks Rgonzo for your fantastic help!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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;…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now