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

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.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,468
Office Version
  1. 365
Platform
  1. Windows
Try
Code:
    fmtDate_Sputter1 wsCycles
    fmtDate_Sputter1 Worksheets(strCycleSheetName)
 

cahinton

New Member
Joined
Dec 18, 2014
Messages
6
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
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,696
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,373
Messages
5,635,884
Members
416,886
Latest member
coreyalaurence37

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
Top