Forum Home
Press F1
 
Thread ID: 54784 2005-02-22 05:43:00 Excel Dannz (1668) Press F1
Post ID Timestamp Content User
327341 2005-02-22 05:43:00 i want to use code in a Macro like this

Selection.Value = Range("Input!A1")

Setting the selected cell to what ever is in A1 on the input sheet but i get an error
Dannz (1668)
327342 2005-02-22 09:57:00 parry you are needed (bump) :D beama (111)
327343 2005-02-22 11:44:00 I’m no expert on these but possibly you need .Select on the end of that line.

Like: Selection.Value = Range("Input!A1").Select

Then any further instructions ending with:

End Sub

The "Macro Writer" should do all this for you (Tools / Macro / Record Macro).
B.M. (505)
327344 2005-02-23 00:31:00 Try: Selection.Value = Worksheets("Input").Range("A1")

I don't think you can reference a sheet name in a range it has to be a cell reference or a named range.

Cheers
Dave
odyssey (4613)
327345 2005-02-23 00:57:00 i want to use code in a Macro like this

Selection.Value = Range("Input!A1")

Setting the selected cell to what ever is in A1 on the input sheet but i get an error

Yup Daves onto it. There are several ways to reference a range on a sheet and heres a few examples...

1. Worksheets("Input").Range ("A1")
2. Sheets("Input").Range ("A1")
3. Sheets(1).Range ("A1")
4. Sheet1.Range ("A1")
5. Sheets("Input").[a1]

The first 2 are exactly the same except you can use the abbreviated Sheets instead of Worksheets. The third one uses a number to represent the sheet. The numbers are the order in which the sheet tabs appear in the book, so in this example its the first sheet. However if you move the tabs around you will find the code is no longer pointing to the correct sheet so you need to be aware of this, but its handy when looping through sheets at times.

The fourth one uses whats called the codename of the sheet and is usually what I prefer. If you open the VBE and see the list of Excel objects in the left hand window you will see Sheet1, Sheet2, Sheet3 followed by the actual name you see on the tab in brackets. The codename never changes but the sheet name or its tab position can, so this one will always work even if you changed the name of the Input sheet to something else. In the example Im presuming Sheet1 is the codename for the Input sheet.

The last one just shows how you can abbreviate the range object using square brackets. Look up the "evaluate method" in VBA help for more info on this.

Hopefully I havent confused you. :-D

EDIT
You can also reference a cell using numbers to represent the row and column of range. See the Cells method for details on this.
Parry (5696)
327346 2005-02-23 01:08:00 Try Activecell.value
instead of Selection.Value
rad_s4 (7401)
327347 2005-02-23 01:20:00 Thankyou to all the help was needed for an NCEA Assignment Dannz (1668)
327348 2005-02-23 01:40:00 Try Activecell.value
instead of Selection.Value

Yes. :-)

The reason why is that Selection can be >1 cell and so it will return the value of the cell in the upper left corner of the selection while activecell is the actual cell you selected (or first cell you selected in a selection of >1 cells)
Parry (5696)
1