|
-
Apr 13th, 2009, 02:26 PM
#1
Thread Starter
New Member
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
-
Apr 13th, 2009, 02:58 PM
#2
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)
-
Apr 13th, 2009, 04:26 PM
#3
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
-
Apr 14th, 2009, 07:37 AM
#4
Thread Starter
New Member
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
-
Apr 14th, 2009, 07:52 AM
#5
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.
-
Apr 14th, 2009, 07:54 AM
#6
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
-
Apr 14th, 2009, 08:00 AM
#7
Thread Starter
New Member
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
-
Apr 14th, 2009, 08:06 AM
#8
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)
-
Apr 14th, 2009, 08:08 AM
#9
Thread Starter
New Member
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.
-
Apr 14th, 2009, 08:10 AM
#10
Thread Starter
New Member
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
-
Apr 14th, 2009, 04:11 PM
#11
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
-
Apr 15th, 2009, 08:00 AM
#12
Thread Starter
New Member
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
-
Apr 15th, 2009, 02:53 PM
#13
Thread Starter
New Member
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
-
Apr 15th, 2009, 03:45 PM
#14
Re: Sending a VB variable from a module to Excel Spreadsheet Cell
Is this what you are trying to do?
vb Code:
'~~> Run This
Sub Test()
Dim Ret
Ret = BearingOutputs()
End Sub
Function BearingOutputs()
'~~> Variables to be sent to Excel
Worksheets("Outputs").Range("Theta").Value = 555
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
-
Apr 15th, 2009, 04:38 PM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|