PC Review


Reply
Thread Tools Rate Thread

Any way to call a worksheet method that may or may not exist?

 
 
Dick Watson
Guest
Posts: n/a
 
      13th Dec 2008
I'm adding a whole bunch of little formatting wedges in VBA on individual
sheets as necessary. I want to call then on workbook save. This works:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)

Sheet3.DoTheKludges
Sheet4.DoTheKludges
Sheet6.DoTheKludges
Sheet9.DoTheKludges
Sheet10.DoTheKludges

End Sub

But it's irredeemably ugly and unmaintainable and doesn't scale well as
sheets get added to or subtracted from this list.

So I was thinking of something along these lines:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)

Dim objWks As Worksheet

For Each objWks In Me.Worksheets

On Error Resume Next
objWks.DoTheKludges
On Error Goto 0

Next

End Sub

Of course, this doesn't work since, I'm guessing, the objWks.DoTheKludges
method call can't be resolved when the runtime compile occurs and that isn't
trapped by the On Error.

Any thoughts how to get around this?

 
Reply With Quote
 
 
 
 
Bob Phillips
Guest
Posts: n/a
 
      13th Dec 2008
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)

Dim objWks As Worksheet

For Each objWks In Me.Worksheets

Application.Run "'" & objWks.Codename & "'!DoTheKludges"
Next

End Sub


--
__________________________________
HTH

Bob

"Dick Watson" <(E-Mail Removed)> wrote in
message news:5FF0460F-C980-4BB0-A4A1-(E-Mail Removed)...
> I'm adding a whole bunch of little formatting wedges in VBA on individual
> sheets as necessary. I want to call then on workbook save. This works:
>
> Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
> Boolean)
>
> Sheet3.DoTheKludges
> Sheet4.DoTheKludges
> Sheet6.DoTheKludges
> Sheet9.DoTheKludges
> Sheet10.DoTheKludges
>
> End Sub
>
> But it's irredeemably ugly and unmaintainable and doesn't scale well as
> sheets get added to or subtracted from this list.
>
> So I was thinking of something along these lines:
>
> Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
> Boolean)
>
> Dim objWks As Worksheet
>
> For Each objWks In Me.Worksheets
>
> On Error Resume Next
> objWks.DoTheKludges
> On Error Goto 0
>
> Next
>
> End Sub
>
> Of course, this doesn't work since, I'm guessing, the objWks.DoTheKludges
> method call can't be resolved when the runtime compile occurs and that
> isn't trapped by the On Error.
>
> Any thoughts how to get around this?



 
Reply With Quote
 
Dick Watson
Guest
Posts: n/a
 
      14th Dec 2008
Cool. I'll try it!

"Bob Phillips" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
> Boolean)
>
> Dim objWks As Worksheet
>
> For Each objWks In Me.Worksheets
>
> Application.Run "'" & objWks.Codename & "'!DoTheKludges"
> Next
>
> End Sub



 
Reply With Quote
 
Dick Watson
Guest
Posts: n/a
 
      14th Dec 2008
The 'sheetname'!macroname form was No Joy.

But here's what does work:

On Error Resume Next
Application.Run objWks.CodeName & ".DoTheKludges"
On Error GoTo 0

Thanks again!

"Bob Phillips" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
> Boolean)
>
> Dim objWks As Worksheet
>
> For Each objWks In Me.Worksheets
>
> Application.Run "'" & objWks.Codename & "'!DoTheKludges"
> Next
>
> End Sub


 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      14th Dec 2008
Isn't that what I said?

--
__________________________________
HTH

Bob

"Dick Watson" <(E-Mail Removed)> wrote in
message news:639C6081-F3CD-4D3D-AEE2-(E-Mail Removed)...
> The 'sheetname'!macroname form was No Joy.
>
> But here's what does work:
>
> On Error Resume Next
> Application.Run objWks.CodeName & ".DoTheKludges"
> On Error GoTo 0
>
> Thanks again!
>
> "Bob Phillips" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
>> Boolean)
>>
>> Dim objWks As Worksheet
>>
>> For Each objWks In Me.Worksheets
>>
>> Application.Run "'" & objWks.Codename & "'!DoTheKludges"
>> Next
>>
>> End Sub

>



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      14th Dec 2008
.. vs !



Bob Phillips wrote:
>
> Isn't that what I said?
>
> --
> __________________________________
> HTH
>
> Bob
>
> "Dick Watson" <(E-Mail Removed)> wrote in
> message news:639C6081-F3CD-4D3D-AEE2-(E-Mail Removed)...
> > The 'sheetname'!macroname form was No Joy.
> >
> > But here's what does work:
> >
> > On Error Resume Next
> > Application.Run objWks.CodeName & ".DoTheKludges"
> > On Error GoTo 0
> >
> > Thanks again!
> >
> > "Bob Phillips" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >> Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
> >> Boolean)
> >>
> >> Dim objWks As Worksheet
> >>
> >> For Each objWks In Me.Worksheets
> >>
> >> Application.Run "'" & objWks.Codename & "'!DoTheKludges"
> >> Next
> >>
> >> End Sub

> >


--

Dave Peterson
 
Reply With Quote
 
Dick Watson
Guest
Posts: n/a
 
      15th Dec 2008
And lose the single ticks.

"Dave Peterson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>. vs !


 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      15th Dec 2008
I concede that the ! was a typo on my part, but I wouldn't the single ticks
mattered. Unnecessary, true, as you wanted the codename, but not a problem.

--
__________________________________
HTH

Bob

"Dick Watson" <(E-Mail Removed)> wrote in
message news:uU3cq%(E-Mail Removed)...
> And lose the single ticks.
>
> "Dave Peterson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>. vs !

>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy Worksheet to a new Workbook creating if it doesn't exist and add more Worksheets if it does exist pete.bastin@btopenworld.com Microsoft Excel Programming 4 18th Jun 2006 06:08 PM
newbie: How to call a method from a static method? deko Microsoft C# .NET 7 26th Feb 2006 08:41 AM
GetObject method not work after Call Shell Method =?Utf-8?B?YmVu?= Microsoft Excel Programming 8 21st Feb 2006 03:45 PM
Warning 1684 CA2214 : Microsoft.Usage : 'RandomShade..ctor(Int32, Int32, Int32, Int32, Int32)' contains a call chain that results in a call to a virtual method defined by the class. Review the following call stack for unintended consequences: steve bull Microsoft C# .NET 4 7th Jul 2005 05:54 PM
static method call vs. Singleton method call - performance Victor Jones Microsoft C# .NET 1 22nd Aug 2003 03:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:26 PM.