This code to run over multiple columns

Pumper

Board Regular
Joined
Sep 12, 2013
Messages
111
Office Version
  1. 2016
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

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
TRy
VBA Code:
Sub Remove_Based_on_Right9()
Dim Clms&, T&, k
Dim Rng As Range
Sheets("Sheet1").Select
Set Rng = Range("A2:F" & Range("A" & Rows.Count).End(xlUp).Row) 'Select the column(here itis F) as required
Clms = Rng.Columns.Count
  
  With Rng
  For T = 1 To Clms
    With .Columns(T)
    k = .Cells(1, 1)
    .Value = Evaluate(Replace("if(isnumber(match(""*""&left(right(#,9),1)&""*"",k,0)),#,0)", "#", .Address))
    On Error Resume Next
    .SpecialCells(xlConstants, xlNumbers).Delete Shift:=xlUp
    On Error GoTo 0
    End With
  Next T
  End With
End Sub
 
Upvote 0
Code modified
VBA Code:
Sub Remove_Based_on_Right9()
Dim Clms&, T&, k
Dim Rng As Range
Sheets("Sheet1").Select
Set Rng = Range("A2:F" & Range("A" & Rows.Count).End(xlUp).Row) 'Select the column(here itis F) as required
Clms = Rng.Columns.Count
  
  With Rng
  For T = 1 To Clms
    With .Columns(T)
    k = .Cells(1, 1)
        If k <> "" Then
        .Value = Evaluate(Replace("if(isnumber(match(""*""&left(right(#,9),1)&""*"",k,0)),#,0)", "#", .Address))
        On Error Resume Next
        .SpecialCells(xlConstants, xlNumbers).Delete Shift:=xlUp
        On Error GoTo 0
        End If
    End With
  Next T
  End With
End Sub
 
Upvote 0
Code modified
VBA Code:
Sub Remove_Based_on_Right9()
Dim Clms&, T&, k
Dim Rng As Range
Sheets("Sheet1").Select
Set Rng = Range("A2:F" & Range("A" & Rows.Count).End(xlUp).Row) 'Select the column(here itis F) as required
Clms = Rng.Columns.Count
 
  With Rng
  For T = 1 To Clms
    With .Columns(T)
    k = .Cells(1, 1)
        If k <> "" Then
        .Value = Evaluate(Replace("if(isnumber(match(""*""&left(right(#,9),1)&""*"",k,0)),#,0)", "#", .Address))
        On Error Resume Next
        .SpecialCells(xlConstants, xlNumbers).Delete Shift:=xlUp
        On Error GoTo 0
        End If
    End With
  Next T
  End With
End Sub
Thanks for taking the time to look at this kvsrinivasamurthy

It runs but not giving the correct results unfortunately, some columns returning nothing others returning some values but not correct.
 
Upvote 0
Sorry I thought first cell of range. Actually it is first cell of that column. Code Modified . Pl Try now.
If Problem is there Explain What is the problem.
VBA Code:
Sub Remove_Based_on_Right9()
Dim Clms&, T&, k
Dim Rng As Range
'Sheets("Sheet1").Select
Set Rng = Range("A2:F" & Range("A" & Rows.Count).End(xlUp).Row) 'Select the column(here itis F) as required
Clms = Rng.Columns.Count
  
  With Rng
  For T = 1 To Clms
    With .Columns(T)
    k = Cells(1, T)
        If k <> "" Then
        .Value = Evaluate(Replace("if(isnumber(match(""*""&left(right(#,9),1)&""*"",k,0)),#,0)", "#", .Address))
        On Error Resume Next
        .SpecialCells(xlConstants, xlNumbers).Delete Shift:=xlUp
        On Error GoTo 0
        End If
    End With
  Next T
  End With
End Sub
 
Upvote 0
Sorry I thought first cell of range. Actually it is first cell of that column. Code Modified . Pl Try now.
If Problem is there Explain What is the problem.
VBA Code:
Sub Remove_Based_on_Right9()
Dim Clms&, T&, k
Dim Rng As Range
'Sheets("Sheet1").Select
Set Rng = Range("A2:F" & Range("A" & Rows.Count).End(xlUp).Row) 'Select the column(here itis F) as required
Clms = Rng.Columns.Count
 
  With Rng
  For T = 1 To Clms
    With .Columns(T)
    k = Cells(1, T)
        If k <> "" Then
        .Value = Evaluate(Replace("if(isnumber(match(""*""&left(right(#,9),1)&""*"",k,0)),#,0)", "#", .Address))
        On Error Resume Next
        .SpecialCells(xlConstants, xlNumbers).Delete Shift:=xlUp
        On Error GoTo 0
        End If
    End With
  Next T
  End With
End Sub
Thanks again!

Tried this but still giving wrong results for example column A returns blanks when it shouldn't, Column B appears correct, column D returned 2 when should have been more...

Hopefully i am being clear here, this would work but trying to avoid doing like this obviously


VBA Code:
Sub Remove_Based_on_Right9_A()
  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

then

VBA Code:
Sub Remove_Based_on_Right9_B()
  With Range("B2", Range("B" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(isnumber(match(""*""&left(right(#,9),1)&""*"",B1,0)),#,0)", "#", .Address))
    On Error Resume Next
    .SpecialCells(xlConstants, xlNumbers).Delete Shift:=xlUp
    On Error GoTo 0
  End With
End Sub
 
Upvote 0
If you explain what you want to achieve without showing code that does not work for you, what would your question be?
Peter has supplied you with code that you don't understand and therefor are unable to change.
 
Upvote 0
If you explain what you want to achieve without showing code that does not work for you, what would your question be?
Peter has supplied you with code that you don't understand and therefor are unable to change.
Not sure the point of your msg.

I also don't understand how computers work but I am using one at the moment.

I showed two sets of code above (yes Peter supplied) and was after help on how to link them without having keep writing almost the same thing for each column.

If my post annoyed you, don't waste your time responding.
 
Upvote 0
Is it possible to upload a sample file with expected results.
A small example would be like this

Original Data

1726035298460.png


After Macro (deleted the last from each column in this case as the letter X (9 from the left) is not in the corresponding row A

So if the letter 9th from the left from row 2 and below is not in row A in that column it needs to be deleted.

1726035325340.png
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,790
Members
451,589
Latest member
Harold14

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