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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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,213,551
Messages
6,114,273
Members
448,559
Latest member
MrPJ_Harper

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