Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Run-time error '9' ---- Code to fix included.

I have a problem with the following code and I cannot get it to run. I
am getting a "Run-time error
'9': Subscript out of range" error when I try to run this code. Here
is the code:


Sub openfilesInALocation()
Dim i As Integer, wb As Workbook
With Application.FileSearch
..NewSearch
..LookIn = "C:\Documents and Settings\chrisf\My Documents\vbatest"
..SearchSubFolders = False
..Filename = "*.xls"
..Execute
For i = 1 To .FoundFiles.Count
'Open each workbook
Set wb = Workbooks.Open(Filename:=.Foun*dFiles(i))
'Perform the operation on the open workbook


----------- Errors on the following line:


wb.Worksheets("sheet1").Range(*"A1").Select


'Save and close the workbook
wb.Save
wb.Close
'On to the next workbook
Next i
End With
End Sub


If anyone can solve this for me I would appreciate the help!


-Chris

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Run-time error '9' ---- Code to fix included.

Had the same error a long time ago. Split the select argument. First select
the sheet, and then select the range.

HTH,

Rody

schreef in bericht
oups.com...
I have a problem with the following code and I cannot get it to run. I
am getting a "Run-time error
'9': Subscript out of range" error when I try to run this code. Here
is the code:


Sub openfilesInALocation()
Dim i As Integer, wb As Workbook
With Application.FileSearch
..NewSearch
..LookIn = "C:\Documents and Settings\chrisf\My Documents\vbatest"
..SearchSubFolders = False
..Filename = "*.xls"
..Execute
For i = 1 To .FoundFiles.Count
'Open each workbook
Set wb = Workbooks.Open(Filename:=.Foun*dFiles(i))
'Perform the operation on the open workbook


----------- Errors on the following line:


wb.Worksheets("sheet1").Range(*"A1").Select


'Save and close the workbook
wb.Save
wb.Close
'On to the next workbook
Next i
End With
End Sub


If anyone can solve this for me I would appreciate the help!


-Chris


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Run-time error '9' ---- Code to fix included.

Rody-

Thanks for the help.....but I am still getting the error. The problem
lies with how I am naming the sheet. The name of "Sheet1" is "Jun".
If I change the code to select "Jun" instead of "Sheet1" it runs fine.
How can I get it reference the first sheet in the file....not a
specifically named sheet?

Thanks for the help!
-Chris

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Run-time error '9' ---- Code to fix included.

You can reference a worksheet by number instead of name. E.g,

Worksheet(1)


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


wrote in message
ups.com...
Rody-

Thanks for the help.....but I am still getting the error. The
problem
lies with how I am naming the sheet. The name of "Sheet1" is
"Jun".
If I change the code to select "Jun" instead of "Sheet1" it
runs fine.
How can I get it reference the first sheet in the file....not a
specifically named sheet?

Thanks for the help!
-Chris



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i change or make this macro??? pictures and code included dispelthemyth Charts and Charting in Excel 0 March 17th 06 01:02 AM
Grab Time with milliseconds included in VBA Erick Excel Programming 4 October 28th 04 10:42 AM
Require extra funtionality - existing code included Hilton Excel Programming 0 October 7th 04 02:57 PM
Run-time error from my code Matt Excel Programming 5 June 24th 04 03:32 AM
Code Error - Run Time Error 5 (Disable Cut, Copy & Paste) Tim[_36_] Excel Programming 4 April 23rd 04 02:53 AM


All times are GMT +1. The time now is 12:30 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"