Personal Macro Not Looping in Different Workbooks - excel

My problem is looping through several worksheets in a workbook.
I need to save the macro in my personal workbook, but if I do this, it loops through the same worksheet until it reaches the sheet count.
If I move the code to the current workbook, it works beautifully. I have investigated writing a macro to copy the module from personal workbook to the current workbook, but for other users this module will simply be saved in another workbook that is only open in the background to run the module.
How do I get this to loop through all worksheets in another workbook, when the code is saved to a module in my personal workbook, or simply saved in another workbook that is open in the background?
Code below:
Sub WorksheetLoop()
' Loop Through Worksheets, Add Totals
Dim LastRow As Long
Dim ws As Worksheet
Dim starting_ws As Worksheet
Set starting_ws = ActiveSheet
' Loop Sum Formula Through All Worksheets
For Each ws In ThisWorkbook.Worksheets
ws.Activate
'Now my code, the do something, (I have simplified this part since this _
works, its the loop that's broken)
'Insert new blank rows
Rows("1:6").Select
Selection.Insert Shift:=xlDown
'Got to next worksheet, but it will not!
Next
End Sub

This code has a bit different logic, but try it if it works:
Sub WorksheetLoop()
' Loop Through Worksheets, Add Totals
Dim LastRow As Long
Dim ws As Worksheet
Dim starting_ws As Worksheet
Dim path As String
Dim WorkingFile As Workbook
' Select the file you will manipulate to surely be in the correct WB.
path = Application.GetOpenFilename(FileFilter:="Excel Files (*.*), *.*", Title:="Please select a file you want to modify")
Workbooks.Open path
Set WorkingFile = ActiveWorkbook
For Each ws In WorkingFile.Worksheets
ws.Rows("1:6").Insert Shift:=xlDown
'Rest of the code
Next
End Sub

Here is the working code, Now to compare it to what was originally posted code.
Thanks again Vinnie and Gordon!
Sub Step_3_Add_Subtotals()
' Loop Through Worksheets, Add Totals
Dim LastRow As Long
Dim ws As Worksheet
Dim starting_ws As Worksheet
Set starting_ws = ActiveSheet
' Loop Sum Formula Through All Worksheets
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
'Now my code, the do something
'Insert new blank rows
Rows("1:6").Select
Selection.Insert Shift:=xlDown
'Copy current headers, paste them in K3:Q3
Range("K7:Q7").Select
Selection.Copy
Range("K3").Select
ActiveSheet.Paste
'Got to next worksheet, and now it does!
Next
starting_ws.Activate
End Sub

Related

AutoFit all active rows / columns in a workbook

I want to create a macro autofit all rows and columns with values in them in across all worksheets in a workbook. This is what I have so far, I am stuck. Thank you in advance.
Sub AutoFitColumnsRows()
Dim ws As Worksheet
Dim starting_ws As Worksheet
Set starting_ws = ActiveSheet
For Each ws In ThisWorkbook.Worksheets
ws.Activate
Cells.EntireRow.AutoFit
Cells.EntireColumn.AutoFit
Next
starting_ws.Activate
End Sub
I am not sure why you need the other variables you have defined, but the basic loop looks like this:
For Each ws In ThisWorkbook.Worksheets
ws.Cells.EntireRow.AutoFit
ws.Cells.EntireColumn.AutoFit
Next

How do I loop through all sheets and rename based on the active sheet cell value?

I am trying to write a macro that will look through all sheets in the workbook, and if a sheet name contains "blank", to rename that sheet with the value in cell C1.
Here is what I have so far:
Sub Rename()
Dim ws As Worksheet
Dim sheetBlank As Worksheet
Set sheetBlank = ActiveWorkbook.ActiveSheet
Dim nameCell As Range
Set nameCell = ActiveSheet.Range("C1")
For Each ws In Sheets
If sheetBlank.Name Like "*blank*" Then
sheetBlank.Name = nameCell.Value
End If
Next ws
End Sub
Now, this does rename the first active sheet, but it is not making any changes to the rest of them. What am I doing wrong?
You're referring to the wrong worksheet:
Sub Rename()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name Like "*blank*" Then
ws.Name = ws.Range("C1").Value
End If
Next ws
End Sub
You set your objects outside the loop and never touch them again.
If you're going to use "ActiveSheet" you need to .Activate each sheet in order to work, but that's not a good approach since your iterator (ws) represents the sheet object.
Public Sub Rename()
Dim Ws As Worksheet
For Each Ws In Worksheets
If InStr(1, Ws.Name, "blank", vbTextCompare) > 0 Then _
Ws.Name = Ws.Range("C1").Value2
Next Ws
End Sub

Get the sheet name in a column when workbook contain about 50 worksheets

