Results 1 to 15 of 15

Thread: Sending a VB variable from a module to Excel Spreadsheet Cell

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Sending a VB variable from a module to Excel Spreadsheet Cell

    How can I send a variable theta_eff from a VB module to a spreadsheet cell named Theta.

    Obviously, I am new to this program I am using v 6.3.

    Thanks anybody!

    B

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Sending a VB variable from a module to Excel Spreadsheet Cell

    Thread moved to Office Development/VBA forum (note that the "VB Editor" in Office programs is actually VBA rather than VB, so the VB6 forum is not really apt)

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Sending a VB variable from a module to Excel Spreadsheet Cell

    try like
    range("Theta") = theta_eff
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Re: Sending a VB variable from a module to Excel Spreadsheet Cell

    Here is what I am trying, with no results so far (no errors either), but that could be because I haven't yet translated all of the rest of the code yet:

    Function BearingOutputs() As Double
    Worksheets("Outputs").Range("Theta").Value = theta_eff
    Worksheets("Outputs").Range("EccentricityRatio").Value = ecc
    Worksheets("Outputs").Range("AttitudeAngle").Value = att

    Where Outputs is the name of the worksheet tab within the file (should it be the filename, instead?). Do I have to place anything else in the function call?

    Also, do I have to do anything to the Theta Excel cell in order to make it take the sent variable value?

    Thanks for the help!

    B

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Sending a VB variable from a module to Excel Spreadsheet Cell

    What you have got should be putting values into the cells, but what values depends on the variables - so check what theta_eff actually contains when that code runs.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Sending a VB variable from a module to Excel Spreadsheet Cell

    if the range "Theta" etc is a defined name AND the variable theta_eff, is in scope and contains a value, then i can see no reason why it should not work

    as the variables are not declared within the function (or at least the part i can see) where are they declared and their values set?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Re: Sending a VB variable from a module to Excel Spreadsheet Cell

    Variables are declared in the previous function in the module. I would assume I have to call them, then, yes?

    B

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Sending a VB variable from a module to Excel Spreadsheet Cell

    If the variables are declared inside a sub/function, they are only available inside that sub/function.

    To share variables between different routines, declare them in the General-Declarations section of the code (at the very top, before any subs/functions) using the keyword Private or Public.

    For further explanation, see the article What is the difference between Dim/Private/Public/Global/Static/Const? from our Classic VB FAQs (in the FAQ forum, which is shown near the top of our home page)

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Re: Sending a VB variable from a module to Excel Spreadsheet Cell

    HTML Code:
    Function BearingOutputs(theta_eff, ecc, att, som, pressure_average, dof, dfc, clearance_eff, loss, flowTotal, peakTemp, deltaTheta, loss) As Double
        
    'Variables to be sent to Excel
    
        Worksheets("Outputs").Range("Theta").Value = theta_eff
        Worksheets("Outputs").Range("EccentricityRatio").Value = ecc
        Worksheets("Outputs").Range("AttitudeAngle").Value = att
    That's a snippet of the code. I have, for example, a cell named Theta, but nothing is going to it at the moment. I believe this is just due to the fact that the previous function isn't complete, but is there anything else you can see that is missing in order for, say, the value of theta_eff, to be sent to Excel cell Theta?

    Thanks for all the help.

  10. #10

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Re: Sending a VB variable from a module to Excel Spreadsheet Cell

    Public is how I had them declared to begin with, before I started tinkering and getting aggrevated because I didn't know what was going on haha.

    Thanks

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Sending a VB variable from a module to Excel Spreadsheet Cell

    you can test the above code by putting a literal value
    Worksheets("Outputs").Range("Theta").Value = 123

    if that works you know the problem is with your variables

    public variables must be declared at the top of the code module in the general section
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Re: Sending a VB variable from a module to Excel Spreadsheet Cell

    Are you sure it is possible to use .Range for a single cell named Theta?

    Here is what I have:


    Code:
     Function BearingOutputs() As Double
        'Variables to be sent to Excel
        Worksheets("Outputs").Range("Theta").Value = 555
    End Function
    I am sending it to a worksheet titles Outputs to a cell entitled Theta and nothing happens

    Any ideas?

    Thanks for all the help

    B

  13. #13

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Re: Sending a VB variable from a module to Excel Spreadsheet Cell

    I think the problem is definitely using a function instead of a subroutine. I thought using Public variables would make this unnecessary, but I think I was wrong, so I am redoing it now and it seem promising so far.

    Thanks for the help!

    B

  14. #14
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Sending a VB variable from a module to Excel Spreadsheet Cell

    Is this what you are trying to do?

    vb Code:
    1. '~~> Run This
    2. Sub Test()
    3.     Dim Ret
    4.     Ret = BearingOutputs()
    5. End Sub
    6.  
    7. Function BearingOutputs()
    8.     '~~> Variables to be sent to Excel
    9.     Worksheets("Outputs").Range("Theta").Value = 555
    10. End Function
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  15. #15
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Sending a VB variable from a module to Excel Spreadsheet Cell

    how did you define the cell theta?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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