Passing worksheet variable to a sub

Gronwold

New Member
Joined
Jul 23, 2015
Messages
2
Hi all,
I would like to pass a worksheet variable to a sub as follows:


Code:
sub InsertData()
Dim wsDetails2 As Worksheet

 CopySheetsIntoVariable "Details (Routine 2)", wsDetails2

MsgBox wsDetails2.Cells(1, 3)

end sub


Code:
Sub CopySheetsIntoVariable(sName As String, ByRef ws As Worksheet)
    
    Dim wbTemp As Workbook
    Dim sSaveLocation As String
    
    'This routine extracts the file save location from the registry
    sSaveLocation = GetSetting("SaveLocation")
    
    'This copies the closed workbooks into the variable
    If Dir(sSaveLocation & sName & ".xlsx", vbDirectory) = "" Then
        MsgBox "Error - Cannot find the file '" & sName & ".xlsx'.  Exiting.", , "File not found"
        Else
        Set wbTemp = Workbooks.Open(sSaveLocation & sName & ".xlsx")
        Set ws = wbTemp.Sheets("Data")
        wbTemp.Close
        End If
        
End Sub

However, all I get is an 'Automation Error' when I get to the line 'MsgBox wsStudentDetails2.Cells(1, 3)'
Would really appreciate it if anyone could suggest why!
Many thanks!1947
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Your sub closes the workbook...

Just remove wbtemp.close

I think you're misunderstanding how object variables work, nothing is copied anywhere, you are just passing a pointer around to the same object
 
Last edited:
Upvote 0
Thank you Kyle123 - no I hadn't realised that - I thought that I had created two variables and copied one to the other, allowing me to close the first.
OK - I closed the workbooks as they open fully on the screen, which I don't want. I could use screenupdating=false, but would prefer either:
copying the contents of the worksheet into a variable so I can close the workbook opened, or
opening the worksheets in memory only, so they are not visible in excel. How could I go about that?
Cheers!
2011
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,078
Latest member
skydd

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