Help for editing code in userform

azizls4

New Member
Joined
Aug 4, 2023
Messages
25
Platform
  1. Windows
hello
I have nice userform that I took from someone. It opens PDF files via userform. I want to modify some simple things in it.
When I put the combobox on sheet1, I want to be the path of PDF Folder C:\Users\Desktop\testpdf1\
and When I put the combobox on sheet2, I want to be the path of PDF Folder C:\Users\Desktop\testpdf2\
So that the PDF files appear in the listbox
thank you

excel userform
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I am sure about the path, only the path appears in the textbox, but the pdf files do not appear in listbox
i move it to userform_initialize but didnt work

I will try the code on another pc and let you know the result. Thank you for your help and patience
I found the problem
Your ComboBox event code did not have any code to display the PDF files when a ComboBox selection was made. Please make the following modifications and
Let me know if this fixes the problem.

In the ComboBox1_Change code

the last 4 lines of code should be:
VBA Code:
  End With
  DisplayPDFFiles
s:
End Sub

Modify TextBox1_Change event code to:
VBA Code:
Private Sub TextBox1_Change()
  DisplayPDFFiles
End Sub

Add the following Sub into UserForm2 (I made it the last sub)
this new sub creates a dir list of PDF for the path that is in TextBox2

VBA Code:
Private Sub DisplayPDFFiles()
  If Me.TextBox2.Value = "" Then Exit Sub
  If Dir(Me.TextBox2.Value & "*.pdf") = "" Then Exit Sub
  Init_ListBox
End Sub
 
Upvote 1
Solution
hello
I have nice userform that I took from someone. It opens PDF files via userform. I want to modify some simple things in it.
When I put the combobox on sheet1, I want to be the path of PDF Folder C:\Users\Desktop\testpdf1\
and When I put the combobox on sheet2, I want to be the path of PDF Folder C:\Users\Desktop\testpdf2\
So that the PDF files appear in the listbox
thank you

excel userform
Do you want the path names displayed? Where? TextBox2?
 
Upvote 0
Do you want the path names displayed? Where? TextBox2?
Yes, path display in TextBox 2, and PDF files shows in listbox
It means if sheet1 is active, The PDF files of path C:\Users\Desktop\testpdf1\ displayed in listbox
and if sheet2 is active, The PDF files of path C:\Users\Desktop\testpdf2\ displayed in listbox
So I view ( or open) pdf file from the listbox by Double click on it
 
Upvote 0
Yes, path display in TextBox 2, and PDF files shows in listbox
It means if sheet1 is active, The PDF files of path C:\Users\Desktop\testpdf1\ displayed in listbox
and if sheet2 is active, The PDF files of path C:\Users\Desktop\testpdf2\ displayed in listbox
So I view ( or open) pdf file from the listbox by Double click on it
What if the 3rd sheet is selected? does that change the path as well? Right now 3 Sheet names are listed in the ComboBox
 
Upvote 0
Try changing the following Sub(s) in your User Form code

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


and

VBA Code:
Private Sub UserForm_Activate()
  Dim ws As Worksheet
  Dim folder As String
  
  For Each ws In ThisWorkbook.Worksheets
    With ComboBox1
      .AddItem ws.Name
      Select Case LCase(ws.Name)
        Case "sheet1"
           folder = "testpdf1\"
        Case "sheet2"
          folder = "testpdf2\"
        Case Else
          folder = "testpdf3\"
      End Select
      .List(.ListCount - 1, 1) = "C:\Users\Desktop\" & folder
    End With
  Next ws
End Sub
 
Upvote 0
Try changing the following Sub(s) in your User Form code

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


and

VBA Code:
Private Sub UserForm_Activate()
  Dim ws As Worksheet
  Dim folder As String
 
  For Each ws In ThisWorkbook.Worksheets
    With ComboBox1
      .AddItem ws.Name
      Select Case LCase(ws.Name)
        Case "sheet1"
           folder = "testpdf1\"
        Case "sheet2"
          folder = "testpdf2\"
        Case Else
          folder = "testpdf3\"
      End Select
      .List(.ListCount - 1, 1) = "C:\Users\Desktop\" & folder
    End With
  Next ws
End Sub
It did not work,
when I change between the sheets, the path appears in the textbox only, but the pdf files do not appear in the listbox
 
Last edited:
Upvote 0
It did not work,
when I change between the sheets, the path appears in the textbox only, but the pdf files do not appear in the listbox
Do you have the following folder on your desktop? C:\Users\Desktop?
On my laptop I have C:\Users\username\Desktop ... where user name is the user's ID.
Please make sure the path name you gave me is correct.
 
Upvote 0
Do you have the following folder on your desktop? C:\Users\Desktop?
On my laptop I have C:\Users\username\Desktop ... where user name is the user's ID.
Please make sure the path name you gave me is
you are right
(C:\Users\Desktop\) this path is just an example
i change it with correct path on my laptop
I copied the correct path from the folder properties And put it in the code here
.List(.ListCount - 1, 1) = "C:\Users\Desktop\" & folder
But unfortunately it didn't work
 
Upvote 0

Forum statistics

Threads
1,215,694
Messages
6,126,254
Members
449,305
Latest member
Dalyb2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top