Consulting

Results 1 to 6 of 6

Thread: Solved: Design Advice & VBA Usage

  1. #1
    VBAX Regular macleanb's Avatar
    Joined
    Feb 2005
    Location
    Essex
    Posts
    23
    Location

    Unhappy Solved: Design Advice & VBA Usage

    Hi

    Long time no see - and as you might have guessed I am in trouble!

    I am creating a "P&L explain" workbook that presents a top level summary of P&L and then allows the user to drill down into each row. Each row is linked to a sepearate worksheet.

    I would dearly like to structure my code so that each subsiduary worksheet exposes a similar set of methods/properties which enable me to access/control them (like an interface).

    At present I have individually named subs like so:

    Factor1_Refresh
    Factor2_Refresh
    Factor3_Refresh/Factor3_Display...

    and so on. What I would like to do is more along the lines of:

    Worksheets("Factor1").Refresh
    Worksheets("Factor2").Refresh
    Worksheets("Factor3").Refresh

    Now the important part is this would then allow me to substitute "FactorX" with a variable, so that I could run the same functions/methods against an object that I reference at runtime.

    Anyone got any good ideas (or am I missing something obvious)


    Many, many thanks


    Ben
    Windows 2k, Excel 2002 SP3

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Ben

    Why not just pass the worksheets as parameters to subs?

  3. #3
    VBAX Regular macleanb's Avatar
    Joined
    Feb 2005
    Location
    Essex
    Posts
    23
    Location
    Hi Norrie

    thanks for the reply.


    All the subs need to do fifferent things depending on what they are explaining, so I just end up with a big case statement - which doesnt get me much further.

    It's not that I cant do the actual coding thats required, I just wanted it to be a bit more, well, object orientated.


    ttfn benm
    Windows 2k, Excel 2002 SP3

  4. #4
    VBAX Regular macleanb's Avatar
    Joined
    Feb 2005
    Location
    Essex
    Posts
    23
    Location
    Hi Guys

    just managed to figure out a way to do what I want - the problem I had was I was trying to do was the equivalent of this:

    Dim wsTmp as worksheet
    wsTmp = worksheets("FooBanana")
    wsTmp.MyGenericFunction()

    But of course the compiler knew that worksheets dont have a method called "FooBanana" so I get an error. SO the cure? should have been obvious:


    Dim wsTmp as object
    wsTmp = worksheets("FooBanana")
    wsTmp.MyGenericFunction()

    Now the compiler has no idea what methods are supported, and I am now well on my way....


    Thanks For looking....
    Windows 2k, Excel 2002 SP3

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Glad you worked it out and thanks for posting your solution. Nice job Ben.
    ttfn
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    VBAX Regular macleanb's Avatar
    Joined
    Feb 2005
    Location
    Essex
    Posts
    23
    Location
    My Actual Code below:

    [VBA]
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim HelperStr As String
    Dim CCYString As String
    Dim TargetWS As Object

    HelperStr = ActiveSheet.Cells(Target.row, [DrillDownHelper].Column)
    CCYString = ActiveSheet.Cells([DrillDownHelper].row, Target.Column)

    Set TargetWS = GetWSFromCName(HelperStr)

    If HelperStr <> "" Then

    If Len(CCYString) <> 3 Then CCYString = "All"
    TargetWS.Visible = True
    TargetWS.Activate
    TargetWS.RefreshMe CCYString

    End If


    Cancel = True
    End Sub
    [/VBA]
    Windows 2k, Excel 2002 SP3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •