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

FuNeS13

Board Regular
Joined
Oct 25, 2016
Messages
161
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...
 
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
thank you, I also tried this solution and it worked as expected... I think your approach I understand a bit more than the previous one.
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Nice to hear it works.

Let me break down the provided VBA code step by step:

VBA Code:
Dim LC As Long, myArray, ce As Range
This line declares variables LC, myArray, and ce of type Long, Variant, and Range respectively.

VBA Code:
LC = Cells(1, Columns.Count).End(xlToLeft).Column
Here, it calculates the last used column in the first row by finding the rightmost cell that contains data (by going from the last column towards the left) using the xlToLeft parameter.

VBA Code:
myArray = Array("Type", "Number", "Order", "Invoice Date", "Due/Paid Date", "Amount", "Shipment", "Customer", "Name", "Credit Limit", "Chk/Ref")
An array named myArray is created and filled with specific strings that will be used later for comparison.

VBA Code:
For Each ce In Range("A1", Cells(1, LC))
This initiates a loop that goes through each cell in the range starting from cell A1 to the last cell in the first row (determined by the LC value calculated earlier). The loop variable ce represents each cell in this range.

VBA Code:
If Not IsNumeric(Application.Match(ce, myArray, 0)) Then
ce.Value = "#N/A"
End If
For each cell in the range, it uses the Application.Match function to see if the value in that cell exists in the myArray. If it doesn't find a match (indicated by IsNumeric(Application.Match(ce, myArray, 0)) being False), it replaces the value in that cell with "#N/A".

VBA Code:
Range("A1", Cells(1, LC)).SpecialCells(xlCellTypeConstants, xlErrors).EntireColumn.Select
'Range("A1", Cells(1, LC)).SpecialCells(xlCellTypeConstants, xlErrors).EntireColumn.Delete
This line selects the entire columns that have constant (non-formula) values with errors (in this case, the cells that were previously changed to "#N/A"). The second line, which is commented out, would delete these selected columns. However, as indicated by the comment, it's currently commented out. To perform the deletion, you would uncomment this line.
 
Upvote 1
thanks, this worked exactly as I was expecting it... would you mind explaining it a little bit to me?
I added some comments:
VBA Code:
Sub FuNeS13()

Dim c As Range, f As Range
Dim myHeader As String
'put the headers to keep, each is enclosed in pipe
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))  'loop each cell in row 1 until last column  with data
                                                                  'vbTextCompare means it's case insensitive
    If InStr(1, myHeader, "|" & f.Value & "|", vbTextCompare) = 0 Then 'if the cell value (enclosed in pipe) is not found in myHeader
        If Not c Is Nothing Then
            Set c = Union(c, f) 'combine cells/range that meet criteria to delete
        Else
            Set c = f 'the first cell that meet criteria to delete
        End If
    End If
Next

c.EntireColumn.Delete 'delete whole column of cells/range that meet criteria to delete

End Sub
 
Upvote 1

Forum statistics

Threads
1,223,880
Messages
6,175,152
Members
452,615
Latest member
bogeys2birdies

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