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...
 
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
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
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

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,216,113
Messages
6,128,907
Members
449,478
Latest member
Davenil

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