I am trying to write some code, that will search the first 30 columns and rows for the words Total
and Area
. I am looking to store the locations of these words in a variable and then use these variables to clear a range relative to them, this then loops across all worksheets.
I have tried to use a number to letter converter that I found online to store the column number, and I think this is where my problem is coming in.
Here is the code I found online:
Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function
and my code:
Private Sub Clear_Click()
Dim LastRowH As Integer
Dim ClearContent As Boolean
Dim ws As Worksheet
Dim testrange As Range
Dim Cell1 As Range
Dim Celln As Range
ClearContent = False
For Each ws In ActiveWorkbook.Worksheets
'FINDS RANGE
For i = 1 To 30
For j = 1 To 30
If ActiveWorkbook.Sheets(ws).Range(Col_Letter(CLng(i)) & j).Value = "Total" Then
Cell1 = ws.Range(Col_Letter(CLng(i + 1)) & j)
End If
If ActiveWorkbook.Sheets(ws).Range(Col_Letter(CLng(i)) & j).Value = "Area" Then
Celln = ws.Range(Col_Letter(CLng(i + 1)) & j - 1)
End If
Next
Next
'...<more code here>...
If ClearContent = True Then
'...<more code here>...
ws.Range(Cell1 & ":" & Celln).ClearContents
End If
Next ws
End Sub
When I run the code, I get the error message:
Run-time error '13': Type Mismatch
I have tried a couple of other methods but cannot get it to work.
Any help is appreciated, Thanks in advance :)
UPDATE I have tried replacing the for loops in the code to use the "Cells" function, as follows:
For i = 1 To 30
For j = 1 To 30
If Sheets(ws).Cells(j, i).Value = "Total" Then
Set Cell1 = ws.Cells(j - 1, i + 1)
End If
If Sheets(ws).Cells(j, i).Value = "Area" Then
Set Celln = ws.Cells(j, i + 1)
End If
Next
Next
But I am still receiving the Type Mismatch