Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

excel vba switching between

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Excel VBA switching between workbooks

Excel VBA switching between workbooks

ResolvedVersion 2003

Tracey has attended:
Excel Advanced course
Excel VBA Intro Intermediate course

Excel VBA switching between workbooks

Hi, Would you be able to advise why this code does not work:

Sub Hyperlinks()
''Set up hyperlinks
Workbooks.Open ("I:\DATA\Enterprise\Peugeot EQC\PSA Bimarques\Project\Peugeot Bi-marque Final 10Mar10.xls")
BimarquesFinal = ActiveWorkbook.Name
Sheets(1).Select

For y = 3 To 57

Dim HyperAddress As String
HyperAddress = "'" & ThisWorkbook.Sheets("Contents list").Cells(y, 2).Value & "'!A1"
Workbooks(BimarquesFinal).Sheets(2).Cells(y, 1).Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
HyperAddress, TextToDisplay:= _
Workbooks(BimarquesFinal).Sheets("Workbook Contents").Cells(y, 1).Value

Next y

The line which does not work is:

Workbooks(BimarquesFinal).Sheets(2).Cells(y, 1).Select

Basically, all I want to do is, once I've picked up my HyperAddress values from the workbook which contains the VBA, go back to the workbook which I've just opened and select a cell on sheet 2! I've tried a number of things and can't seem to find a way of specifying this without it falling over.

Thanks

Tracey

RE: Excel VBA switching between workbooks

Hi Tracey

Thanks for your question

Could you clarify something for me? When your code falls over does it generate a run time error. If so what is the number and description?

Thanks

Stephen

RE: Excel VBA switching between workbooks

Hi Stephen,

Yes, it's run-time error 1004 - "Select method of Range class failed".

Tracey

RE: Excel VBA switching between workbooks

Hi Tracy

Thanks for the update. Sorry about the delay in getting back to you, I've been down with flu.

Your problem is that you can only select a cell in the active worksheet. In your code, before the loop you select sheet 1 and then in the loop you try to select a cell in sheet 2.

If you select sheet 2 before the loop that should solve the problem

Regards

Stephen

RE: Excel VBA switching between workbooks

Thanks. So, for future reference, you cannot select a cell without first having selected the worksheet? You cannot select both a worksheet and a cell with the same line of code?

Could I also have achieved the same result by selecting the worksheet on one line and the cell on the next line?

Tracey

---

Sub Hyperlinks()
''Set up hyperlinks
Workbooks.Open ("I:\DATA\Enterprise\Peugeot EQC\PSA Bimarques\Project\Peugeot Bi-marque Final 10Mar10.xls")
BimarquesFinal = ActiveWorkbook.Name

For y = 3 To 57

Dim HyperAddress As String
HyperAddress = "'" & ThisWorkbook.Sheets("Contents list").Cells(y, 2).Value & "'!A1"
Workbooks(BimarquesFinal).Sheets(2).Select
Cells(y, 1).Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
HyperAddress, TextToDisplay:= _
Workbooks(BimarquesFinal).Sheets("Workbook Contents").Cells(y, 1).Value

Next y

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Change the Value of a Constant

When using a named constant in a worksheet, you may wish to change the value of that constant.

From the 'Insert' menu, select 'Name', then select 'Define'.

In the 'Define Name' dialog box, select the constant that you want to change.
Change the value in the 'Refers To' box.
Click OK.

Wherever that named constant has been used it will now use its new value.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.