+ Reply to Thread
Results 1 to 6 of 6

count rows in closed workbook without opening it

Hybrid View

  1. #1
    Registered User
    Join Date
    05-15-2016
    Location
    Sydney
    MS-Off Ver
    excel 2013
    Posts
    21

    count rows in closed workbook without opening it

    Hi

    i have some performance issues with my macros with too much opening and closing of a number of workbooks.

    i am thinking of an alternative to copying data from an entire sheet in a workbook without opening it. Hopefully that might fasten things up.

    The code below is returning 1 for the variable mycount which is not right. it should be 300.. any ideas? or any suggestions? Thanks guys

    Sub chekc()
    
    Dim mycount As Long
    mycount = Application.WorksheetFunction.CountA("'/Users/JohnSmith/Desktop/[myWB.xlsx]Sheet1'!A:A")
    Debug.Print mycount
    
    
        strPath = "/Users/JohnSmith/Desktop/"
        strFile = "myWB.xls"
        strSheet = "Sheet1"
        strRng = Range("A1:A" & mycount)
        
        strRef = "'" & strPath & "[" & strFile & "]" & strSheet & "'!" & strRng
        
        Result = ExecuteExcel4Macro(strRef)
    
    End Sub

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: count rows in closed workbook without opening it

    Hi,

    It is not possible to use WorksheetFunction.CountA with a range in a closed workbook. Currently it is merely counting the string you pass to it, which is why it returns 1.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Registered User
    Join Date
    05-15-2016
    Location
    Sydney
    MS-Off Ver
    excel 2013
    Posts
    21

    Re: count rows in closed workbook without opening it

    Quote Originally Posted by xlnitwit View Post
    Hi,

    It is not possible to use WorksheetFunction.CountA with a range in a closed workbook. Currently it is merely counting the string you pass to it, which is why it returns 1.
    ok thanks for that. could your recommend a faster way to copy data from worksheets into arrays? Someone recommended ADO...but i have not use that before...All i need to do is read data in 8 worksheets to arrays..

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: count rows in closed workbook without opening it

    I doubt that opening/closing is the issue. It is the writing. I am not sure why you say copy an entire sheet. Once open, you can easily get the sheet's UsedRange.

    Getting value(s) like the Excel4 method is not copy. You can get the values in other ways like indirect referencing or ADO. Both of which give you values.

    For now, let's explore the copy issue a bit. Are you using the Application options to speed things up? e.g.
    'http://vbaexpress.com/kb/getarticle.php?kb_id=1035
    Public glb_origCalculationMode As Integer
    
    Sub SpeedOn(Optional StatusBarMsg As String = "Running macro...")
      glb_origCalculationMode = Application.Calculation
      With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
        .Cursor = xlWait
        .StatusBar = StatusBarMsg
        .EnableCancelKey = xlErrorHandler
      End With
    End Sub
    
    Sub SpeedOff()
      With Application
        .Calculation = glb_origCalculationMode
        .ScreenUpdating = True
        .EnableEvents = True
        .DisplayAlerts = True
        .CalculateBeforeSave = True
        .Cursor = xlDefault
        .StatusBar = False
        .EnableCancelKey = xlInterrupt
      End With
    End Sub
    
    Sub Yours()
        On Error GoTo EndSub
        SpeedOn
         
    ' your stuff here
         
    EndSub:
        SpeedOff
    End Sub

  5. #5
    Registered User
    Join Date
    05-15-2016
    Location
    Sydney
    MS-Off Ver
    excel 2013
    Posts
    21

    Re: count rows in closed workbook without opening it

    Quote Originally Posted by Kenneth Hobson View Post
    I doubt that opening/closing is the issue. It is the writing. I am not sure why you say copy an entire sheet. Once open, you can easily get the sheet's UsedRange.

    Getting value(s) like the Excel4 method is not copy. You can get the values in other ways like indirect referencing or ADO. Both of which give you values.

    For now, let's explore the copy issue a bit. Are you using the Application options to speed things up? e.g.
    'http://vbaexpress.com/kb/getarticle.php?kb_id=1035
    Public glb_origCalculationMode As Integer
    
    Sub SpeedOn(Optional StatusBarMsg As String = "Running macro...")
      glb_origCalculationMode = Application.Calculation
      With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
        .Cursor = xlWait
        .StatusBar = StatusBarMsg
        .EnableCancelKey = xlErrorHandler
      End With
    End Sub
    
    Sub SpeedOff()
      With Application
        .Calculation = glb_origCalculationMode
        .ScreenUpdating = True
        .EnableEvents = True
        .DisplayAlerts = True
        .CalculateBeforeSave = True
        .Cursor = xlDefault
        .StatusBar = False
        .EnableCancelKey = xlInterrupt
      End With
    End Sub
    
    Sub Yours()
        On Error GoTo EndSub
        SpeedOn
         
    ' your stuff here
         
    EndSub:
        SpeedOff
    End Sub
    Hi . yes i already have some optimisation code to do all that...using vba i copy data from various sheets into arrays, do the necessary calculations and in the end have one write to the sheet. so i have about 8 reads and only one write..but data i am reading from the workbooks is quite big.
    Last edited by twozedz; 09-01-2016 at 08:57 AM.

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: count rows in closed workbook without opening it

    Are the worksheets all in one workbook or several?

    ADO brings its own set of issues, particularly if your data is not type-consistent within columns. I would recommend that you provide your actual code here so that it may be reviewed in toto rather than attempting to suggest generic improvements.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Sum and Count Data from Closed Workbook
    By Lewster in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 05-05-2016, 03:01 PM
  2. Import a worksheet from a closed workbook without opening
    By aprilapple04 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-23-2013, 12:02 AM
  3. [SOLVED] Import A specified Range From Closed Workbook (without opening and closing)
    By webboj in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-18-2013, 05:47 AM
  4. Opening, refreshing, saving, and closing a closed workbook
    By hootiebsc in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-18-2013, 05:22 PM
  5. [SOLVED] Copying specific rows from an open workbook into the next row of a closed workbook.
    By Deimola in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-05-2012, 12:59 PM
  6. Using SUMPRODUCT to count values in closed workbook
    By Lifeseeker in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-04-2012, 02:34 PM
  7. Replies: 18
    Last Post: 12-13-2011, 10:15 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1