?
Solved

Need a code wizard to help with this one

Posted on 2013-01-29
5
Medium Priority
?
148 Views
Last Modified: 2013-01-29
I'm hoping someone can write the macro code to do the following to the attached file.

Step 1:  Remove any spaces AFTER the last character in column C
Step 2:  Take all characters after the first space in column A and put it after the last character in column C (with a space between the original character string in column C and the new text to be placed there

Row 7 shows exactly what I want the end result to be.  The original file is in the tab named "Before".  I'd like the end result to be in the tab named "After"

Can someone help?

--Steve
Test-File-To-Alter.xlsx
0
Comment
Question by:SteveL13
[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
  • 3
5 Comments
 
LVL 26

Accepted Solution

by:
redmondb earned 2000 total points
ID: 38832895
Hi, SteveL13.

Please see attached. A few points...
(1) The example shows spaces removed from BEFORE the original Col. C values, so that's what I did as well. A simple change to do AFTER, or BOTH.
(2) The example shows leading spaces removed from the extracted Col. A values, so that's what I did as well. Again, a simple change to undo this.
(3) The example shows the exctracted data dropped from the Col. A cells, so that's what I did. Again, an easy chase.
(4) The macro starts in Row 11. Is that correct?

The code is...
Option Explicit

Sub Concat()
Dim xLast_Row As Long
Dim xCell As Range

Sheets("Sheet1").Activate

xLast_Row = Range("A1").SpecialCells(xlLastCell).Row
If xLast_Row < 11 Then
    MsgBox ("No data found - run cancelled.")
    Exit Sub
End If

For Each xCell In Range("C11:C" & xLast_Row)
    xCell = LTrim(xCell) & IIf(xCell.Offset(0, -2) = "", "", " " & LTrim(Mid(xCell.Offset(0, -2), InStr(1, xCell.Offset(0, -2) & " ", " "), 9999)))
    If xCell.Offset(0, -2) <> "" Then
        xCell.Offset(0, -2) = RTrim(Mid(xCell.Offset(0, -2), 1, InStr(1, xCell.Offset(0, -2) & " ", " ") - 1))
    End If
Next

End Sub

Open in new window

Regards,
Brian.Test-File-To-Alter-V2.xlsm
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38832905
Sub switchtext()
    Dim cel As Range
    For Each cel In Range("A11:A" & Range("A" & Rows.Count).End(xlUp).Row)
        cel.Offset(, 2) = WorksheetFunction.Trim(cel.Offset(, 2) & " " & Right(cel, Len(cel) - InStr(cel & " ", " ") + 1))
        cel.Value = Left(cel, InStr(cel & " ", " ") - 1)
    Next cel
End Sub
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38832982
SteveL13,

My first version update the cells in place, but in case you want it written to a new sheet...
Option Explicit

Sub Concat()
Dim xLast_Row As Long
Dim xCell As Range
Dim xSrce As Worksheet
Dim xDest As Worksheet
Dim xRow As Long

Set xSrce = Sheets("Sheet1")

xLast_Row = xSrce.Range("A1").SpecialCells(xlLastCell).Row
If xLast_Row < 11 Then
    MsgBox ("No data found - run cancelled.")
    Exit Sub
End If

Application.ScreenUpdating = False

Set xDest = Sheets.Add
xSrce.Range("A10:C10").Copy Destination:=xDest.Range("A1")
xRow = 1

For Each xCell In xSrce.Range("C11:C" & xLast_Row)
    xRow = xRow + 1
    
    xDest.Range("A" & xRow & ":" & "C" & xRow) = Array( _
        IIf(xCell.Offset(0, -2) = "", "", RTrim(Mid(xCell.Offset(0, -2), 1, InStr(1, xCell.Offset(0, -2) & "  ", " ") - 1))) _
        , xCell.Offset(0, -1) _
        , LTrim(xCell) & IIf(xCell.Offset(0, -2) = "", "", " " & LTrim(Mid(xCell.Offset(0, -2), InStr(1, xCell.Offset(0, -2) & " ", " "), 9999))))
Next

Application.ScreenUpdating = True

End Sub

Open in new window

Regards,
Brian.Test-File-To-Alter-V3.xlsm
0
 

Author Closing Comment

by:SteveL13
ID: 38833050
Row 11 is the start, yes.

As far as I can tell what you did is perfect!  Thank you.
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38833086
Thanks, SteveL13!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

800 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