This code to run over multiple columns

Pumper

Board Regular
Joined
Sep 12, 2013
Messages
114
Office Version
  1. 365
Hi All, I have this code that Peter_SSs kindly provided that works perfectly.

I would like to expand this code to run over each column if row 1 in that column has something in it.

So once it has run over column A move on to column B etc until there is nothing in row 1 of whatever column that may be.

Have tried to get this to work via a loop but no bueno, here is the code that works on column A

Any assistance would be much appreciated

VBA Code:
Sub Remove_Based_on_Right9()
Sheets("Sheet1").Select
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(isnumber(match(""*""&left(right(#,9),1)&""*"",A1,0)),#,0)", "#", .Address))
    On Error Resume Next
    .SpecialCells(xlConstants, xlNumbers).Delete Shift:=xlUp
    On Error GoTo 0
  End With
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Your sample results for column B look wrong, it contains V & X which are not in B1.

This is a bit long winded but give it a try.
VBA Code:
Sub Remove_Based_on_Right9_v02()

    Dim sht As Worksheet
    Dim rngHdg As Range, rngData As Range
    Dim strFormula As String
    Dim i As Long, lastCol As Long, lastRow As Long
   
    Set sht = Worksheets("Sheet1")
    With sht
        lastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
       
        For i = 1 To lastCol
            Set rngHdg = .Cells(1, i)
            lastRow = .Cells(Rows.Count, i).End(xlUp).Row
           
            If rngHdg.Value <> "" And lastRow <> 1 Then
                Set rngData = .Range(.Cells(2, i), .Cells(lastRow, i))
                With rngData
                    strFormula = "if(isnumber(match(""*""&left(right(#,9),1)&""*"",A1,0)),#,0)"
                    strFormula = Replace(strFormula, "#", .Address)
                    strFormula = Replace(strFormula, "A1", rngHdg.Address(0, 0))
                    Debug.Print strFormula
                    .Value = Evaluate(strFormula)
                    On Error Resume Next
                    .SpecialCells(xlConstants, xlNumbers).Delete Shift:=xlUp
                    On Error GoTo 0
                End With
            End If
        Next i
    End With
End Sub

In case it helps anyone else here is an XL2BB of the test data:

20240911 VBA Loop through columns Pumper.xlsm
AB
1F,J,N,VH,M,U,Z
2PLV4 TesterPAU4 Tester
3PLF5 TesterPAV4 Tester
4PLJ5 TesterPAX4 Tester
5PLN5 TesterPAZ4 Tester
6PLV5 TesterPAH5 Tester
7PLF6 TesterPAM5 Tester
8PLJ6 TesterPAU5 Tester
9PLN6 TesterPAZ5 Tester
10PLV6 TesterPAH6 Tester
11PLF7 TesterPAM6 Tester
12PLJ7 TesterPAU6 Tester
13PLN7 TesterPAZ6 Tester
14PLX7 TesterPAH7 Tester
15PAM7 Tester
16PAX7 Tester
Sheet1 (2)
 
Upvote 1
Solution
Your sample results for column B look wrong, it contains V & X which are not in B1.

This is a bit long winded but give it a try.
VBA Code:
Sub Remove_Based_on_Right9_v02()

    Dim sht As Worksheet
    Dim rngHdg As Range, rngData As Range
    Dim strFormula As String
    Dim i As Long, lastCol As Long, lastRow As Long
  
    Set sht = Worksheets("Sheet1")
    With sht
        lastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
      
        For i = 1 To lastCol
            Set rngHdg = .Cells(1, i)
            lastRow = .Cells(Rows.Count, i).End(xlUp).Row
          
            If rngHdg.Value <> "" And lastRow <> 1 Then
                Set rngData = .Range(.Cells(2, i), .Cells(lastRow, i))
                With rngData
                    strFormula = "if(isnumber(match(""*""&left(right(#,9),1)&""*"",A1,0)),#,0)"
                    strFormula = Replace(strFormula, "#", .Address)
                    strFormula = Replace(strFormula, "A1", rngHdg.Address(0, 0))
                    Debug.Print strFormula
                    .Value = Evaluate(strFormula)
                    On Error Resume Next
                    .SpecialCells(xlConstants, xlNumbers).Delete Shift:=xlUp
                    On Error GoTo 0
                End With
            End If
        Next i
    End With
End Sub

In case it helps anyone else here is an XL2BB of the test data:

20240911 VBA Loop through columns Pumper.xlsm
AB
1F,J,N,VH,M,U,Z
2PLV4 TesterPAU4 Tester
3PLF5 TesterPAV4 Tester
4PLJ5 TesterPAX4 Tester
5PLN5 TesterPAZ4 Tester
6PLV5 TesterPAH5 Tester
7PLF6 TesterPAM5 Tester
8PLJ6 TesterPAU5 Tester
9PLN6 TesterPAZ5 Tester
10PLV6 TesterPAH6 Tester
11PLF7 TesterPAM6 Tester
12PLJ7 TesterPAU6 Tester
13PLN7 TesterPAZ6 Tester
14PLX7 TesterPAH7 Tester
15PAM7 Tester
16PAX7 Tester
Sheet1 (2)
Sorry on the test results, you are correct.

Your code worked too! ran pretty quickly so no issues there.

Thank you very much for taking the time to look at this 👍
 
Upvote 0
Try.
VBA Code:
Sub Remove_Based_on_Right9()
Dim Clms&, Lr&, T&, k, z$
Dim Rng As Range
Sheets("Sheet1").Select
Set Rng = Range("A:F")  'Select the column(here it is F) as required
Clms = Rng.Columns.Count
 
  For T = 1 To Clms
    Lr = Cells(Rows.Count, T).End(xlUp).Row
    With Range(Cells(2, T), Cells(Lr, T))
    z = .Address
    k = Cells(1, T).Address
        If k <> "" Then
        .Value = Evaluate("if(isnumber(match(""*""&left(right(" & z & ",9),1)&""*""," & k & ",0))," & z & ","""")")
        On Error Resume Next
        .SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
        On Error GoTo 0
        End If
    End With
  Next T
End Sub
 
Upvote 0
Try.
VBA Code:
Sub Remove_Based_on_Right9()
Dim Clms&, Lr&, T&, k, z$
Dim Rng As Range
Sheets("Sheet1").Select
Set Rng = Range("A:F")  'Select the column(here it is F) as required
Clms = Rng.Columns.Count
 
  For T = 1 To Clms
    Lr = Cells(Rows.Count, T).End(xlUp).Row
    With Range(Cells(2, T), Cells(Lr, T))
    z = .Address
    k = Cells(1, T).Address
        If k <> "" Then
        .Value = Evaluate("if(isnumber(match(""*""&left(right(" & z & ",9),1)&""*""," & k & ",0))," & z & ","""")")
        On Error Resume Next
        .SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
        On Error GoTo 0
        End If
    End With
  Next T
End Sub
Hi, sorry late reply, must be in different time zones.

Nice job this works!

Thanks for coming back multiple times to crack this, very kind 👍
 
Upvote 0

Forum statistics

Threads
1,223,788
Messages
6,174,570
Members
452,573
Latest member
Cpiet

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