Diving_Dan
Board Regular
- Joined
- Oct 20, 2019
- Messages
- 161
Hi all,
I'm having an issue trying to loop a macro through all worksheets in a workbook. The code worked if I run the macro on an individual sheet but when I added in the loop it only takes the data from the active sheet. Below is some of my code, I've removed quite a bit of it for copying values as that isn't needed.
I'm guessing the problem is going to be where I set "sh" as the activesheet. I hoped that because I started the loop before i set the activesheet I wouldn't have a problem but I am. I run the code and I only get data transferred from the activesheet that I'm on at the time I run the macro.
Any help is appreciated as always.
I'm having an issue trying to loop a macro through all worksheets in a workbook. The code worked if I run the macro on an individual sheet but when I added in the loop it only takes the data from the active sheet. Below is some of my code, I've removed quite a bit of it for copying values as that isn't needed.
I'm guessing the problem is going to be where I set "sh" as the activesheet. I hoped that because I started the loop before i set the activesheet I wouldn't have a problem but I am. I run the code and I only get data transferred from the activesheet that I'm on at the time I run the macro.
Any help is appreciated as always.
Code:
Dim sDate As Date
Dim sh As Worksheet 'original sheet
Dim wb As Workbook 'destination
Dim f As Range
For Each Worksheet In Worksheets
Set sh = ThisWorkbook.ActiveSheet 'original book original sheet
sDate = sh.Range("C3").Value
Set wb = Workbooks.Open("C\Master.xlsx")
wb.Sheets("Data").Unprotect Password:="abcde"
Set f = wb.Sheets("Data").Range("A:A").Find(sDate, , xlFormulas, xlWhole)
If Not f Is Nothing Then
f.Offset(, 1).Value = sh.Range("C6").Value
wb.Sheets("Data").Protect Password:="abcde"
wb.Close True
Else
MsgBox "Date does not exist"
wb.Close False
End If
Next
Application.ScreenUpdating = True
MsgBox "Transfer to master sheet complete"