Opening a workbook

ch46guy

New Member
Joined
Apr 29, 2013
Messages
29
I'm trying to open an excel workbook and read the data from sheet1. when it gets to the "With Workbooks(xyz).sheets(Sheet1)" it gives me an error "Run-Time error '9': Subscript out of range.

Also when I try and close the workbook, it gives me an error. Any help would be appreciated.

Danka

Code:
Public Sub imporCord()
Dim RowNum As Integer
RowNum = 1
Dim easting(), northing(), elevation() As Double
Dim boring As String
Dim selection As String
Dim xyz As String


   ' selection = Application.GetOpenFilename("MIP Files (*.xls), *.xls")
  xyz = ("Z:\Advanced Site Characterization\Public Folder\Project Folder\2014\14.0089 - Johnson Company - Flowery Branch, GA\Data from JCO\MIP_location_XYZ.xlsx")
   Workbooks.Open (xyz)
    
  With Workbooks(xyz).Sheets(Sheet1)


     End With
    
 Workbooks.Close (xyz)


End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
You only need the workbook name when referring to a workbook with Workbooks.
 
Upvote 0
So I tried using xyz as just the work book name but it gives me an error: Run-Time error '13' type mismatch.


Code:
Public Sub imporCord()
Dim RowNum As Integer
RowNum = 1
Dim easting(), northing(), elevation() As Double
Dim boring As String
Dim selection As String
Dim xyz As String


   ' selection = Application.GetOpenFilename("MIP Files (*.xls), *.xls")
  xyz = Dir$("Z:\Advanced Site Characterization\Public Folder\Project Folder\2014\14.0089 - Johnson Company - Flowery Branch, GA\Data from JCO\MIP_location_XYZ.xlsx")
   Workbooks.Open ("Z:\Advanced Site Characterization\Public Folder\Project Folder\2014\14.0089 - Johnson Company - Flowery Branch, GA\Data from JCO\MIP_location_XYZ.xlsx")
    
  With Workbooks(xyz).Worksheets(Sheet1)


     End With
    




End Sub
 
Upvote 0
Sheet1 could be the code name of a worksheet in the workbook you have opened, when using Sheets to refer to a sheet you need the sheet name as a string.
 
Upvote 0

Forum statistics

Threads
1,216,180
Messages
6,129,339
Members
449,504
Latest member
Alan the procrastinator

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top