ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Newbie Run Time Error (https://www.excelbanter.com/excel-programming/364451-newbie-run-time-error.html)

have_a_cup

Newbie Run Time Error
 

When using a Dim statement, is there any way to declare a generic value,
so that if there aren't actually 10 workbooks, the macro just ends...or,
how do I get around the run time 1004 error from happening / displaying
alert????

Dim x As Integer
Dim WB As String

For x = 1 To 10


WB = "G:\myFolder\TEST\RBA\RBA " & x & ".xls"
Workbooks.Open Filename:=WB


Next

End Sub


--
have_a_cup
------------------------------------------------------------------------
have_a_cup's Profile: http://www.excelforum.com/member.php...o&userid=35394
View this thread: http://www.excelforum.com/showthread...hreadid=552501


Jim Thomlinson

Newbie Run Time Error
 
There is no way to avoid the error, but you can handle the error, something
like this...

Dim x As Integer
Dim WB As String
dim wbk as workbook

For x = 1 To 10
WB = "G:\myFolder\TEST\RBA\RBA " & x & ".xls"
on error resume next
set wbk = Workbooks.Open (Filename:=WB)
on error goto 0
if not wbk is nothing then
'the workbook exists so have at it...
endif
Next
End Sub

--
HTH...

Jim Thomlinson


"have_a_cup" wrote:


When using a Dim statement, is there any way to declare a generic value,
so that if there aren't actually 10 workbooks, the macro just ends...or,
how do I get around the run time 1004 error from happening / displaying
alert????

Dim x As Integer
Dim WB As String

For x = 1 To 10


WB = "G:\myFolder\TEST\RBA\RBA " & x & ".xls"
Workbooks.Open Filename:=WB


Next

End Sub


--
have_a_cup
------------------------------------------------------------------------
have_a_cup's Profile: http://www.excelforum.com/member.php...o&userid=35394
View this thread: http://www.excelforum.com/showthread...hreadid=552501



RB Smissaert

Newbie Run Time Error
 
You can handle that in several ways, but maybe the simplest is just putting:
On Error Resume Next
before your line:
Workbooks.Open Filename:=WB

RBS


"have_a_cup" wrote
in message ...

When using a Dim statement, is there any way to declare a generic value,
so that if there aren't actually 10 workbooks, the macro just ends...or,
how do I get around the run time 1004 error from happening / displaying
alert????

Dim x As Integer
Dim WB As String

For x = 1 To 10


WB = "G:\myFolder\TEST\RBA\RBA " & x & ".xls"
Workbooks.Open Filename:=WB


Next

End Sub


--
have_a_cup
------------------------------------------------------------------------
have_a_cup's Profile:
http://www.excelforum.com/member.php...o&userid=35394
View this thread: http://www.excelforum.com/showthread...hreadid=552501




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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com