Solved

Looping through Workbooks

Posted on 2011-09-02
6
343 Views
Last Modified: 2012-05-12
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.
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

Open in new window

0
Comment
Question by:mato01
  • 4
6 Comments
 
LVL 17

Expert Comment

by:Kent Dyer
ID: 36476998
Your If Then construct is funky!!

Sub ClearContents()
	Dim i As Long
	On Error Resume Next
	ActiveSheet.Unprotect
	For i = 6 To 44
		If InStr(LCase(ActiveSheet.Name), "wire") Then ' -- You don't need > 0 here..
			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

Open in new window


Looping through all sheets - http://www.mrexcel.com/archive/VBA/24967.html

HTH,

Kent
0
 

Author Comment

by:mato01
ID: 36477000
Yes, and It isn't working too.  That's my point.  But thanks for pointing that out.
0
 
LVL 42

Expert Comment

by:dlmille
ID: 36477034
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.

   
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

Open in new window


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
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 42

Expert Comment

by:dlmille
ID: 36477035
from line 24 should read:

   wks.Range("A6").select
   on error resume next
   wks.protect
   on error goto 0
next wks
0
 
LVL 42

Accepted Solution

by:
dlmille earned 125 total points
ID: 36477036
Apologies for the repeat posting....

Please use this correction:

 
Sub ClearContents()
Dim i As Long
Dim wks As Worksheet

    For Each wks In ActiveWorkbook.Worksheets
        
        wks.Activate
        
        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
        
        wks.Range("A6").Select
        On Error Resume Next
        wks.Protect
        On Error GoTo 0
        
    Next wks
       
        
End Sub

Open in new window


Dave
0
 
LVL 42

Expert Comment

by:dlmille
ID: 36479585
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
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question