Increment value on multiple visible worksheets

Krle

New Member
Joined
Apr 7, 2021
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
I do not have much experience with macros and I am trying to create one to add a increment value to a cell only in visible sheets.
The macro below adds values on all sheets. How to change to add value only to visible sheets?

Sub Test()

Dim wb As Workbook, sh As Worksheet
Dim ws As Long
Dim mynum As Long
mynum = -1

For Each sh In ActiveWorkbook.Sheets
For ws = 1 To Worksheets.count
With Worksheets(ws).Range("Q1")
If sh.Visible Then
.Value = mynum - 1 + ws
End If
End With
Next ws
Next sh
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try:

VBA Code:
Sub Test()

Dim wb As Workbook, sh As Worksheet
Dim ws As Long
Dim mynum As Long
mynum = -1

For Each sh In ActiveWorkbook.Sheets
For ws = 1 To Worksheets.count
If sh.Visible = -1 Then
With Worksheets(ws).Range("Q1")
.Value = mynum - 1 + ws
End With
End If
Next ws
Next sh
End Sub
 
Upvote 0
Thanks, but no change, loop does not add value only to the visible sheets!
 
Upvote 0
This is working, got rid of sh.

note sheet1 won't change -1-1+1 =-1

VBA Code:
Sub Test()

Dim wb As Workbook
Dim ws As Long
Dim mynum As Long
mynum = -1

For ws = 1 To Worksheets.Count
If Worksheets(ws).Visible = -1 Then
With Worksheets(ws).Range("Q1")
.Value = mynum - 1 + ws
End With
End If
Next ws
End Sub
 
Upvote 0
This is ok, but there is another problem. It does not add value to hidden sheets, but still counts hidden tabs and increment value is not correct!
Can you exclude hidden sheets and add sequential values only for visible tabs?
 
Upvote 0
So first sheet = 1, second = 2 etc?


VBA Code:
Sub Test()

Dim wb As Workbook
Dim ws As Long
Dim mynum As Long
mynum = 1

For ws = 1 To Worksheets.Count
If Worksheets(ws).Visible = -1 Then
With Worksheets(ws).Range("Q1")
.Value = mynum
mynum = mynum + 1
End With
End If
Next ws
End Sub
 
Upvote 0
So first sheet = 1, second = 2 etc?


VBA Code:
Sub Test()

Dim wb As Workbook
Dim ws As Long
Dim mynum As Long
mynum = 1

For ws = 1 To Worksheets.Count
If Worksheets(ws).Visible = -1 Then
With Worksheets(ws).Range("Q1")
.Value = mynum
mynum = mynum + 1
End With
End If
Next ws
End Sub
Yes I need first visible Sheet value 1, second visible 2 etc.
The last code add same number for all visible sheets!
 
Upvote 0
I try step into code, but can't find what is wrong
 
Upvote 0

Forum statistics

Threads
1,214,559
Messages
6,120,203
Members
448,951
Latest member
jennlynn

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