VBA If statement

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm working with Columns H & I in Range from rows 11 to 180.

How would I write this code below in lieu of writing out each line of its range:


VBA Code:
If Range("H11") = "Enter Tally" And Range("I11") = "" Then
Range("H11").ClearContents
End If

If Range("H12") = "Enter Tally" And Range("I12") = "" Then
Range("H12").ClearContents
End If

If Range("H13") = "Enter Tally" And Range("I13") = "" Then
Range("H13").ClearContents
End If

.
.
.

If Range("H180") = "Enter Tally" And Range("I180") = "" Then
Range("H180").ClearContents
End If

For example, how would I shorten its length of my proposed code in lieu of writing out each and every line until row 180?

Please let me know when you get a chance.

Thanks!
pinaceous
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this

PHP:
Public Sub Test()
Dim i As Long
For i = 11 To 180 Step 1
    If Range("H" & i).Value = "Enter Tally" And Range("I" & i).Value = "" Then
        Range("H" & i).ClearContents
    End If
Next i
End sub
 
Upvote 0
Solution
Try this

PHP:
Public Sub Test()
Dim i As Long
For i = 11 To 180 Step 1
    If Range("H" & i).Value = "Enter Tally" And Range("I" & i).Value = "" Then
        Range("H" & i).ClearContents
    End If
Next i
End sub
Many Thanks Phuoc!!!
 
Upvote 0
Try this:
There are other ways but to show you how to do it your way:
VBA Code:
Sub My_Script()
'Modified  6/21/2022  11:48:43 PM  EDT
Dim i As Long
Application.ScreenUpdating = False

    For i = 11 To 180
        If Cells(i, "H").Value = "Enter Tally" And Cells(i, "I").Value = "" Then Cells(i, "H").ClearContents
    Next
Application.ScreenUpdating =True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,443
Messages
6,124,890
Members
449,194
Latest member
JayEggleton

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