I would rethink how your trying to do this.
You could certainly add more Case sheetname statements in the Form Initialization, but it mean each time you add new sheet with a corresponding folder you have to modify your VBA code.
This is not desirable.
I also noticed that there is no data in your sheets, is there a reason for this? If you are just using Excel for the PDF forms to display PDF files. You would not need the sheets. Just wondering.
Instead try this ...
Create a new sheet (I called mine "Config") where you can create a Sheet to Folder relationship in 2 Columns like below
If you change the name of the sheet that contain this list or change the columns, the OFFSET function code for the ComboBox.RowSource will also have to change.
openpdf.xlsm |
---|
|
---|
| A | B |
---|
1 | Sheet Name | PDF Folder |
---|
2 | sheet1 | C:\Users\forrest\Desktop\testpdf1\ |
---|
3 | sheet2 | C:\Users\forrest\Desktop\testpdf2\ |
---|
4 | ورقة3 | C:\Users\forrest\Desktop\testpdf3\ |
---|
|
---|
Then in the property dialog box enter the following for the RowSource property for your ComboBox
OFFSET(Config!$A$1,1,0,COUNTA(Config!$A:$A)-1,3)
This RowSource property entry does the following:
- looks for the non-blank entries (should be consecutive) in Column A
- counts the not-blank rows (minus 1 for the header row)
Also, the ComboBox.ColumnCount property is set to 2. This way both the Sheet name from Column A and the Folder from Column B are automatically loaded into the ComboBox.
No, code is necessary. When you added or delete Sheets that have related folders all you have to do is update the list on the Config sheet.
And your form initialization code is now like the following:
VBA Code:
Private Sub UserForm_Initialize()
Me.TextBox2.Value = c00
Init_ListBox
End Sub
Also, several Subs in UserForm2 need some minor modifications; replace them with the following:
added an event handler for TextBox2 change, so when this sub updates TextBox2 the change event fires
VBA Code:
Private Sub ComboBox1_Change()
On Error GoTo s
With ComboBox1
If .ListIndex <> -1 Then
ThisWorkbook.Sheets(ComboBox1.Text).Activate
TextBox2 = .List(.ListIndex, 1)
End If
End With
s:
End Sub
because changes to TextBox2 fires its Change event you do not want to update it multiple times in a Sub
I used tmp to get the folder name, trim it and add "\" before putting a value in TextBox2
VBA Code:
Private Sub CommandButton3_Click()
Dim tmp
Me.CommandButton3.Value = False
tmp = GetFolder(c00, "file name:")
If Len(Trim(tmp)) = 0 Then Exit Sub
Me.TextBox2.Value = TrailingSlash(tmp)
Init_ListBox
End Sub
new TextBox2 Change event handler
VBA Code:
Private Sub TextBox2_Change()
DisplayPDFFiles
End Sub
Private Sub UserForm_Initialize()
Me.TextBox2.Value = c00
Init_ListBox
End Sub
...
Added the ListBox.Clear here so that previous entries in the box are removed any time the folder changes
It was not clearing if there were not PDF files in a folder
VBA Code:
Private Sub DisplayPDFFiles()
If Me.TextBox2.Value = "" Then Exit Sub
Me.ListBox1.Clear
If Dir(Me.TextBox2.Value & "*.pdf") = "" Then Exit Sub
Init_ListBox
End Sub