How do I loop this code using Excel 2007 VBA?

SirSchiz

New Member
Joined
May 4, 2011
Messages
24
Hi All,

Thanks for reading. I have a silly question. For the life of me I can not figure this simple code out. How would I state it to loop through each worksheet in the active workbook?

This code Finds a blank cell in the Used Range (Always A1:A) then for each empty cell it simply insert a value of 1.

Any help is appreciated!

Code:
Sub FindABlank()

For Each D In ActiveSheet.UsedRange
If IsEmpty(D) Then
D.Value = "1"
End If
Next
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
You probably don't even need to loop.

Try:

Code:
    Columns("A:A").SpecialCells(xlCellTypeBlanks).Select
    Selection.Value = 1
 
Upvote 0
Hi,

try

Code:
Sub kTest()
    
    Dim i   As Long
    
    Application.ScreenUpdating = 0
    For i = 1 To Worksheets.Count
        With Worksheets(i).UsedRange
            On Error Resume Next
            .Columns(1).SpecialCells(4).Value = 1
        End With
    Next
    Application.ScreenUpdating = 1
    
End Sub

HTH
 
Upvote 0
Thanks for the reply. I've seen this before, but due to my ignorance with VBA I stumble on how to apply this?
 
Upvote 0
Code:
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
 ...

Next ws

So your code would be:
Code:
Sub FindABlank()

Dim ws As Worksheet
Dim d As Range

For Each ws In ActiveWorkbook.Worksheets
    For Each d In ws.UsedRange
        If IsEmpty(d) Then
            d.Value = "1"
        End If
    Next d
Next ws

End Sub
 
Upvote 0
Kris,

Perfect! Thanks! You just saved me a long day of tedious work. I hate to ask another question, but if you don't mine? How would I could I incorporate this into another Macro? I have a larger one I use to sort a different workbook, it creates a new one, and this new one is where I would run this. How can I call it to continue the process without having to run this new macro after the first completes?
 
Upvote 0
Scott..

Thanks, this also works very nicely! Now I'm just trying to get this macro to play nicely with another one I run before this one. Any ideas?
 
Upvote 0
Another question: What if the used range is already selected? Could I alter this like so?
Code:
 Columns("A:A").SpecialCells(xlCellTypeBlanks).Selection.Value = 1
 
Upvote 0
Hi,

something like..

Code:
Sub Put1OnEmptyCells(ByRef OtherWbk As Workbook)
    
    Dim i   As Long
    
    Application.ScreenUpdating = 0
    For i = 1 To OtherWbk.Worksheets.Count
        With OtherWbk.Worksheets(i).UsedRange
            On Error Resume Next
            .Columns(1).SpecialCells(4).Value = 1
        End With
    Next
    Application.ScreenUpdating = 1
    
End Sub

Sub YourAnotherMacro()

    'call the procedure
    
    Put1OnEmptyCells Workbooks("OtherWorkbookName.xls")

End Sub


Note: The other workbook must be opened before run the macro

HTH
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top