Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

excel 1

Status
Not open for further replies.

HOMMER

Technical User
Apr 12, 2001
112
US
In a macro or vba how do I reference to a cell.
I am using a if then loop and I want x = a3.
What is the code to do this.
 
Hommer,

The following VBA code should get you started...

Sub Example()

If Range("condition_cell").Value = Range("test_cell").Value Then

Do_This 'subroutine - see below

Else

Do_That 'subroutine - see below

End If

End Sub


Sub Do_This()

Application.Goto Application.Range("B20")

ActiveCell = "This action was taken because the condition is TRUE"

End Sub


Sub Do_That()

Application.Goto Application.Range("J10")

ActiveCell = "This action was taken because the condition is FALSE"

End Sub



PLEASE NOTE: The BEST method of referring to a cell (or range of cells) in the worksheet, is to "NAME" it. This is to say, assign a "Range Name".

In the example code above, I have used two Range Names - called "test_cell", and "condition_cell". These two names have therefore been assigned to two separate cells in the worksheet.

The "easy" method of assigning a Range Name is: 1) Go to the cell (or highlight a range), 2) use Control-F3 (hold down the Control key and hit the F3 key), 3) type the name you wish to use, and 4) hit the Enter key.

IMPORTANCE OF RANGE NAMES: Range Names are EXTREMELY important - because by using them, you do NOT have to worry about always having to update your VBA code to reflect the changes which will likely occur in your worksheet as it evolves. With Range Names, Excel (internally)
"automatically" adjusts the names to reflect any changes to the worksheet.

I hope this helps.

Regards, ...Dale Watson dwatson@bsi.gov.mb.ca







 
Thank you evry much for your help dale the email you sent me was ver very helpful.
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top