View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
JLGWhiz[_2_] JLGWhiz[_2_] is offline
external usenet poster
 
Posts: 1,565
Default Subscript Out of Range Error

Well, I worked up this little test case and it seemed to work OK.

Option Base 1
Sub multWB()
Dim i As Long, myPath As String, myNames
myPath = ThisWorkbook.Path
myNames = Array("Joe", "George", "Ralph")
For i = 1 To UBound(myNames)
Workbooks("TestBook.xls").SaveCopyAs _
Filename:=myPath & "\" & myNames(i) & ".xls"
Next
End Sub


Chip pointed out something that I overlooked in my earlier post, and that is
that using just SaveAs will change the name of the active workbook and
effectively closes the original workbook that was copied. But using the
SaveCopyAS method leaves the original workbook intact and saves the new
workbooks as a closed file. The setup above captures the essence of what
your code did to build the array of names, using option base 1, so that when
the For ... Next loop runs, it is using the same parameters that your code
was using and it worked. I don't know if it was a typo when you made the
last post, but in this:

Workbooks("QA Template.xls").SaveCopyAs Filename:=myPath & "\" & Employee &
".xls"

the (x) is missing from Employee, to indicate which item of the array. Your
actual code probably has it included, but if it doesn't that would keep it
from working.



"WLMPilot" wrote in message
...
Well, back to square one. I have made a couple of changes:

1) Changed SaveAs to SaveCopyAs
2) Removed the need of an array by creating the workbook for the employee
at the point the name is placed in the spreadsheet.

Workbook("QA Template.xls") does have the file ext .xls. It is in the
same
directory as the QA Master.xls workbook which is where mypath gets the
path.

FYI...The explanation previously given that sts the error may have been
caused because of the SaveAs renaming the template workbook apparently was
not true in this case. The QA Template workbook was never renamed on the
first pass of the loop.

Here is the section of the macro that contains the SaveCopyAs code now so
you can see the new placement:

End If
rng.Offset(0, -2).Value = cnt 'cnt serves as a count on number of
employees
in list.
'Create workbook for each employee entered
Workbooks("QA Template.xls").SaveCopyAs Filename:=myPath & "\" & Employee
&
".xls"
Msg = "Do you want to enter another employee?"

Thanks for your efforts. I await other options.

Les


"JLGWhiz" wrote:

For k = 1 To num
Workbooks("QA Template.xls").SaveAs Filename:=myPath & "\" &
Employee(k)
& ".xls"
Next k

Les, if this is where you are getting the error, then be sure that your
workbook name, including the file extension is correct. You mentioned
that
you are working in xl07 and saving as xl97 - xl03. If so, your QA
Template
file could have .xlsx or .xlsm file extension which would throw the
error.





"WLMPilot" wrote in message
...
The workbooks and macros used are being created in Excel 2007, but
saved
in the
97-2003 format.

I have a macro that the boss will use to enter employees and place the
names, date added, and count (sequential) into a worksheet. It loops
in
case
there is more than one name to be added.

NOTE: The boss will be in workbook(QA Master.xls)

Once the boss is through entering names, the macro will then loop and
create
a workbook, copying the Workbook("QA Template.xls") and renaming it
with
the
employee name that was entered.

I don't know why, but I am getting the following error on the line that
copies and names the new workbook:
Runtime Error 9
Subscript Out of Range

The macro is below:

Option Base 1
'Add Employee to list and create workbook for employee
Private Sub CommandButton2_Click()
Dim Employee(1 To 200), Msg, Title As String 'Array may not reach 200
entries
Dim Config, num, k As Integer
Dim Ans1, Ans2, cnt As Integer
Dim rng As Range
num = 1 'Tracks number of employees entered. Becomes actual UBound of
array
myPath = ThisWorkbook.Path
ADDEMP:
Employee(num) = InputBox("Enter Employee's Name (First or Middle, Last
Name)", "ADD EMPLOYEE")
If Employee(num) = "" Or Left(Employee(num), 1) = " " Then
MsgBox ("Invalid Entry. Please enter employee's name.")
Employee(num) = ""
GoTo ADDEMP
End If
Msg = "Is the employee's name correctly entered?"
Msg = Msg & vbNewLine & vbNewLine
Msg = Msg & Employee(num)
Config = vbYesNo + vbQuestion
Title = "VERIFY ENTRY"
Ans1 = MsgBox(Msg, Config, Title)
If Ans1 = vbNo Then
Employee(num) = ""
GoTo ADDEMP
End If
'Validation of Entry Complete
'Proceed to place data in cells
Set rng = Range("I65536").End(xlUp).Offset(1, 0)
rng.Select
rng.Value = Employee(num)
rng.Offset(0, -1).Value = Date 'Place DATE employee added into cell
If rng.Offset(-1, -2).Value = "NUM" Then 'Determine if this is first
line
in list
cnt = 1 'If YES, cnt = 1
Else
cnt = rng.Offset(-1, -2).Value 'Pick up last number entered (total
employees to date)
cnt = cnt + 1 'Add one to last count in employee list
End If
rng.Offset(0, -2).Value = cnt 'cnt serves as a count on number of
employees
in list.
Msg = "Do you want to enter another employee?"
Config = vbYesNo + vbQuestion
Title = "CONTINUE"
Ans2 = MsgBox(Msg, Config, Title)
If Ans2 = vbYes Then
num = num + 1
GoTo ADDEMP
End If
'Create workbook for each employee entered
For k = 1 To num
Workbooks("QA Template.xls").SaveAs Filename:=myPath & "\" &
Employee(k)
& ".xls"
Next k
End Sub

I appreciate any help with this.

Les



.