Macro clear contents & loop

Abvlecxe

Board Regular
Joined
Sep 10, 2015
Messages
53
Hello I need some help with the following macro which I am using to clear the contents of specific cells.
Basically the macro should select cells C9:F9 clear the contents and then loop down 7 rows to C16:F16, clear those cells and then keep on looping by 7 rows until it reaches the lastrow but I can’t seem to get it to work so any help would be appreciated, thanks in advance


Sub ClearDataInput()
Dim lastRow As Long
Dim Row As Long
Dim x As Long
Application.ScreenUpdating = False
Row = 9
For x = 9 To lastRow
With Sheet5
.Select
lastRow = .Range("c7").SpecialCells(xlCellTypeLastCell).Row
.Range("C" & Row & ": f" & Row).Select
Selection.ClearContents
Row = Row + 7

End With
Next x

Application.ScreenUpdating = True
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
How about

Code:
Dim lastRow as Long, cCells as Range
With Sheet5
     lastRow = .Range("c7").SpecialCells(xlCellTypeLastCell).Row

    Set cCells = .Range("C9:F9")

    For i = 9 to lastRow Step 7
        Set cCells = Application.Union(cCells, .Rows(i).Range("C1:F1")
    Next i
End With

cCells.ClearContents
 
Upvote 0
Thanks but that doesn't work. Have you set defined all the variables in your code?

It doesn't like:
For i = 9 To lastRow Step 7
Set cCells = Application.Union(cCells, .Rows(i).Range("C1:F1")



How about

Code:
Dim lastRow as Long, cCells as Range
With Sheet5
     lastRow = .Range("c7").SpecialCells(xlCellTypeLastCell).Row

    Set cCells = .Range("C9:F9")

    For i = 9 to lastRow Step 7
        Set cCells = Application.Union(cCells, .Rows(i).Range("C1:F1")
    Next i
End With

cCells.ClearContents
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,613
Members
452,661
Latest member
Nonhle

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