Clear cell

Panoos64

Well-known Member
Joined
Mar 1, 2014
Messages
890
Hi everyone, i would like to write a vba so that to run through col. "A" and where find the text Grand Total should clear the contents in adjacent cell in col. "B" as my below extract



Original Data

A B
Grand Total Debtor Name



Expected result


A B
Grand Total
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Code:
Sub test2()
Dim Lastrow As Integer, Cnt As Integer
With Sheets("Sheet1")
    Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
For Cnt = 1 To Lastrow
If Sheets("Sheet1").Range("A" & Cnt).Value = "Grand Total" Then
Sheets("Sheet1").Range("B" & Cnt).Value = vbNullString
End If
Next Cnt
End Sub
HTH. Dave
 
Upvote 0
Hi Nd, I am sorry, but i have to say that it doesn't work and no error message appear. If you would like to adjust it, i am kindly require to arrange it through "Active Sheet" and not for a specific "Sheet1". However thanks for your support and your time spent for my project. Have a nice day
 
Upvote 0
Give this macro a try...
Code:
Sub ClearNextToGrandTotal()
  With Columns("A")
    .Replace "Grand Total", "#N/A", xlWhole, , False, , False, False
    With Columns("A").SpecialCells(xlConstants, xlErrors)
      .Offset(, 1).ClearContents
      .Value = "Grand Total"
    End With
  End With
End Sub
 
Upvote 0
Or this one:
Code:
Sub ClearBGT()
    Dim LastRow As Long, Addr1 As String, Addr2 As String
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    Addr1 = "A1:A" & LastRow
    Addr2 = "B1:B" & LastRow
    Range(Addr2) = Evaluate("IF(" & Addr1 & "=""Grand Total"",""""," & Addr2 & ")")
End Sub
 
Upvote 0
@ Rick Rothstein:

Just for fun: 10,000 rows, 2,500 "Grand Total", your code 0.125 seconds, my code 0.0156 seconds.
 
Upvote 0
Hi Tetra, the code works and it clear the data to adjacent cell of the Grand Total. The problem which occur is where in Col. "A" is blank, it appears in adjacent cells in col "B" 0 (zero). However i would like to thank you for your support. Have a nice day!
 
Upvote 0
Thank you for spotting this -- acknowledged and fixed:
Code:
Sub ClearBGT()
    Dim LastRow As Long, Addr1 As String, Addr2 As String
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    Addr1 = "A1:A" & LastRow
    Addr2 = "B1:B" & LastRow
    Range(Addr2) = Evaluate("IF((" & Addr1 & "=""Grand Total"")+(" & Addr2 & "=""""),""""," & Addr2 & ")")
End Sub
 
Upvote 0
I express my apologies Nd. It works perfect your code. I was testing the code with wrong data. Many thanks for your support and i am apologizing once again. Have a great day!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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