vba - clear contents of cell if adjacent cell contains part of text

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,501
Office Version
  1. 2016
Platform
  1. Windows
Hello Friends,

I need a vba code that would clear contents of cell in a range B1:B4 if adjacent cell Column A contains text Total

Pls see below table
Book2
AB
1123401-Jun
2234501-Jun
3345601-Jun
4Total Set(s)01-Jun
Sheet1


any help would be appreciated

regards,

humayun
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
How about
VBA Code:
Sub hrayani()
   With ActiveSheet
      .Range("A1:B1").AutoFilter 1, "*Total*"
      .AutoFilter.Range.Offset(1).Columns(2).Value = ""
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub hrayani()
   With ActiveSheet
      .Range("A1:B1").AutoFilter 1, "*Total*"
      .AutoFilter.Range.Offset(1).Columns(2).Value = ""
      .AutoFilterMode = False
   End With
End Sub
Hi Fluff,

Sorry for coming back late on this.... Can we just restrict the range like A1:B4
There is some other stuff just under the range
 
Upvote 0
Does this work the way you want...
VBA Code:
Sub ClearTotalValues()
  With Range("A1:A4")
    .Offset(, 1).Value = Evaluate(Replace("IF(isnumber(search(""total"",@)),"""",Offset(@,0,1))", "@", .Address))
  End With
End Sub
 
Upvote 0
Does this work the way you want...
VBA Code:
Sub ClearTotalValues()
  With Range("A1:A4")
    .Offset(, 1).Value = Evaluate(Replace("IF(isnumber(search(""total"",@)),"""",Offset(@,0,1))", "@", .Address))
  End With
End Sub
Hi Rick, Thanks for the reply

Yes it does on the first click (clear the contents)
but after the second click it inserts 00-Jan in there coz the cells are already empty after the first click

Could you also please look at the below code
only this part If Cl.Value = "Total" Then needs to be amended to make it work. It does if the cell only has Total in there but it does not work if there is Total Sets or something like that...

I tried with If Cl.Value = "*Total*" Then (putting the Asterisk sign but to no avail

VBA Code:
Sub DelStates()

    Dim Cl As Range
    
    For Each Cl In Range("A1:B28")
        If Cl.Value = "Total" Then
            Cl.Offset(0, 1).ClearContents
        End If
    Next Cl
    
End Sub
 
Upvote 0
How about this macro then...
Excel Formula:
Sub ClearTotalValues()
  With Range("A1:A4")
    .Offset(, 1).Value = Evaluate(Replace("IF(@="""","""",IF(ISNUMBER(SEARCH(""total"",@)),"""",OFFSET(@,0,1)))", "@", .Address))
  End With
End Sub
 
Upvote 0
Solution
How about this macro then...
Excel Formula:
Sub ClearTotalValues()
  With Range("A1:A4")
    .Offset(, 1).Value = Evaluate(Replace("IF(@="""","""",IF(ISNUMBER(SEARCH(""total"",@)),"""",OFFSET(@,0,1)))", "@", .Address))
  End With
End Sub
Done... Thanks very much Rick

But also tell me how to amend this part [If Cl.Value = "Total" Then] of the below code to work
I am still at learning stage


VBA Code:
Sub DelStates()

    Dim Cl As Range
    
    For Each Cl In Range("A1:A28")
        If Cl.Value = "*Total*" Then
            Cl.Offset(0, 1).ClearContents
        End If
    Next Cl
    
End Sub
 
Upvote 0
Assuming you know the word "Total" will always appear in proper case (upper case T, rest of the letters in lower case), you can use the Like operator to do your wildcard test...

If Cl.Value Like "*Total*" Then

If you cannot be sure of the letter casing, then use this instead...

If LCase(Cl.Value) Like "*total*" Then
 
Upvote 0
Assuming you know the word "Total" will always appear in proper case (upper case T, rest of the letters in lower case), you can use the Like operator to do your wildcard test...

If Cl.Value Like "*Total*" Then

If you cannot be sure of the letter casing, then use this instead...

If LCase(Cl.Value) Like "*total*" Then
Works like a charm :)

Many Thanks

Regards,
Humayun
 
Upvote 0
Assuming you know the word "Total" will always appear in proper case (upper case T, rest of the letters in lower case), you can use the Like operator to do your wildcard test...

If Cl.Value Like "*Total*" Then

If you cannot be sure of the letter casing, then use this instead...

If LCase(Cl.Value) Like "*total*" Then
Just one last thing

what if does not contain
can we using anything like Not Like "*Total*"
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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