Incorrect number of worksheet counted?

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
424
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
This code counts only 4 Sheet rather than 50 sheets ??


VBA Code:
Sub vLookupAnotherWorkbook()

    Dim Src As Workbook
    Dim Des As Workbook
    Dim SASheet As Worksheet
    Dim PASheet As Worksheet
    Dim MBefore As Integer
    Dim MName As String
    Dim ColName As Object
    Dim WSArray, i
    ReDim WSArray(1 To Sheets.Count)
   
    
    Dim FileToOpen As Variant
   
    When I try to count the worksheets it seems to only count rather than 51 sheets?


   [CODE=vba]With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    FileToOpen = ("\\DC01\Company\PURCHASING\Forecasting\Brett Martin Template for Vlookup.xlsm")
    Workbooks.Open FileToOpen
    Set Src = Workbooks("Brett Martin Template for Vlookup.xlsm")
    Set Des = Workbooks("Brett Martin Forecast 2022.xlsm")
    Set SASheet = Worksheets("Sales Analysis")
    Set PASheet = Worksheets("Purchasing Analysis")

    MBefore = Format(DateAdd("m", -1, Date), "mm")
   
    MName = MonthName(MBefore)
    
    For i = 1 To Sheets.Count
    WSArray(i) = Des.Sheets(i).Name
    Next


    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
   
  Src.Close False

     End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Which workbook are you trying to count the sheets in & which part of the code is returning the wrong count?
 
Upvote 0
Hi

This workbook (Brett Martin Forecast 2022)

VBA Code:
    Dim WSArray, i
    ReDim WSArray(1 To Sheets.Count)
    For i = 1 To Sheets.Count
    WSArray(i) = Des.Sheets(i).Name
    Next
 
Upvote 0
In that case you need to use
VBA Code:
For i = 1 To Des.Sheets.Count
otherwise it's counting the sheet in the workbook you just opened.
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,936
Messages
6,122,340
Members
449,079
Latest member
rocketslinger

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