ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Yes, I'm an idiot...But I cannot seem to divide two values in VBA see below (https://www.excelbanter.com/excel-programming/344125-yes-im-idiot-but-i-cannot-seem-divide-two-values-vba-see-below.html)

[email protected]

Yes, I'm an idiot...But I cannot seem to divide two values in VBA see below
 
Obviously I'm a ultra newbie to this. I tried searching this group and
find stuff that is close, but not what I need. If fear that I am
looking for something that is so easy, that everyone else has figured
it out. Hence, I cannot find the right solution.

See below for the oh so eloquent abortion of code that I have brought
together.

Dim span As Range
Dim divisions As Range
Set span.Value = Worksheets("Sheet1").Range("G20")
Set divisions.Value = Worksheets("Sheet1").Range("E20")
Set answer.Value = span.Value / divisions.Value
Worksheets("Sheet1").Range("J20").Value = answer.Value

Now that I have the masses writhing in pain from my stupidity, could
someone rectify the situation for this dolt.


Chip Pearson

Yes, I'm an idiot...But I cannot seem to divide two values in VBA see below
 
Try

Set span = Worksheets("Sheet1").Range("G20")
Set divisions = Worksheets("Sheet1").Range("E20")
answer= span.Value / divisions.Value
Worksheets("Sheet1").Range("J20").Value = answer


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



wrote in message
oups.com...
Obviously I'm a ultra newbie to this. I tried searching this
group and
find stuff that is close, but not what I need. If fear that I
am
looking for something that is so easy, that everyone else has
figured
it out. Hence, I cannot find the right solution.

See below for the oh so eloquent abortion of code that I have
brought
together.

Dim span As Range
Dim divisions As Range
Set span.Value = Worksheets("Sheet1").Range("G20")
Set divisions.Value = Worksheets("Sheet1").Range("E20")
Set answer.Value = span.Value / divisions.Value
Worksheets("Sheet1").Range("J20").Value = answer.Value

Now that I have the masses writhing in pain from my stupidity,
could
someone rectify the situation for this dolt.




JE McGimpsey

Yes, I'm an idiot...But I cannot seem to divide two values in VBA see below
 
You need to Set the object (range) variable, not its value, to the
range. Also, your answer is a double, not an object. Here's one way:

Dim span As Range
Dim divisions As Range
Dim answer As Double
Set span = Worksheets("Sheet1").Range("G20")
Set divisions = Worksheets("Sheet1").Range("E20")
answer = span.value / divisions.Value
Worksheets("Sheet1").Range("J20").Value = answer

Another, using With...End With:

Dim span As Range
Dim divisions As Range
Dim answer As Double
With Worksheets("Sheet1")
Set span = .Range("G20")
Set divisions = .Range("E20")
answer = span.value / divisions.Value
.Range("J20").Value = answer
End With

You could avoid object variables altogether:

Dim answer As Double
Dim span As Double
Dim divisions As Double
span = Worksheets("Sheet1").Range("G20").Value
divisions = Worksheets("Sheet1").Range("E20").Value
answer = span / divisions
Worksheets("Sheet1").Range("J20").Value = answer

Alteratively, without using intermediate variables:

With Worksheets("Sheet1")
.Range("J20").Value = _
.Range("G20").Value / .Range("E20").Value
End With




In article .com,
wrote:

Obviously I'm a ultra newbie to this. I tried searching this group and
find stuff that is close, but not what I need. If fear that I am
looking for something that is so easy, that everyone else has figured
it out. Hence, I cannot find the right solution.

See below for the oh so eloquent abortion of code that I have brought
together.

Dim span As Range
Dim divisions As Range
Set span.Value = Worksheets("Sheet1").Range("G20")
Set divisions.Value = Worksheets("Sheet1").Range("E20")
Set answer.Value = span.Value / divisions.Value
Worksheets("Sheet1").Range("J20").Value = answer.Value

Now that I have the masses writhing in pain from my stupidity, could
someone rectify the situation for this dolt.


[email protected]

Yes, I'm an idiot...But I cannot seem to divide two values in VBA see below
 
In full disclosure I will post the entire code. I keep getting a
"Overflow error".
----------------------------------------------------------------------------------------------
Private Sub ComboBox1_Change()
If ComboBox1.Value = "" Then Exit Sub
If ComboBox1.Value = "µm per pixel" Then
Sheets("Sheet1").Range("N20").Value = _
Sheets("Sheet2").Range("B21").Value
With Worksheets("Sheet1")
.Range("J20").Value = _
.Range("G20").Value / .Range("E20").Value
End With
ElseIf ComboBox1.Value = "µm per div" Then
Sheets("Sheet1").Range("N20").Value = _
Sheets("Sheet2").Range("B21").Value
ElseIf ComboBox1.Value = "thou per pixel" Then
Sheets("Sheet1").Range("N20").Value = _
Sheets("Sheet2").Range("D21").Value
ElseIf ComboBox1.Value = "thou per div" Then
Sheets("Sheet1").Range("N20").Value = _
Sheets("Sheet2").Range("D21").Value
ElseIf ComboBox1.Value = "mag" Then
Sheets("Sheet1").Range("N20").Value = _
Sheets("Sheet2").Range("B143").Value
ElseIf ComboBox10.Value = "N/A" Then
Sheets("Sheet1").Range("N20").Value = "N/A"
Sheets("Sheet1").Range("J20").Value = "N/A"
End If
Application.ScreenUpdating = True
End Sub
-----------------------------------------------------------------------------------------------------


Tom Ogilvy

Yes, I'm an idiot...But I cannot seem to divide two values in VBA see below
 
One more if you don't specifically need to create those range references.

With Worksheets("Sheet1")
.Range("J20").value = .Range("G20").Value/.Range("E20").Value
End With


--
Regards,
Tom Ogilvy


wrote in message
oups.com...
Obviously I'm a ultra newbie to this. I tried searching this group and
find stuff that is close, but not what I need. If fear that I am
looking for something that is so easy, that everyone else has figured
it out. Hence, I cannot find the right solution.

See below for the oh so eloquent abortion of code that I have brought
together.

Dim span As Range
Dim divisions As Range
Set span.Value = Worksheets("Sheet1").Range("G20")
Set divisions.Value = Worksheets("Sheet1").Range("E20")
Set answer.Value = span.Value / divisions.Value
Worksheets("Sheet1").Range("J20").Value = answer.Value

Now that I have the masses writhing in pain from my stupidity, could
someone rectify the situation for this dolt.




[email protected]

Yes, I'm an idiot...But I cannot seem to divide two values in VBA see below
 
Ok,

I figured out that I didn't have any numbers in those cells and it was
throwing a Overflow error.

Thanks to the both of you. I do appreciate it.



All times are GMT +1. The time now is 10:37 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com