Loop with VBA

thecat23

New Member
Joined
Apr 6, 2019
Messages
19
Office Version
  1. 365
Hi all,

I am trying to repeat the code below a number of times until it reaches row 28. Every time, the row number increases 1, e.g. all 21 becomes 22, 23 and so on until reaches 28. I am not sure how to do it with a loop, what I have done is to copy and paste the code 8 times, and change the row values manually. But this make the code too long, is there a easier way to do this? perhaps with a loop?

Thanks in advance.

VBA Code:
If Range("AB21").Value <> "" Then
        Range("AW21").Value = 0
        Range("AX21").Value = Range("N10").Value
Else
       Range("AW21").ClearContents
       Range("AX21").ClearContents
End If
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi thecat23,

Is the value in N10 to be used in all cases where the if statement is true?

Doug
 
Upvote 0
Try:
VBA Code:
Sub Test()
    Application.ScreenUpdating = False
    Dim x As Long
    For x = 21 To 28
        If Range("AB" & x).value <> "" Then
            Range("AW" & x).Value = 0
            Range("AX" & x).Value = Range("N10").Value
        Else
            Range("AW" & x).ClearContents
            Range("AX" & x).ClearContents
        End If
    Next x
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Did you try the macro I suggested in Post #3?
 
Upvote 0
VBA Code:
Sub CheckUpdate()

    Dim rngCheck As Range
    Dim rng As Range
    
    Set rngCheck = Sheet1.Range("AB21:AB28")
    
    For Each rng In rngCheck
        If rng.Value <> "" Then
            rng.Offset(0, 21).Value = 0
            rng.Offset(0, 22).Value = Sheet1.Range("N10").Value
        Else
            rng.Offset(0, 21).ClearContents
            rng.Offset(0, 22).ClearContents
        End If
    Next rng

End Sub
 
Upvote 0
VBA Code:
Sub CheckUpdate()

    Dim rngCheck As Range
    Dim rng As Range
   
    Set rngCheck = Sheet1.Range("AB21:AB28")
   
    For Each rng In rngCheck
        If rng.Value <> "" Then
            rng.Offset(0, 21).Value = 0
            rng.Offset(0, 22).Value = Sheet1.Range("N10").Value
        Else
            rng.Offset(0, 21).ClearContents
            rng.Offset(0, 22).ClearContents
        End If
    Next rng

End Sub
Hi duggie33,

Thanks for your help, I have change the offset value and it does what exactly what I was trying to do.

Thumps up.
 
Upvote 0

Forum statistics

Threads
1,215,511
Messages
6,125,250
Members
449,218
Latest member
daynle

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