Sorry if this is an obvious question, I'm new to VBA but I learn by doing and I'm trying to use this code and it's not working for me...
Basically I have a workbook that has 175 sheets on it. Each sheet has data in column A (UPC codes) and I want to put the number 1 in column B next to every UPC.
I can get the macro to work on an individual worksheet, but I want it to cycle through all the sheets. This is the code I have:
Sub ReplaceOne()
Dim wsheet As Worksheet
Dim Cell As Range
For Each wsheet In ThisWorkbook.Worksheets
For Each Cell In Range("A:A")
If Cell.Value <> "" Then Cell.Offset(0, 1).Value = 1
Next
Next wsheet
End Sub
As I said, the actual action works fine on the active sheet, but it doesn't move on to the next sheet. I've tried it on a smaller data set and no issues.
I've already had to do this once for a slightly smaller data set and I just wrote into the code
ActiveSheet.Next.Select and then used a keyboard shortcut repeatedly, which worked (code below)
Sub ReplaceOne()
Dim wsheet As Worksheet
Dim Cell As Range
For Each Cell In Range("A:A")
If Cell.Value <> "" Then Cell.Offset(0, 1).Value = 1
Next
ActiveSheet.Next.Select
End Sub
What's the issue with the top code? Interestingly, when I added the ActiveSheet.Next.Select into the original code after the first Next statement, it ran through all the sheets. I did however get an error (91, Object variable or With block variable not set) at the end....
Thanks for any help you can give!
Basically I have a workbook that has 175 sheets on it. Each sheet has data in column A (UPC codes) and I want to put the number 1 in column B next to every UPC.
I can get the macro to work on an individual worksheet, but I want it to cycle through all the sheets. This is the code I have:
Sub ReplaceOne()
Dim wsheet As Worksheet
Dim Cell As Range
For Each wsheet In ThisWorkbook.Worksheets
For Each Cell In Range("A:A")
If Cell.Value <> "" Then Cell.Offset(0, 1).Value = 1
Next
Next wsheet
End Sub
As I said, the actual action works fine on the active sheet, but it doesn't move on to the next sheet. I've tried it on a smaller data set and no issues.
I've already had to do this once for a slightly smaller data set and I just wrote into the code
ActiveSheet.Next.Select and then used a keyboard shortcut repeatedly, which worked (code below)
Sub ReplaceOne()
Dim wsheet As Worksheet
Dim Cell As Range
For Each Cell In Range("A:A")
If Cell.Value <> "" Then Cell.Offset(0, 1).Value = 1
Next
ActiveSheet.Next.Select
End Sub
What's the issue with the top code? Interestingly, when I added the ActiveSheet.Next.Select into the original code after the first Next statement, it ran through all the sheets. I did however get an error (91, Object variable or With block variable not set) at the end....
Thanks for any help you can give!