Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default Subscript out of range

I have the following before save macro saved, but when I try to run the macro
I get the following error:
Run-time error '9':

Subscript out of range

and my macro looks like this:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Range("B1:B20").Select
Selection.Copy
Workbooks.Open Filename:= _
"C:\Documents and Settings\User\Desktop\Book1"
Windows("Book1").Activate
Sheets("Sheet1").Select
Application.Goto Reference:="R1C256"
Selection.End(xlToLeft).Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(0, 1).Select
End If
Loop Until IsEmpty(ActiveCell) = True
ActiveSheet.Paste
End Sub

when I click on Debug
the line: Windows("Book1").Activate
becomes highlighted with flash yellow with an arrow on the left is visible.

when I try to ignore that step like this:
' Windows("Book1").Activate

the debugger highlights the next line..

if you know what's wrong with it or what should I do to correct it, please
assist.

this macro is supposed to copy the contents highlighted in the second line
and opens the file "book1" from the desktop and paste it at the next unused
cell starting from the end of the sheet [Range("VI1")]

if you know a better way to avoid those steps and use better ones please
provide me with assistance.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Subscript out of range



"Abdul" wrote:

I have the following before save macro saved, but when I try to run the macro
I get the following error:
Run-time error '9':

Subscript out of range

and my macro looks like this:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Range("B1:B20").Select
Selection.Copy
Workbooks.Open Filename:= _
"C:\Documents and Settings\User\Desktop\Book1"
Windows("Book1").Activate
Sheets("Sheet1").Select
Application.Goto Reference:="R1C256"
Selection.End(xlToLeft).Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(0, 1).Select
End If
Loop Until IsEmpty(ActiveCell) = True
ActiveSheet.Paste
End Sub

when I click on Debug
the line: Windows("Book1").Activate
becomes highlighted with flash yellow with an arrow on the left is visible.

when I try to ignore that step like this:
' Windows("Book1").Activate

the debugger highlights the next line..

if you know what's wrong with it or what should I do to correct it, please
assist.

this macro is supposed to copy the contents highlighted in the second line
and opens the file "book1" from the desktop and paste it at the next unused
cell starting from the end of the sheet [Range("VI1")]

if you know a better way to avoid those steps and use better ones please
provide me with assistance.


Hi Abdul

Change the line "Windows("Book1").Activate" to
"Windows("Book1.xls").Activate"

.... the Windows collection holds all open windows (not just Excel ones) and
as such, needs the full name. Also, when you commented out this line, the
next line - "Sheets("Sheet1").Select" had the same error. I'm guessing this
is because both open sheets have a "Sheet1" and the debugger has no idea
which one to use ... throwing up a rather cryptic error message which means
"Not sure where to look in the Sheets collection array".

BTW - It's better practice to assign your workbooks/sheets etc to the
correct object variable. There's plenty of good info on this in the Excel VBA
help files.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default Subscript out of range

thanks for the reply, however your suggestion didn't solve the problem. and
currently I edited few more of the lines and removed some unnecessary ones,
but I'm still unable to paste the copied data, the code now looks like the
following:

Range("B1:B20").Select
Selection.Copy
Workbooks.Open Filename:= _
"C:\Documents and Settings\User\Desktop\Book1.xls"
ActiveWorkbook.Windows("Book1.xls").Activate
ActiveWorkbook.Sheets("Sheet1").Activate
Application.Goto Reference:="R1C256"
Selection.End(xlToLeft).Select
ActiveCell.Offset(0, 1).Select
Selection.Paste
End sub

the debugger shows there is an error on the line before the
last,[selection.paste] do you have any suggestions?
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Subscript out of range

If you had recorded a macro you would have found that the correct
final line is
ActiveSheet.Paste
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default Subscript out of range

ActiveSheet.Paste

I've been using "Record Macro" function to come up with most of the lines
up there, but it seems that even using exact copy & paste doesn't work.

I tried the following:
ActiveSheet.paste
ActiveCell.paste
Activeworkbook.paste
active... etc, none of them worked.

in the end I just removed the header for "Before save" kind of auto macro,
into a normal user triggered macro, and it finally worked, but sometimes it
fails when the contents of the target paste cells contain a different format
pattern than the source copy format of the cells. in my case it was the way
the Date was formatted on source was different from destination, adjusting
them manually to be the same format fixed the issue. but i don't know if
it'll happen again.


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
Subscript out of range OKHM Excel Discussion (Misc queries) 0 August 6th 09 05:18 PM
what does 'Subscript Out of range' mean?? Gary Excel Worksheet Functions 2 March 22nd 07 01:33 AM
Subscript out of Range Jon[_22_] Excel Programming 4 April 6th 06 11:24 PM
Subscript out of range? Jason Hancock Excel Programming 3 May 26th 04 07:11 PM
Subscript Out of Range John Wilson Excel Programming 2 September 7th 03 04:07 AM


All times are GMT +1. The time now is 06:53 PM.

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"