Loop thru columns to check for match

jeffreybrown

Well-known Member
Joined
Jul 28, 2004
Messages
5,152
I'm a little stuck on how to loop thru this array to show the correct sheet order. vCOLs represents what should be the headers for each column.

So in this macro, I would like the msgbox output to show what is currently in the column as the header and then show right next to each column header text what it header should actually be.

As you can see in the image, the current columns are 11, but the real columns should only be 9.

VBA Code:
Sub CheckColumns()
Dim c As Long
Dim x As Long
Dim vCOLs As Variant
Dim ColumnLetter As String
Dim strValues As String
x = ActiveSheet.UsedRange.Columns.Count
Dim myCell As Range

    vCOLs = Array("Site", "Name", "Description", "Owner", "Model", "Device Pool", "Calling Search Space Name", "Calling Search Space Name", "Lines")
    With ActiveSheet

For Each myCell In Range(Cells(1, 1), Cells(1, x))
' For c = LBound(vCOLs) To UBound(vCOLs)
ColumnLetter = Split(myCell.Address, "$")(1)
strValues = strValues & "(" & ColumnLetter & ") " & myCell.Value & vbCrLf
' Next c
Next myCell

        MsgBox "Wrong Order with... " & vbCrLf & vbCrLf & strValues, vbOKOnly
    End With

End Sub
 

Attachments

  • Capture.JPG
    Capture.JPG
    19.8 KB · Views: 2

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
How about
VBA Code:
Sub CheckColumns()
Dim c As Long
Dim x As Long
Dim vCOLs As Variant
Dim ColumnLetter As String
Dim strValues As String
x = ActiveSheet.UsedRange.Columns.Count
Dim myCell As Range

    vCOLs = Array("Site", "Name", "Description", "Owner", "Model", "Device Pool", "Calling Search Space Name", "Calling Search Space Name", "Lines")
    With ActiveSheet

For Each myCell In Range(Cells(1, 1), Cells(1, x))
' For c = LBound(vCOLs) To UBound(vCOLs)
ColumnLetter = Split(myCell.Address, "$")(1)
If c <= UBound(vCOLs) Then
   strValues = strValues & "(" & ColumnLetter & ") " & myCell.Value & vbTab & vCOLs(c) & vbCrLf
Else
   strValues = strValues & "(" & ColumnLetter & ") " & myCell.Value & vbTab & "nothing" & vbCrLf
End If
c = c + 1
' Next c
Next myCell

        MsgBox "Wrong Order with... " & vbCrLf & vbCrLf & strValues, vbOKOnly
    End With

End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,813
Messages
6,121,706
Members
449,048
Latest member
81jamesacct

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