worksheet name versus reference

cahinton

New Member
Joined
Dec 18, 2014
Messages
6
I'm having trouble with assigning a worksheet reference, then passing that to a sub. In the sample below I use two different ways to identify the worksheet that I want to format. I can't figure out the difference between passing the reference to the worksheet versus its name.
Thanks in advance!

Code:
[INDENT]Sub fmtDate_Sputter1(ws As Worksheet)
'
    ws.Range("C:C").NumberFormat = "yyyy-mmm-dd, hh:mm:ss"
End Sub


Sub fmtDate_Sputter2(wsname As String)
'
    Worksheets(wsname).Range("C:C").NumberFormat = "yyyy-mmm-dd, hh:mm:ss"
End Sub


Sub Foo23()
' test driver for fmtDate_Sputter()
    Dim wsCycles As Worksheet
    strCycleSheetName = "cycle"
    Set wsCycles = Worksheets(strCycleSheetName) 
    'Set wsCycles = Sheets(strCycleSheetName)
    fmtDate_Sputter1 (wsCycles)    ' err 438: no support method / property
    fmtDate_Sputter1 (Worksheets(strCycleSheetName)) ' err 438: no support method / property
    fmtDate_Sputter2 (strCycleSheetName) ' works
End Sub
[/INDENT]
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try
Code:
    fmtDate_Sputter1 wsCycles
    fmtDate_Sputter1 Worksheets(strCycleSheetName)
 
Upvote 0
Thanks for the quick help! I found I needed to retain the parentheses when I was passing the string in a variable, but not as a literal.
Code:
Sub Foo23()
' test driver for fmtDate_Sputter()
    Dim wsCycles As Worksheet
    strCycleSheetName = "cycle"
    Set wsCycles = Worksheets(strCycleSheetName) ' err 438: no support method / property
    'Set wsCycles = Sheets(strCycleSheetName)
    fmtDate_Sputter1 wsCycles
    fmtDate_Sputter1 Worksheets(strCycleSheetName)
    fmtDate_Sputter2 (strCycleSheetName)
    fmtDate_Sputter2 "Cycle"
End Sub
 
Upvote 0
Thanks for the quick help! I found I needed to retain the parentheses when I was passing the string in a variable, but not as a literal.

That's only because you didn't declare your variable so you were attempting to pass a Variant to a routine that requires a String. Adding the parentheses allows evaluation of the variable to a string.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,331
Members
452,636
Latest member
laura12345

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