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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
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
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.
This works like it supposed to work on my laptop. Are you sure you have your path names defined correctly, and that you have PDF in the desired folders?


Move the following lines of code from the UserForm_Activate Sub
to UserForm_Initialize. Having this code that sets the values for the ComboBox executes after opening a PDF file.
When you return to the UserForm after viewing a PDF the sheets were added a 2nd time to the ComboBox.
By putting the Code in the UserForm_Initialize Sub the ComboBox is set up only once.

VBA Code:
  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\forrest\Desktop\" & folder
    End With
  Next ws
 
Upvote 0

Forum statistics

Threads
1,223,927
Messages
6,175,434
Members
452,641
Latest member
Arcaila

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