Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Concatinate and Trim Column A and B

Posted on 2011-09-13
6
Medium Priority
?
266 Views
Last Modified: 2012-05-12
I am looking for a macro that will work with column A and B.  First I would like to concatenate column A with column B, however column B has unwanted spaces in it, so I need to TRIM out or CLEAN column B either before or after.  Is there an easier way to do this by the column instead of having to copy the formula down and copy/paste specail values etc.  There is no set number of rows, which can change from day-to-day.

Would like to automate this.  Please advise and thanks. -R-
0
Comment
Question by:RWayneH
  • 4
  • 2
6 Comments
 
LVL 13

Expert Comment

by:nike_golf
ID: 36530918
This is quick and dirty but you get the idea. The concatenated item is placed in column "C", this also assumes there are no blank rows in colum "A".

Public Sub concat()
Dim rng, x As Range

Set rng = ActiveSheet.Range("a1", Range("A1").End(xlDown))

For Each x In rng
    x.Offset(0, 3) = x & Trim(x.Offset(0, 1))
Next x

End Sub

NG,


0
 
LVL 13

Expert Comment

by:nike_golf
ID: 36530923
Sorry the answer is put in column "D"... and can be adjusted by the x.Offset(0, 3)

NG,

0
 

Author Comment

by:RWayneH
ID: 36531101
Sorry, I forgot to ask....  Is there a way to put a hyphen between the values in Col A and Col B?  -R-
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 13

Accepted Solution

by:
nike_golf earned 2000 total points
ID: 36531187
Public Sub concat()
Dim rng, x As Range

Set rng = ActiveSheet.Range("a1", Range("A1").End(xlDown))

For Each x In rng
    x.Offset(0, 3) = x & " - " & Trim(x.Offset(0, 1))
Next x

End Sub

NG,
0
 

Author Closing Comment

by:RWayneH
ID: 36531400
This is an EXCELent and very flexible solution.  Thanks. -R-
0
 
LVL 13

Expert Comment

by:nike_golf
ID: 36531730
Glad I could help.

NG,
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
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 the scrolling table in Microsoft Excel using the INDEX function.

783 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