Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / January 2008

Tip: Looking for answers? Try searching our database.

Variable causing subscript out of range error

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andyjim - 24 Jan 2008 21:02 GMT
I am trying to saving a file based on the text name in a designated cell in
the workbook. However, with this code, I get an “subscript out of range”
error.  I am running out of ideas.  There must be a way to do  this!  Seems
to be problem with variable.

Thanks for your help.

Windows("fxRM_Update.xls").Activate

Dim bk As Workbook, bk1 As Workbook
Dim sstr As String
Dim path2 As String
   path2 = ActiveWorkbook.Path

Set bk = Workbooks("fxRM_update.xls")
sstr = bk.Worksheets("lookup").Range("d39").Value
Set bk1 = Workbooks(sstr) THIS IS WHERE SUBSCRIPT ERROR OCCURS

ActiveWorkbook.SaveCopyAs filename:= bk1
'bk.saveas
'ActiveWorkbook.SaveCopyAs filename:= p & bk1
' ActiveWorkbook.SaveCopyAs filename:=path2 & "\" & bk1

A similar macro works fine:

Sub update4() 'defines user file in fxRM_Update.xls Need to insert this
everytime you reactivate fxRM_Update and want to refer back to user file.

Dim bk As Workbook, bk1 As Workbook
Dim sstr As String

 Set bk = Workbooks("fxRM_update.xls")

 sstr = bk.Worksheets("lookup").Range("d39").Value

 Set bk1 = Workbooks(sstr)

bk1.Activate


End Sub
Jim Thomlinson - 24 Jan 2008 21:47 GMT
Your issue is gong to be that there is no workbook open that matches the
value sstr. Add a message box just before the line that sets the object
reference to confirm the value of sstr. You will need it to match exactly to
the name of the open workbook. Check for blank spaces and such...

sstr = bk.Worksheets("lookup").Range("d39").Value
msgbox sstr 'Check the value of the variable
Set bk1 = Workbooks(sstr) THIS IS WHERE SUBSCRIPT ERROR OCCURS
Signature

HTH...

Jim Thomlinson

> I am trying to saving a file based on the text name in a designated cell in
> the workbook. However, with this code, I get an “subscript out of range”
[quoted text clipped - 37 lines]
>
> End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.