Getting used VBA on sheet transpond to a Form

Dedeke

Board Regular
Joined
Dec 1, 2020
Messages
70
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi there,

Having an issue getting vba code (wich works fine) into a UserForm.
The VBA code is running on the sheet included.
The used code is following ( Thx to some other forums where i found it)

VBA Code:
Private Function hdrs(Par1 As String, Par2 As String) As String
    Dim tbl6 As ListObject
    Dim rit As String
    Dim Lrow As Long
    
    Set tbl6 = ActiveSheet.ListObjects("Tabel6")
    Lrow = tbl6.Range.Rows.Count
    
    For Each cl In ActiveSheet.Range("D2:J" & Lrow)
        If cl.Value = Par2 And Cells(cl.Row, 2) = Par1 Then
            If InStr(1, rit, Cells(1, cl.Column).Value) = 0 Then
                rit = rit & Cells(1, cl.Column).Value & ", "
            End If
        End If
    Next cl
    If rit <> "" Then hdrs = Left(rit, Len(rit) - 2)
End Function

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B18:C18")) Is Nothing Then
        If Range("B18") <> "" And Range("C18") <> "" Then
            Range("D17") = hdrs(Range("B18"), Range("C18"))
        End If
    End If
End Sub
Code:
I would like to create on "Blad2" a userform which has the same result.
Unfortunally i don't have a lot experience comes to VBA.
i already prepaired the form a bit but can't get some things working
Made a print screen also.
So in the dropdown make the same choices and get the results under it.
hope it make sense.
Don't know how to get the complete file uploaded and if this is allowed.
Hope someone can help me out.




Fiteren ritten (1).xlsm
F
24
Blad1
 

Attachments

  • Form example.JPG
    Form example.JPG
    87.7 KB · Views: 9

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Solved with the help from: https://chat.openai.com/chat

After some questions, in Dutch i got this code. some small changes in names and it worked fine.
This code solved my problem

VBA Code:
Private Sub CommandButton1_Click()
    Dim searchValue1 As String
    Dim searchValue2 As String
    Dim resultHeaders As String
    Dim foundCell As Range
    
    'Lees de zoekwaarden uit de comboboxen in het Userform1
    searchValue1 = UserForm1.ComboBox1.Value
    searchValue2 = UserForm1.ComboBox2.Value
    
    'Zoek de waarde uit de eerste combobox in de tabel op blad1
    Set foundCell = Sheets("blad1").ListObjects("Tabel3").Range.Columns(1).Find(What:=searchValue1, LookIn:=xlValues, LookAt:=xlWhole)
    
    'Controleer of de cel is gevonden
    If Not foundCell Is Nothing Then
        'Zoek de waarde uit de tweede combobox in de rij waarin de eerste waarde is gevonden
        For Each cell In foundCell.EntireRow.Cells
            If cell.Value = searchValue2 Then
                'Voeg de header van de cel toe aan de resultHeaders
                resultHeaders = resultHeaders & Sheets("blad1").ListObjects("Tabel3").HeaderRowRange.Cells(cell.Column).Value & ", "
            End If
        Next cell
        
        'Verwijder de laatste komma en spatie uit de resultHeaders
        resultHeaders = Left(resultHeaders, Len(resultHeaders) - 2)
        
        'Toon de resultHeaders in de textbox op het Userform1
        UserForm1.TextBox4.Value = resultHeaders
    Else
        'Als de cel niet is gevonden, geef dan een foutmelding weer
        MsgBox "De waarde " & searchValue1 & " kon niet worden gevonden in de tabel op blad1."
    End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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