Results 1 to 7 of 7

Thread: Resolved: Pulling VBA Func Result into Cell (Excel)

  1. #1

    Thread Starter
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Resolved Resolved: Pulling VBA Func Result into Cell (Excel)

    Ok, hopefully this will be clear.
    I have an Excel worbook that has a number of wroksheets, each of which do a number of rather complex calculations. A lot of these calculations are based upon rates. Fortunatly these rates are found in a database, and I've added a macro sub that, when run will log into the database and run a stored procedure that will return the rate values based on a date range found on sheet 1. With me so far?

    Ok, so now, I have all these rates on a new sheet in the workbook. Because these rates can change over time, there's some calculations that go on in the worksheet to prorate the rates and calc a new "blanded" rate.

    Still with me?

    So what I'd like to do now, is to pull the rates into the appropriate places on the individual sheets. If I could, I would simply goto the destination cell and punch in =RateSheet!A23. But that won't work, since if a new rate is added, the rate will no longer be in cell A23, but could be in A24, or A30 even. So that's no good.

    I haven't lost you yet have I?

    So, I worte a VBA function, that given a name, looks through the list on that sheet, find the named rate and returns the value. Sounds simple. Except that I cannot for the life of me figure out how to call my VBA Func from the cell so that the value shows up. Know what I mean? Does any one know if it's possible? I tried as VLookupTable but that didn't work out too well.



    Tg
    Last edited by techgnome; Dec 10th, 2004 at 11:22 AM. Reason: Resolved
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Talking Re: Pulling VBA Func Result into Cell (Excell)

    TG, how about invoking your macro code from within the double-click event
    of a particular cell? I modified some of my code from CodeBank to look like below...

    Code:
    'Behind "ThisWorkbook"
    
    Option Explicit
    
    Public goTest As clsEventTrap
    
    Private Sub Workbook_Open()
        Set goTest = New clsEventTrap
    End Sub
    
    
    'Behind a new Class file names "clsEventTrap"
    Option Explicit
    
    Public WithEvents goApp As Excel.Application
    Public WithEvents goSheet As Excel.Worksheet
    Public WithEvents goBook As Excel.Workbook
    
    Private Sub Class_Initialize()
        Set goApp = Excel.Application
        Set goBook = goApp.ActiveWorkbook
        Set goSheet = goBook.ActiveSheet
    End Sub
    
    Private Sub goApp_NewWorkbook(ByVal Wb As Workbook)
        goApp.DisplayAlerts = False
        Wb.Close False
        goApp.DisplayAlerts = True
    End Sub
    
    Private Sub goApp_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
        'MsgBox "No double-clicking either!", vbOKOnly + vbExclamation
        'TGs macro function call goes here!
        'You can check the range and determine what the cell is that was double-clicked in
        'Then use that cells value to pass to your macro, if thats what you need?
        'Cancel = True
    End Sub
    
    Private Sub goApp_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    'FIRES SECOND AFTER THE GOSHEET_BEFORERIGHTCLICK EVENT
        MsgBox "No right-clicking!", vbOKOnly + vbExclamation
        Cancel = True
    End Sub
    
    Private Sub goApp_WorkbookNewSheet(ByVal Wb As Workbook, ByVal Sh As Object)
        goApp.DisplayAlerts = False
        Sh.Delete
        goApp.DisplayAlerts = True
        MsgBox "No adding new worksheets!" & vbNewLine & "I removed it for you already!", vbOKOnly + vbExclamation
    End Sub
    
    Private Sub goBook_Open()
    '
    End Sub
    
    Private Sub goSheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    '
    End Sub
    
    Private Sub goSheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    'FIRES FIRST BEFORE THE GOAPP_SHEETBEFORERIGHTCLICK EVENT
        MsgBox "File size in bytes: " & goBook.BuiltinDocumentProperties.Item("Number of Bytes")
    End Sub
    I have some other features that may be helpful in there too.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Pulling VBA Func Result into Cell (Excell)

    Thanks for the help. Some one here suggested the samething, but with over 100 rates needing to be updated, that didn't sound like a fun idea. What I'm kinda hoping for is to allow the user to open the WB, enter some info on the Ws1, click over to Ws2, hit the Update Rates button, then be able to click through Ws3-Ws12 to see the results of the calculations.

    I'm probably asking too much of Excel, and am thinking of it in terms of a VB devloper.... too bad this isn't in VB6, I'd whip out some code no prob... but alas.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Pulling VBA Func Result into Cell (Excell)

    Ok, now you kinda lost me. Isnt that easier to call your macro from the refresh
    button click event? I didnt know that using a button was an option. You can
    have your rates populate several sheets no problem there, but getting the
    updated data, I would think, would be more difficult?

    To get that solution working what do you need solved first? We can take it a
    step at a time.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Pulling VBA Func Result into Cell (Excell)

    I wondered how long it would take to lose someone.....

    Mind if I PM you with the details? I'd rather not get into too much specifics here.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Pulling VBA Func Result into Cell (Excell)

    No Prob. I will try to help as much as I can.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Thumbs up Re: Resolved: Pulling VBA Func Result into Cell (Excel)

    Milli grati to RobDog888 to helping me get this.

    I ended up doing it in a round about way, but the prob is now solved. I had to create a sub section with a list of rates and added a macro that loops through each sheet, fiunds the list, then pulls the rates from the rates sheet. Then the formulas then point to the subsection.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

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



Click Here to Expand Forum to Full Width