I need to open a workbook based on a text entry selected from a drop down box in another workbook. If a workbook by the selected name exists, then it opens. If it does not exist, I want the option to create it by first having the code open an existing template.xls file (on which the selected name will be based), then saving-as automatically to the originally selected name in the drop-down box.
Example:
A dropdown (combo) box on the worksheet “Current”, in the workbook Database.xls, displays a list of name entries from a worksheet list.
I choose one from the list (i.e. my name) then the entry gets appended with “.xls”. The “my name.xls” workbook then opens if it exists. Of course if it doesn't exist, the option to create it follows.
Here is my code thus far:
Sub NamePickup()
Dim wbname As Range
Dim wbfound As Boolean
Dim wb As Workbook
Set wbname = ThisWorkbook.Sheets("Current").DropDowns("Drop Down 839").ListIndex = GetDropDownIndex.Worksheets("Current").DropDowns("Drop Down 839").Value
wbfound = False
For Each wb In Application.Workbooks
If wb.Name = wbname.Text & ".xls" Then
wbfound = True
wb.Open
Exit For
End If
Next
If wbfound = False Then GoTo Exit_Point
' Do the first action e.g:
Range("A1").Select
Exit_Point:
If wbfound = False Then Response = MsgBox("Workbook " & wbname & ".xls cannot be found. Do you want to create it?")
If Response = vbYes Then
ChDir "C:\my folder\Database"
Workbooks.Open(Filename:= _
" C:\my folder\\template.xls" _
).RunAutoMacros Which:=xlAutoOpen
ActiveWorkbook.SaveAs Filename:=wb.Name
Else: GoTo Exit_Point
Exit_Point:
Exit Sub
End If
End Sub
Example:
A dropdown (combo) box on the worksheet “Current”, in the workbook Database.xls, displays a list of name entries from a worksheet list.
I choose one from the list (i.e. my name) then the entry gets appended with “.xls”. The “my name.xls” workbook then opens if it exists. Of course if it doesn't exist, the option to create it follows.
Here is my code thus far:
Sub NamePickup()
Dim wbname As Range
Dim wbfound As Boolean
Dim wb As Workbook
Set wbname = ThisWorkbook.Sheets("Current").DropDowns("Drop Down 839").ListIndex = GetDropDownIndex.Worksheets("Current").DropDowns("Drop Down 839").Value
wbfound = False
For Each wb In Application.Workbooks
If wb.Name = wbname.Text & ".xls" Then
wbfound = True
wb.Open
Exit For
End If
Next
If wbfound = False Then GoTo Exit_Point
' Do the first action e.g:
Range("A1").Select
Exit_Point:
If wbfound = False Then Response = MsgBox("Workbook " & wbname & ".xls cannot be found. Do you want to create it?")
If Response = vbYes Then
ChDir "C:\my folder\Database"
Workbooks.Open(Filename:= _
" C:\my folder\\template.xls" _
).RunAutoMacros Which:=xlAutoOpen
ActiveWorkbook.SaveAs Filename:=wb.Name
Else: GoTo Exit_Point
Exit_Point:
Exit Sub
End If
End Sub