I have a workbook contain about 50 worksheets (sheet 1, sheet 2, sheet 3,........, sheet 50). I want to get the sheet name as a column infront of my data in each sheet. I used following code for that.
Sub tgr1()
Dim ws As Worksheet
Dim wsDest As Worksheet
Set wsDest = Sheets("Sheet1")
For Each ws In ActiveWorkbook.Sheets
If ws.Name <> wsDest.Name Then
ws.Range("A:A").Insert Shift:=xlToRight
'Selection.Insert Shift:=xlToRight
ws.Range("A12").FormulaR1C1 = _
"=IF(RC[1]>0,MID(CELL(""filename"",R[-11]C[1]),FIND(""]"",CELL(""filename"",R[-11]C[1]))+1,255),"""")"
ws.Range("A12").Copy
ws.Range("A13:A500").PasteSpecial xlPasteFormulas
ws.Range("A12:A500").Copy
ws.Range("A12:A500").PasteSpecial xlPasteValues
End If
ActiveWorkbook.Save
Next ws
But this code isn't working for all the sheets i have. it applies to random sheets. What should i do to make it apply for all the sheets.
1. Change ActiveWorkbook to ThisWorkbook
2. To get the worksheet name all you need is ws.Range("A12").Value = Ws.Name
3. No point saving the workbook each time the loop runs. Do it outside the loop.
Is this what you are trying?
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim wsDest As Worksheet
Set wsDest = Sheets("Sheet1")
For Each ws In ThisWorkbook.Sheets
If ws.Name <> wsDest.Name Then
ws.Columns(1).Insert Shift:=xlToRight
ws.Range("A12:A500").Value = ws.Name
End If
Next ws
ThisWorkbook.Save
DoEvents
End Sub

Copying values from a specified cell in Sheet1 into a specified range of the selected sheets

I would like to paste values from a range of cells in sheet1 into a specific range set in previously selected/activated sheets. Thus I want it to paste in let say only range B1 onwards but only for the sheets Sheet2, Sheet3 and NOT for Sheet4 since I have not selected it on my workbook.
Sub CopyFirstRow()
Dim Wb As Workbook
Dim Sht As Worksheet
Dim mySelectedSheets As Sheets
Wb.Sheets("Global").Range("B1", "Q39").Copy
Set mySelectedSheets = ActiveWindow.SelectedSheets
For Each Sht In mySelectedSheets
ActiveSheet.Paste
Sht.Range("B1").PasteSpecial xlPasteValues
Next
Application.CutCopyMode = False
End With
End With
End Sub
Please explain what I am doing wrong, as I am trying to understand more and more VBA and specifically the SET, FOR, WITH functions.
You dim'd the wb but never set the workbook.
You have two "End With"'s but have no starting "With"'s
.
You have Activesheet.paste but have not selected a sheet
Sub Button1_Click()
Dim Wb As Workbook
Dim Sht As Worksheet
Dim mySelectedSheets As Sheets
Set Wb = ThisWorkbook
Wb.Sheets("Global").Range("B1", "Q39").Copy
Set mySelectedSheets = ActiveWindow.SelectedSheets
For Each Sht In mySelectedSheets
Sht.Range("B1").PasteSpecial xlPasteValues
Next
Application.CutCopyMode = False
End Sub

Copy and Paste a set range in the next empty row_LOOP

I am new to macro and I am struggling with creating macro that will allow me to copy and paste the same range of cells from all sheets in worksheet and paste them in the first sheet in the next available cell. I know that is has to be done with the combination of loop and lastrow. Unfortunately, all my attempts fail
This is the macro that I would like to run through all sheet, but the sheets name is different
Sub Macro10()
'
' Macro10 Macro
'
'
Sheets("1449GW.WLWaterLevel.0sec").Select
Range("H1:Y2").Select
Selection.Copy
Sheets("Sheet1").Select
Range("A1").Select
ActiveSheet.Paste
End Sub
Try this. Two versions as not sure what you're asking.
If you are copying the same range from a single sheet to multiple sheets
Sub Macro10()
Dim ws As Worksheet, ws1 As Worksheet
Set ws1 = Worksheets("1449GW.WLWaterLevel.0sec")
For Each ws In Worksheets
If ws.Name <> ws1.Name Then
ws1.Range("H1:Y2").Copy ws.Range("A" & Rows.Count).End(xlUp)(2)
End If
Next ws
End Sub
If you are copying the same range from multiple sheets to a single sheet
Sub Macro10()
Dim ws As Worksheet, ws1 As Worksheet
Set ws1 = Worksheets("Sheet1")
For Each ws In Worksheets
If ws.Name <> ws1.Name Then
ws.Range("H1:Y2").Copy ws1.Range("A" & Rows.Count).End(xlUp)(2)
End If
Next ws
End Sub

Resources