PDA

View Full Version : [SOLVED:] VBA Clear contents help



rhysm144
11-18-2016, 12:05 PM
I am trying to create a macro whereby if U5:U6 is empty, I want to clear the contents of E5:E6, then loop to the next worksheet until end. This is what I have so far:


Sub ClearDub()
Dim wSheet As Worksheet
For Each wSheet In Worksheets
If wSheet.Range("U5:U6") = "" Then
Range(“E5:E6”).ClearContents
End If
Next wSheet
End Sub

JKwan
11-18-2016, 02:27 PM
give this a go

Sub ClearDub()
Dim wSheet As Worksheet

For Each wSheet In Worksheets
With wSheet
If (.Range("U5") = "") And (.Range("U6") = "") Then
.Range("E5:E6").ClearContents
End If
End With
Next wSheet
End Sub

Leith Ross
11-18-2016, 04:48 PM
Hello rhysm144,

If the cells in Range("E5:E6") are empty then there is no need to clear them.

To clear all formatting from the cells then use this code...


Sub ClearDub()


Dim wSheet As Worksheet


For Each wSheet In ThisWorkbook.Worksheets
If wSheet.Range("U5:U6") = "" Then
wSheet.Range(“E5:E6”).ClearAll
End If
Next wSheet


End Sub

rhysm144
11-19-2016, 09:08 AM
All working. Thank you both.