LittleGriff
New Member
- Joined
- Jan 13, 2012
- Messages
- 11
Hi, and thanks in advance. I have a macro that merges a two-column range in my worksheet. Right now I'm duplicating and editing the macro for multiple ranges of columns, because the macro is appropriate for about 10 ranges in my worksheet, maybe more.
Is there a way to add a "For Each Range..." type loop to specify each pair of columns that will be joined/merged? Here's the macro (it's rudimentary; I'm a beginner). Any place there's a string variable or a column letter, would be where I would like to substitute a placeholder of some sort so the macro will run on every pair of "qualifying" columns.
Thanks again!
<code>
</code>
Is there a way to add a "For Each Range..." type loop to specify each pair of columns that will be joined/merged? Here's the macro (it's rudimentary; I'm a beginner). Any place there's a string variable or a column letter, would be where I would like to substitute a placeholder of some sort so the macro will run on every pair of "qualifying" columns.
Thanks again!
<code>
Code:
Sub PreferredReturnPercent() 'PreferredReturnPercent and PreferredReturnType columns DG and DH
' format 0.00% (text details)
Application.ScreenUpdating = False
Dim startRow As Long
Dim lastRow As Long
Dim i As Long 'counter to loop through cells
Dim prefRetPct As String 'text of the 1st column contents
Dim prefRetType As String ' text of the 2nd column contents
startRow = 2
lastRow = Sheets("SheetName").Cells(Sheets("SheetName").Rows.Count, 1).End(xlUp).Row
For i = startRow To lastRow
prefRetPct = Sheets("SheetName").Range("DG" & i).Value
prefRetType = Sheets("SheetName").Range("DH" & i).Value
If prefRetPct <> "" And prefRetType <> "" Then
newString = Format(Worksheets("SheetName").Range("DG" & i).Value, "0.00%") & vbNewLine & " (" & Worksheets("SheetName").Range("DH" & i).Value & ")"
Worksheets("SheetName").Range("DG" & i).Value = newString 'write the new string to the first column; second column to be deleted later
Range("DG:DG").HorizontalAlignment = xlCenter
Else
End If
Next
Application.ScreenUpdating = True
End Sub
Last edited: