English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

For example, let's pretend in cell A1 the total value is 100

When I create a button in Excel, and Assign Macro (which opens up Visual Basic) , how can I get the value of the cell into a variable I declared?

2006-09-21 10:24:49 · 5 answers · asked by b1911 2 in Computers & Internet Programming & Design

5 answers

' //
' // Start Code
' //
Sub b1911()
szCell = Workbooks("Book1"). Worksheets("Sheet1"). Cells(1, 1). Value
MsgBox szCell
End Sub
' //
' // End Code
' //

2006-09-21 10:32:40 · answer #1 · answered by f 3 · 0 0

In your VBA code, write
Sub GetValue()
ThisWorkbook.Activate
Sheets("xxxx").select
myVar = Range("A1")
End Sub
Where xxxx is the name of the sheet on which you have the value in cell A1.

Further you can write msgbox "Value of my var is " & myVar to show a pop up message displaying the value of myVar.

Hope this helps

2006-09-21 17:25:55 · answer #2 · answered by SmartSpider 4 · 0 0

I don't see my favorite way to refer to cells in the other answers:

dim MyA1Var
MyA1Var = [A1]

I capitalize the A for appearances. It works in any case.

2006-09-24 07:31:48 · answer #3 · answered by Ken C. 6 · 0 0

Use either of the two below:

=Range("A1")
or
= Cells(1,1)

Note that when using 'Range' you have to use inverted commas, and hence cannot be used with a formula that returns the Rows or column numbers.

whereas when using 'Cells' you do not use any inverted commas and therefore is more versatile.
Cells(RowNum,ColNum)

2006-09-23 08:45:00 · answer #4 · answered by Dilip Rao 3 · 0 0

oCell = Workbooks(""). Worksheets(""). Cells(1, 1)

2006-09-21 18:10:53 · answer #5 · answered by Ralph 7 · 0 0

fedest.com, questions and answers