variable not accepting range value

egoosen3

New Member
Joined
Mar 11, 2012
Messages
18
Hi

I am using an inputBox to get a value from a cell in excel. My variable gInput is not accepting the value from the range.
ub gAddDetail1()
Dim rng As Range
Dim gInput As Variant
'Get A Cell Address From The User to Get Number Format From
On Error Resume Next
Set rng = Application.InputBox( _
Title:="Data field from Cell", _
Prompt:="Select a cell to pull in your Data Field", _
Type:=8)
On Error GoTo 0

'Test to ensure User Did not cancel
If rng Is Nothing Then Exit Sub

'Set Variable to first cell in user's input
Set rng = rng.Cells(1, 1)

'Data Field
gInput = rng.Value

'

End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
if i run the code, the rng.Value contains the value of the cell in excel, however gInput in gInput = rng.Value is empty
 
Upvote 0
You must be looking before the code has stepped past the line that is assigning the cell value to the glinput variable.
 
Upvote 0
If you step through the code using F8, then gInput will not have a value until End Sub is highlighted yellow.
The yellow line is the next line to get run.
 
Upvote 0
Until the code goes past the line where the value of the cell is assigned to the variable, the variable will be empty because it hasn't received the instruction yet.
 
Upvote 0
Works fine for me. Add a message box at the end what does it say?

VBA Code:
gInput = rng.Value

MsgBox gInput

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,661
Messages
6,120,792
Members
448,994
Latest member
rohitsomani

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top