.In other words VBA wants to see something in the () or brackets or
parenthesesthat has the spreadsheet Format for co ordinates A1 , B1, C3 , $A$1 , $D$7 etc.Anything else causes am error.
<o></o>
. Also in VBA (everywhere I think?) double quotes ""defines a string of the value of what you type within the double quotes ""
<o></o>
. So this does not work to give you the valuein the first cell in a spreadsheet<o></o>
<o>
Code:
Sub RangeBasics1()
MsgBox "First cell value is " & Range(A1).Value
End Sub
</o>
Because A1 is anundefined variable<o></o>
<o> </o>These two do because youhave defined that variable to a string of the form that the Range objectexpects as it's argument.<o></o>
<o> </o>
<o>
Code:
Sub Rangebasics2()
Dim A1 As String
Let A1 = "A1"
MsgBox "First cell value is " & Range(A1).Value
End Sub
</o>
<o>
Code:
Sub RangeBasics3()
Dim A1 As String
Let A1 = "$A$1"
MsgBox "First cell value is " & Range(A1).Value
End Sub
</o>
<o> </o>
Concatenating is inother words sticking things intimately together (rather than saying linking orjoining which might involve something in between) . & can be thought of asthe VBA Glue.<o></o>
<o> </o>. Things get really complicated if you look atthe VBA evaluate Function. This in it's simplestform allows you to use in VBA the normal spreadsheetFunctions.<o></o>
. So for our simplest example in the spreadsheetyou would have, rather than a message box, this =A1 written in some cell.<o></o>
. The convention / syntax of the Evaluation Functionin VBA is <o></o>
=Evaluate (“ Type_in_here_wot_you_would_after_the= “<o></o>
<o> </o>. So the following works as well for our simpleexample of getting the value of the first cell in the spreadsheet<o></o>
<o>
Code:
Sub Rangebasics4()
MsgBox "First cell value is " & Evaluate("A1")
End Sub
</o>
. In a spreadsheet, you can put more than onething in a cell using the spreadsheet & which can be thought of as thespreadsheet glue! <o></o>
. So consider wanting to get the first cell writtentwice. In a Spreadsheet it would be =A1 & A1. But something like thefollowing will not work in VBA<o></o>
<o> </o>Sub Rangebasics5()<o></o>
MsgBox "First cellvalue is " & Evaluate("A1" & "A1")<o></o>
End Sub<o></o>
That is equivalent to =A1A1in the spreadsheet, which Excel will error.<o></o>
. To get the evaluate towork you must apply it to the three things, A1, then the spreadsheet &and finally the second A1. These must be intimately stuck together with the VBA &.<o></o>
So this works<o></o>
<o> </o>Sub Rangebasics6()<o></o>
MsgBox "First cellvalue is " & Evaluate("A1" & "&"& "$A$1")<o></o>
End Sub<o></o>
<o> . </o>Looking at the ThemeRange and Evaluate further: As well as the VBA & , you can also putother VBA stuff in which again must be intimatelystuck together with a VBA & . The syntax it towrite that outside or not in the doublequotes "". For example you can get your first cell string $A$1 using
Range(“A1”).Address ( Note in parsing thatthe .Address property gives the address in the “Dollar” form $A$1 )<o></o>
<o> </o>
Sub Rangebasics7()<o></o>
MsgBox "First cellvalue is " & Evaluate("A1" & "&"& Range("A1").Address)<o></o>
End Sub<o></o>
Or going back to our simple example, an equivalent to SubRangebasics3() is<o></o>
Sub Rangebasics8()<o></o>
MsgBox "First cellvalue is " & Evaluate(Range("A1").Address)<o></o>
End Sub<o></o>
<o> </o>. Just one pen ultimate step before I stopconfusing us all!. I prefer this Format<o></o>
<o> </o>Sub Rangebasics9()<o></o>
MsgBox "First cellvalue is " & Evaluate(" " & Range("A1").Address &" ")<o></o>
End Sub<o></o>
As this helps me in along evaluation line to find out what is going on. Note between the double quotes ""I can, as I have done have any number of spaces, which again I have found helpsme to identify and build up a long evaluation line.<o
></o
>
. As a last bit. I goback to writing out the cell value twice but as we usually prefer for clarity aseparator is included between them. (I use a _ rather than a – as this helpsprevent the annoying thing of Excel “guessing I am writing a date or time and changingthe format appropriately – another tip for a beginner!). First it is good towrite out the formula in the spread sheet : =A1 & " _" & A1 . Thingsare very tricky here and you really must think what VBA is doing here. Onemight think one has 5 things to evaluate. We sort of do, but although “ _ “ canbe evaluated as in a spreadsheet formula = “ _ “ , the VBA Evaluate syntax following ourdiscussions so far would be would be <o></o>
Evaluate(""- "")<o></o>
And correspondinglythe VBA code<o></o>
Sub Rangebasics10()<o></o>
MsgBox "Firstcell value is " & Evaluate("A1" & "&"& "" - "" &"&" & "A1")<o></o>
End Sub <o></o>
But they do not work.The reason is that VBA in the evaluate syntaxthen reads evaluate nothing and then itsees a _ which it does not understand.<o></o>
So the trick (I think!!) is that immediately after the “&”we are sort of in the Spreadsheet environment. So we immediately after the “&”type “ _ “ and then immediately after this type “&” to get back in the VBA Evaluate environment. Like this<o></o>
Sub Rangebasics11()<o></o>
MsgBox "Firstcell value is " & Evaluate("A1" & "&""- ""&" & "A1")<o></o>
End Sub<o></o>
It seems to work.
<o></o>
. The same again mixingSpreadsheet and VBA bits in an Evaluate thing<o></o>
Sub Rangebasics12()<o></o>
MsgBox "Firstcell value is " & Evaluate("A1" & "&""- ""&" & " " & Range("A1").Address &" ")<o></o>
End Sub<o></o>
. Sorry to have rambled a bit and perhapstaken the thread a bit away from the original point. But it helped me to cleara bit of difficult syntax stuff in my head.
Alan<o></o>