@Siddharth Rout LastColumn is variable ,so I am using cells. – Soma
14 mins ago
If LastColumn is a variable then here are two ways to achieve what you want
With sh
.Range(.Cells(2, LastColumn), .Cells(.Rows.Count, LastColumn)).ClearContents
End With
You can also use an alternate method.
Dim LastCol As String
With sh
LastCol = Split(.Cells(, LastColumn).Address, "$")(1)
'~~> The below is similar to
'~~> sh.Range("J2:J" & sh.Rows.count).ClearContents which is nothing but
'~~> sh.Range("J" & "2:" & "J" & sh.Rows.count).ClearContents
.Range(LastCol & "2:" & LastCol & .Rows.Count).ClearContents
End With
One important note. Fully qualify the objects (Ex: Cells, Rows etc.) like I have done above (Notice the DOT before the object). You may want to read about it in Why does Range work, but not Cells?