Loop through column titles (1st row) if name is in array

FuNeS13

Board Regular
Joined
Oct 25, 2016
Messages
160
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. Mobile
  3. Web
I know I'm doing something wrong in this part of the code, but I don't know what...

VBA Code:
Dim MyArray As Variant
Dim found As Boolean
Dim uB As Integer, lB As Integer
Dim LC As Long

MyArray = Array("Type", "Number", "Order", "Invoice Date", "Due/Paid Date", "Amount", "Shipment", "Customer", "Name", "Credit Limit", "Chk/Ref")
LC = Cells(1, Columns.Count).End(xlToLeft).Column '56 columns
     uB = UBound(MyArray)
    lB = LBound(MyArray)
c = 1

For i = lB To uB
        Do Until c = LC + 1
                If Range(Split(Cells(1, c).Address, "$")(1) & "1").Value = MyArray(i) Then
                    found = True
                Else
                    found = False
                End If
                
                If found = False Then
                    Range(Split(Cells(1, c).Address, "$")(1) & ":" & Split(Cells(1, c).Address, "$")(1)).Select
                    Selection.Delete Shift:=xlToLeft
                Else
                GoTo nexti
                End If
        c = c + 1
        Loop
nexti:
Next i

what I want to do is if the titles on the first row exist in my array list, then I want to keep that column, if it doesn't exist then I want the column to be removed...
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
And the problem is? Aside from undeclared variables (i and C, something I virtually never do) one possible anomaly I see is that your loop is limited to the Ubound of your array (10) but your column count is 56 according to your code comments.
Should
For i = lB To uB be
For i = lB To LC ?
and do away with the Do Until ?
EDIT = maybe not. Will keep looking but I don't know what the issue is.
 
Upvote 0
OK, I think your issue is that incrementing i is in the wrong place, or you need to increment it in more than one place, or you need to reset C before the outer loop loops again. C reaches the max in the inner loop and i is still 0 so nothing beyond the first array element is ever used. Getting late here now, so I have to leave it for this evening.
Except resetting C will mess up Range(Split(Cells(1, C).Address, "$")(1) & "1")
 
Last edited:
Upvote 1
both i and c are declared outside the sub-procedure, I have a code template I usually start my work with and have several things predeclared...

the original column count is 56, I want to find out if any of those columns -1st rows- have one of the 10 values in my array... if it doesn't then I want that column to be deleted...

My apologies for not making my request clear.
 
Upvote 0
OK, I think your issue is that incrementing i is in the wrong place, or you need to increment it in more than one place, or you need to reset C before the outer loop loops again. C reaches the max in the inner loop and i is still 0 so nothing beyond the first array element is ever used. Getting late here now, so I have to leave it for this evening.
okay let me review your answer and see if I can fix it based on your comment.
 
Upvote 0
I keep saying I gotta quit but I didn't. 😖
Add this before the Select
Debug.Print Range(Split(Cells(1, C).Address, "$")(1) & "1"), step through your code and keep looking at your sheet after the select. I see the selection moving to the 2nd column but since the first column was deleted (in my case) it moves onto the second and leaves the (now) first column alone. Usually when you delete rows/columns you count backwards from right to left or down to up as the case may be. That's because the rows/columns are shifting left/up and you don't want that.
PS. I meant I knew what you wanted to have happen, I didn't know what was/is wrong. Hope you didn't think I was too curt.
This time I have to put it away for the night!
 
Upvote 0
what I want to do is if the titles on the first row exist in my array list, then I want to keep that column, if it doesn't exist then I want the column to be removed...
Try this code:
VBA Code:
Sub FuNeS13()

Dim c As Range, f As Range
Dim myHeader As String
myHeader = "|Type|Number|Order|Invoice Date|Due/Paid Date|Amount|Shipment|Customer|Name|Credit Limit|Chk/Ref|"

For Each f In Range("A1", Cells(1, Columns.Count).End(xlToLeft))
    If InStr(1, myHeader, "|" & f.Value & "|", vbTextCompare) = 0 Then
        If Not c Is Nothing Then
            Set c = Union(c, f)
        Else
            Set c = f
        End If
    End If
Next

c.EntireColumn.Delete

End Sub
 
Upvote 0
Solution
Another way is to iterate through each cell in row 1, use the MATCH function to check for existence in MyArray. If it doesn't exist, change the value to an "error".
Then, use SpecialCells to highlight the column, and afterwards perform a single delete operation (in the code, use .select for verification, and if it's fine, replace it with .delete).

VBA Code:
Dim LC As Long, myArray, ce As Range
LC = Cells(1, Columns.Count).End(xlToLeft).Column
myArray = Array("Type", "Number", "Order", "Invoice Date", "Due/Paid Date", "Amount", "Shipment", "Customer", "Name", "Credit Limit", "Chk/Ref")
For Each ce In Range("A1", Cells(1, LC))
    If Not IsNumeric(Application.Match(ce, myArray, 0)) Then
        ce.Value = "#N/A"
    End If
Next
Range("A1", Cells(1, LC)).SpecialCells(xlCellTypeConstants, xlErrors).EntireColumn.Select
'Range("A1", Cells(1, LC)).SpecialCells(xlCellTypeConstants, xlErrors).EntireColumn.Delete
 
Upvote 1
Try this code:
VBA Code:
Sub FuNeS13()

Dim c As Range, f As Range
Dim myHeader As String
myHeader = "|Type|Number|Order|Invoice Date|Due/Paid Date|Amount|Shipment|Customer|Name|Credit Limit|Chk/Ref|"

For Each f In Range("A1", Cells(1, Columns.Count).End(xlToLeft))
    If InStr(1, myHeader, "|" & f.Value & "|", vbTextCompare) = 0 Then
        If Not c Is Nothing Then
            Set c = Union(c, f)
        Else
            Set c = f
        End If
    End If
Next

c.EntireColumn.Delete

End Sub
thanks, this worked exactly as I was expecting it... would you mind explaining it a little bit to me?
 
Upvote 0

Forum statistics

Threads
1,215,077
Messages
6,122,992
Members
449,094
Latest member
masterms

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