VBA Macro Loop Through worksheet

LearnVBA83

Board Regular
Joined
Dec 1, 2016
Messages
109
Office Version
  1. 365
Platform
  1. Windows
Is there a way to write a macro that will loop through each worksheet in workbook and copy the name of the worksheet into that worksheet in cell A1 to A200?
 
For the message box I put at the end of the code right before the last End Sub. Does that sound right?

MsgBox "Complete"
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
There is a lot of brilliance on this board. Watching the posts and the answers is for sure a good thing. Keep asking questions for different topics and you will see all the different/creative ideas to handle situations. I just joined in Jan. Been around it for a while :). Just hanging on the coat tales of greatness around here.


This is where you would check for a value (in the if statement, don't add it it's zero...Hint: check the sum column for a value > 0 you just made it work) then it doesn't get added to the final result.

VBA Code:
For i = 1 To .UsedRange.Rows.Count
                    If InStr(1, ws.Range("C" & i).Value, "Expense", vbTextCompare) > 0 And ws.Range("B" & i).Value <> "" Then
                        Set fillRangeRow = ws.Range(ws.Cells(i, 1), ws.Cells(i, 15))
                        Set rng = mstTemp.Cells(lr, 1)
                        Set rng = rng.Resize(1, fillRangeRow.Columns.Count)
                        rng.Value = fillRangeRow.Value
    
                        lr = lr + 1
                    End If
                Next

Here is a good link on message boxes.

MessageBox


I'd also research error checking. Ie. file not found for the workbook.

Might want to clear the master sheet before you write to it.

Create a shape and assign the macro to it on your master tab.

Some ideas :)
 
Upvote 0
Great idea! That was going to be my last question on the naming. The Data will probably have a different title each week so would you copy and paste the file name in the code each month or would you create a folder called Macro and put the master in the folder and write the code to open and execute on any other file in that folder? So then all i'd have to do is drop the file in the macro folder open the master and run right? Is that possible or would you just add the data path in the code each week.
 
Upvote 0
Nope I would open a file dialog box. Browse for whatever file you are looking for and let it run. Keep the master file in your files or desktop.

So the shape you click, opens a file dialog picker, then runs the code.

make sense?
 
Upvote 0
Nope I would open a file dialog box. Browse for whatever file you are looking for and let it run. Keep the master file in your files or desktop.

So the shape you click, opens a file dialog picker, then runs the code.

make sense?

I'm picturing my master template:

Add code at the beginning to clear master template and open dialog box to look up the file and then run it. Does that sound right? I also changed your save from True to False at the bottom b/c i'd like for the original data set to go back to what it was before the macro b/c we use that spreadsheet for other stuff.
 
Upvote 0
Thanks :)

I am on purpose writing this top down so you can learn a bit caught that in your LearnVBA83. For production I would most likely break this out into smaller subs/functions and apply a lot more error checking. Let's get it running first.

Error was Activesheet on the filter. Also, I am changing where the master sheet gets built up.

Give me a bit.

sk
So what are you suggesting here when you say break into smaller subs/functions?
 
Upvote 0
What rules do you use (in your mind) to determine when to use string vs long vs integer vs double. I read where Double is for real numbers so why string or long and not double?
 
Upvote 0
All depends on your data. Double is needed for decimals. String is for text. Int and long are dependent on size of number. Variant can be anything but, don't get lazy. Use what is required. The smaller the better for performance.

Here is a reference.

Var Types
 
Upvote 0
So what are you suggesting here when you say break into smaller subs/functions?

I wrote everything in 1 sub so you could unpack it easier and digest it.

The idea "most of the time" is to use some smaller helper functions (func. returns a value, Subs execute ). VBA is pretty loose, you can do either with both but, best practice is to follow common coding standards for them.

I would break out certain sections of that code into smaller sections. Mostly for reuse and the readability.

Example instead of putting app... at the top and bottom of all the subs i would create utility subs. That way I can call 1 line of code instead of 3-5. I then can reuse that anywhere.



VBA Code:
Application.ScreenUpdating = False
Application.EnableEvents = False

?...

Application.ScreenUpdating = True
Application.EnableEvents = true


Public Sub ApplicationLock()
' locks the application for faster processing

    If applicationIsLocked Then Exit Function
    
    With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .StatusBar = False
        .EnableEvents = False
        iWindowState = .WindowState
    End With
    applicationIsLocked = True
End Function

Public Sub ApplicationUnLock()
' unlocks application for normal activity

    If applicationIsLocked Then
        With Application
            .Calculation = xlCalculationAutomatic
            .ScreenUpdating = True
            .StatusBar = True
            .EnableEvents = True
            .WindowState = iWindowState
        End With
    
        applicationIsLocked = False
    End If
End Function
 
Upvote 0
Hey, to keep this thread clean. It's probably better if you have other questions to start a personal conversation. Click under my name for that.
:)
 
Upvote 0

Forum statistics

Threads
1,217,366
Messages
6,136,128
Members
449,993
Latest member
Sphere2215

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