Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is cell "A1" being used in this GetValue function in VBA?

Tags:

excel

vba

I'm using this function to retrieve a value from a closed workbook. In this 8th line of this code, I don't understand why "A1" is being used. What exactly is happening in that entire 8th line? I'm confused by the xlR1C1 argument as well.

Private Function GetValue(path, file, sheet, ref)

    Dim arg As String
    If Right(path, 1) <> "\" Then path = path & "\"
    If Dir(path & file) = "" Then
        GetValue = "File Not Found"
        Exit Function
    End If
    arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
      Range(ref).Range("A1").Address(, , xlR1C1)
    GetValue = ExecuteExcel4Macro(arg)

End Function
like image 978
shampouya Avatar asked Jan 07 '15 19:01

shampouya


People also ask

How do I set a cell value in VBA?

Using an Input Box If you want a user to specify a value to enter in a cell you can use an input box. Let's say you want to enter the value in cell A1, the code would be like this: Range("A1"). Value = _ InputBox(Prompt:="Type the value you want enter in A1.")

How do you define a cell in VBA?

To refer to a single cell, you have to refer to a single cell. Syntax is simple “Range(“Cell”)”. Here, we will use “. Select” command to select the single cell from the sheet.

How do I pull a value from a cell in Excel VBA?

From the VBA IntelliSense list, choose the “Value” property to get the value from the mentioned cell. Now, the variable “CellValue” holds the value from cell A1. Show this variable value in the message box in VBA. Run the code and see the result in a message box.

How do I select a cell in Excel VBA?

Selecting a Single Cell Using VBARange(“A1”) tells VBA the address of the cell that we want to refer to. Select is a method of the Range object and selects the cells/range specified in the Range object. The cell references need to be enclosed in double quotes.


3 Answers

Range().Range() Documentation here:

When applied to a Range object, the property is relative to the Range object. For example, if the selection is cell C3, then Selection.Range("B1") returns cell D3 because it’s relative to the Range object returned by the Selection property. On the other hand, the code ActiveSheet.Range("B1") always returns cell B1.

The code is using that second Range("A1") to ensure that if you have a ref range larger than one cell, it only returns the top left cell of that range. Also it would appear your other Sub called ExecuteExcel4Macro() requires an R1C1 type cell reference so the address is being converted into that type for passing the arg string into the Sub.

like image 106
Chrismas007 Avatar answered Oct 07 '22 00:10

Chrismas007


xlR1C1 is a reference style which is used to specify how formulas work. When using this style, your formulas will work and look very differently than you expect. The R1C1 specification, basically means the cells are referred to differently using row & column ordinals instead of letter names. For example, when using xlR1C1, you would access cell B2 by using =R2C2 (row2, column 2). Another Example, cell C10 could be referred to as =R10C3

As to whats happening on line 8... you are constructing a cell reference that looks like this: (Note that your cell reference will be different because it has a file path in it)

='[Myfilename.xlsx]Sheet1'!R1C1

You can use the debugger to view the string contained in the arg variable.

like image 32
laylarenee Avatar answered Oct 06 '22 22:10

laylarenee


Adding Range("A1") to the code doesn't appear to do anything at all. Typing this into the immediate window results in some... unexpected results.

?Range("B3").Range("A1").Address
$B$3

Now, I would have expected that to return $A$1, but it seems that this part of the function will return the address of Range(ref).

Now, calling Range.Address with the ReferenceStyle argument would produce these results.

?Range("B3").Range("A1").Address(,,xlR1C1)
R3C2
like image 43
RubberDuck Avatar answered Oct 06 '22 23:10

RubberDuck