Hello,
I'm working in Excel 2003. I have reports that get dumped into worksheets. The problem is that names(Client Name) are in 1 column as last, first format. I need to separate them out into 2 columns of Last and First. Some reports the Client Name column is in Column A and in some they end up in another column. I could create individual macros for each report, but instead I would like to create one macro that will look for the title text string as a variable txt_TCName.
Here is the initial macro I developed:
' SeparateName Macro
' Create first name and last name from full name.
'
Dim LastRow As Long, r As Long 'Last Row
'Find the LAST real row
LastRow = ActiveSheet.Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row
'
Columns("B:B").Select
Selection.Insert Shift:=xlToRight
Selection.Insert Shift:=xlToRight
Selection.Insert Shift:=xlToRight
Selection.Insert Shift:=xlToRight
Columns("B:E").Select
Selection.ColumnWidth = 20
Range("B1").Select
ActiveCell.FormulaR1C1 = "Last Name"
With ActiveCell.Characters(Start:=1, Length:=9).Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 10
End With
Range("B2").Select
ActiveCell.FormulaR1C1 = "=LEFT(RC[-1],FIND("","",RC[-1])-1)"
Range("B2").Select
Selection.AutoFill Destination:=Range("B2:B" & LastRow), Type:=xlFillDefault
Range("B2:B" & LastRow).Select
Range("D1").Select
ActiveCell.FormulaR1C1 = "First Name"
With ActiveCell.Characters(Start:=1, Length:=10).Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 10
End With
Range("D2").Select
ActiveCell.FormulaR1C1 = _
"=RIGHT(RC[-3],LEN(RC[-3])-FIND(""*"",SUBSTITUTE(RC[-3],"","",""*"",LEN(RC[-3])-LEN(SUBSTITUTE(RC[-3],"","","""")))))"
Range("D2").Select
Selection.AutoFill Destination:=Range("D2
" & LastRow), Type:=xlFillDefault
Range("D2
" & LastRow).Select
Range("B1:B" & LastRow).Select
Selection.Copy
Range("C1").Select
Selection.PasteSpecial Paste:=xlPasteValues
Range("D1
" & LastRow).Select
Application.CutCopyMode = False
Selection.Copy
Range("E1").Select
Selection.PasteSpecial Paste:=xlPasteValues
Columns("D
").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("A:B").Select
Range("B1").Activate
Selection.Delete Shift:=xlToLeft
Range("C12").Select
All help is really appreaciated.
I'm working in Excel 2003. I have reports that get dumped into worksheets. The problem is that names(Client Name) are in 1 column as last, first format. I need to separate them out into 2 columns of Last and First. Some reports the Client Name column is in Column A and in some they end up in another column. I could create individual macros for each report, but instead I would like to create one macro that will look for the title text string as a variable txt_TCName.
Here is the initial macro I developed:
' SeparateName Macro
' Create first name and last name from full name.
'
Dim LastRow As Long, r As Long 'Last Row
'Find the LAST real row
LastRow = ActiveSheet.Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row
'
Columns("B:B").Select
Selection.Insert Shift:=xlToRight
Selection.Insert Shift:=xlToRight
Selection.Insert Shift:=xlToRight
Selection.Insert Shift:=xlToRight
Columns("B:E").Select
Selection.ColumnWidth = 20
Range("B1").Select
ActiveCell.FormulaR1C1 = "Last Name"
With ActiveCell.Characters(Start:=1, Length:=9).Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 10
End With
Range("B2").Select
ActiveCell.FormulaR1C1 = "=LEFT(RC[-1],FIND("","",RC[-1])-1)"
Range("B2").Select
Selection.AutoFill Destination:=Range("B2:B" & LastRow), Type:=xlFillDefault
Range("B2:B" & LastRow).Select
Range("D1").Select
ActiveCell.FormulaR1C1 = "First Name"
With ActiveCell.Characters(Start:=1, Length:=10).Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 10
End With
Range("D2").Select
ActiveCell.FormulaR1C1 = _
"=RIGHT(RC[-3],LEN(RC[-3])-FIND(""*"",SUBSTITUTE(RC[-3],"","",""*"",LEN(RC[-3])-LEN(SUBSTITUTE(RC[-3],"","","""")))))"
Range("D2").Select
Selection.AutoFill Destination:=Range("D2
Range("D2
Range("B1:B" & LastRow).Select
Selection.Copy
Range("C1").Select
Selection.PasteSpecial Paste:=xlPasteValues
Range("D1
Application.CutCopyMode = False
Selection.Copy
Range("E1").Select
Selection.PasteSpecial Paste:=xlPasteValues
Columns("D
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("A:B").Select
Range("B1").Activate
Selection.Delete Shift:=xlToLeft
Range("C12").Select
All help is really appreaciated.