VBA SelectedSheets not working properly

DanielCol

New Member
Joined
Aug 29, 2022
Messages
12
Office Version
  1. 365
  2. 2010
  3. 2007
Platform
  1. Windows
Hello,
Why is it that this function always return 1 when used as a Excel function ?
VBA Code:
Function NbFeuillesActives()
  NbFeuillesActives = ActiveWindow.SelectedSheets.Count
End Function
Capture d'écran_20221210_121710.png


Regards.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Selecting a sheet or adding to the selected sheets does not trigger calculation.
The formula won't be recalculated when the user selects sheets.

You could make it a volatile function. That wouldn't necessarily be accurate, because the recalculation has to wait until a cell's value is changed.
 
Upvote 0
Thanks for answering. Unless I miss something, even if I change a cell value, the formula value is still 1 ?
 
Upvote 0
Did you make the Function volatile as Mike stated?
 
Upvote 0
Yes, I did it. Same result... The funny thing if I execute th code line by line, the result is correct
 
Upvote 0
Please post the code as you currently have it
 
Upvote 0
Well, after mikerickson suggestion :

VBA Code:
Function NbFeuillesActives()
  Application.Volatile
  NbFeuillesActives = ActiveWindow.SelectedSheets.Count
End Function
 
Upvote 0
I'll test once I get back in, about a hour
 
Upvote 0
Code is working fine for me. A4 has the formula and A1 is the cell that I am changing. Is your calculation set to Automatic?

1670690382966.png
 
Upvote 0
Weird. Calculation set to automatic, changing any cell value, formula in E3 :
Capture d'écran_20221210_180128.png


Local problem ? Other users here have the same problem.
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,996
Members
452,542
Latest member
Bricklin

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