Assigning keys in macro

Edwardm23

New Member
Joined
Mar 1, 2011
Messages
21
All

How can you assign multiple columns in a worksheet to a single key in a macro?

Please see below

Option Explicit<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Public Sub CombineRows2()<o:p></o:p>
<o:p></o:p>
Const sKeyCol As String = "C"<o:p></o:p>
Const sColIn As String = "K"<o:p></o:p>
Const sColOut As String = "T"<o:p></o:p>
Const iStartRow As Long = 2<o:p></o:p>
<o:p></o:p>
Dim ws As Worksheet<o:p></o:p>
Dim iLastRow As Long<o:p></o:p>
Dim iRow As Long<o:p></o:p>
Dim iRowOut As Long<o:p></o:p>
Dim sCustID As String<o:p></o:p>
Dim iMoved As Long<o:p></o:p>
<o:p></o:p>
Set ws = ThisWorkbook.Sheets("Sheet1")<o:p></o:p>
iLastRow = ws.Cells(ws.Rows.Count, sKeyCol).End(xlUp).Row<o:p></o:p>
ws.Range(sColOut & CStr(iStartRow) & ":" & sColOut & CStr(Rows.Count)).ClearContents<o:p></o:p>
<o:p></o:p>
iRow = iStartRow<o:p></o:p>
iMoved = 0<o:p></o:p>
sCustID = ""<o:p></o:p>
<o:p></o:p>
Do Until iRow > iLastRow<o:p></o:p>
If ws.Cells(iRow, sKeyCol) <> sCustID Then<o:p></o:p>
iRowOut = iRow<o:p></o:p>
sCustID = ws.Cells(iRow, sKeyCol)<o:p></o:p>
End If<o:p></o:p>
If Not IsEmpty(ws.Cells(iRow, sColIn)) Then<o:p></o:p>
If IsEmpty(ws.Cells(iRowOut, sColOut)) Then<o:p></o:p>
ws.Cells(iRowOut, sColOut) = ws.Cells(iRow, sColIn).Value<o:p></o:p>
iMoved = iMoved + 1<o:p></o:p>
End If<o:p></o:p>
End If<o:p></o:p>
iRow = iRow + 1<o:p></o:p>
DoEvents<o:p></o:p>
<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:place>Loop</st1:place><o:p></o:p>
<o:p></o:p>
MsgBox "Done: " & CStr(iRow - iStartRow + 1) & " records read, " _<o:p></o:p>
& CStr(iMoved) & " fields moved." & Space(10), vbOKOnly + vbInformation<o:p></o:p>
<o:p></o:p>

End Sub


This macro assigns a single column to - sKeyCol. How can I change this to assign 2,3 or 4 columns to sKeyCol?

Thanks
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Dim sKeyCol as a range, then use Union

Code:
Dim skeyCol As Range
Set skeyCol = Union(Range("C:C"), Range("K:K"), Range("T:T"))
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,172
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top