mato01
asked on
Looping through Workbooks
I having slight problems with the below code.
1. It should go from Row 6 to 44, but its only working thru row 20/
2. I'm trying to loop through every worksheet in the workbook that has the name wire in the name.
1. It should go from Row 6 to 44, but its only working thru row 20/
2. I'm trying to loop through every worksheet in the workbook that has the name wire in the name.
Sub ClearContents()
Dim i As Long
On Error Resume Next
ActiveSheet.Unprotect
For i = 6 To 44
If InStr(LCase(ActiveSheet.Name), "wire") > 0 Then
Range("B" & i).Select
If Range("B" & i).Value = " " Then
ActiveSheet.Range("i" & i & ":j" & i & ":k" & i).Cells.SpecialCells(xlTextValues).ClearContents
End If
End If
Next i
Range("A6").Select
ActiveSheet.Protect
Exit Sub
End Sub
ASKER
Yes, and It isn't working too. That's my point. But thanks for pointing that out.
Try this and look at your debug immediate window after it runs. It will tell you which rows were converted, which were skipped. Also, there's no need to reference I6:j6:K6 - I6:K6 is the same range, so I modified your range selection. There's also no need to select any cells.
On the surface, I'm not sure why its not getting past row 20, but let's see if it processes every row. If it does, then let's take a look at the data in a row that was skipped - was it a formula? did it have "wire" in it? etc...
Dave
Sub ClearContents()
Dim i As Long
Dim wks As Worksheet
For Each wks In ActiveWorkbook.Worksheets
On Error Resume Next
wks.Unprotect
On Error GoTo 0
For i = 6 To 44
If InStr(LCase(wks.Name), "wire") > 0 Then
If Range("B" & i).Value = " " Then
wks.Range("i" & i & ":k" & i).Cells.SpecialCells(xlTextValues).ClearContents
Debug.Print wks.Name & ":Cleared i" & i & ":k" & i
End If
Else
Debug.Print wks.Name & ":Skipping i" & i & "K" & i
End If
Next i
Range("A6").Select
wks.Protect
Next wks
End Sub
On the surface, I'm not sure why its not getting past row 20, but let's see if it processes every row. If it does, then let's take a look at the data in a row that was skipped - was it a formula? did it have "wire" in it? etc...
Dave
from line 24 should read:
wks.Range("A6").select
on error resume next
wks.protect
on error goto 0
next wks
wks.Range("A6").select
on error resume next
wks.protect
on error goto 0
next wks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I note youre debugging beforesave issues on another post and using this code. Glad its working.
Just one comment - for completeness, as we're activating wks, the prefixes aren't needed, but they are a good practice. So, to my chagrin, I must point out one other "miss" on my part, lol
Like 16 should read
if wks.Range("B" & i).value = " " then
As a good practice the wks. reference will ensure the Range property is pointing at the right sheet (even though in this example, it is the activesheet).
Dave
Just one comment - for completeness, as we're activating wks, the prefixes aren't needed, but they are a good practice. So, to my chagrin, I must point out one other "miss" on my part, lol
Like 16 should read
if wks.Range("B" & i).value = " " then
As a good practice the wks. reference will ensure the Range property is pointing at the right sheet (even though in this example, it is the activesheet).
Dave
Open in new window
Looping through all sheets - http://www.mrexcel.com/archive/VBA/24967.html
HTH,
Kent