Need help using data from UserForm

joshhud

New Member
Joined
Apr 12, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi all, multi part question. I have a UserForm, where the user selects 1 or more of 52 fields and i need those fields pasted as headers on another file. Originally i accomplished this with the code below, but i fee like there is a better way.

Private Sub CommandButton1_Click()
Dim index As Integer
Dim cell As Range
Set ls = ActiveWorkbook.Worksheets("List")
Set ASR = ActiveWorkbook.Worksheets("Combined ASR")
Set cell = ls.Range("C1")
For index = 0 To UserForm1.ListBox1.ListCount - 1
If UserForm1.ListBox1.Selected(index) Then
cell.Value = UserForm1.ListBox1.List(index)
Set cell = cell.Offset(1)
End If
Next



Unload UserForm1
Worksheets("List").Range("C1", Cells(Rows.Count, "C").End(xlUp)).Copy
Worksheets("Combined ASR").Range("A1").PasteSpecial Transpose:=True

End Sub

Secondly, i need to perform a vlookup to another dataset based on the fields they select. Currently i cannot think of a way to dynamically have it identify which field was selected and perform the vlookup from there. My only thought is to do something like this. But again i feel like there is a better way since field BA could be any of the available 52 fields. ( i only included 7 of the 52 fields below as an example).

Sub fields()

Set ASR = ActiveWorkbook.Worksheets("Combined ASR")

Dim F1 As String
Dim F2 As String
Dim F3 As String
Dim F4 As String
Dim F5 As String
Dim F6 As String
Dim F7 As String

F1 = "=VLOOKUP('Combined ASR'!A2,HR_Data!A:BA,2,0)"
F2 = "=VLOOKUP('Combined ASR'!A2,HR_Data!A:BA,3,0)"
F3 = "=VLOOKUP('Combined ASR'!A2,HR_Data!A:BA,4,0)"
F4 = "=VLOOKUP('Combined ASR'!A2,HR_Data!A:BA,5,0)"
F5 = "=VLOOKUP('Combined ASR'!A2,HR_Data!A:BA,6,0)"
F6 = "=VLOOKUP('Combined ASR'!A2,HR_Data!A:BA,7,0)"
F7 = "=VLOOKUP('Combined ASR'!A2,HR_Data!A:BA,8,0)"

If ASR.Range("BA1") = "EMP_Status_CD" Then
ASR.Range("BA2") = F1
ElseIf ASR.Range("BA1") = "EMP_DEPARTMENT_ID" Then
ASR.Range("BA2") = F2
ElseIf ASR.Range("BA1") = "EMP_DEPARTMENT_NAME" Then
ASR.Range("BA2") = F3
ElseIf ASR.Range("BA1") = "LEVEL5_ID" Then
ASR.Range("BA2") = F4
ElseIf ASR.Range("BA1") = "LEVEL5_NAME" Then
ASR.Range("BA2") = F5
ElseIf ASR.Range("BA1") = "LEVEL6_ID" Then
ASR.Range("BA2") = F6
Else
ASR.Range("BA1") = "LEVEL6_NAME"
ASR.Range("BA2") = F7
End If

End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Sorry should have read FAQ first

VBA Code:
Private Sub CommandButton1_Click()
Dim index As Integer
Dim cell As Range
Set ls = ActiveWorkbook.Worksheets("List")
Set ASR = ActiveWorkbook.Worksheets("Combined ASR")
Set cell = ls.Range("C1")
For index = 0 To UserForm1.ListBox1.ListCount - 1
If UserForm1.ListBox1.Selected(index) Then
cell.Value = UserForm1.ListBox1.List(index)
Set cell = cell.Offset(1)
End If
Next



Unload UserForm1
Worksheets("List").Range("C1", Cells(Rows.Count, "C").End(xlUp)).Copy
Worksheets("Combined ASR").Range("A1").PasteSpecial Transpose:=True

End Sub

VBA Code:
Sub fields()

Set ASR = ActiveWorkbook.Worksheets("Combined ASR")

Dim F1 As String
Dim F2 As String
Dim F3 As String
Dim F4 As String
Dim F5 As String
Dim F6 As String
Dim F7 As String

F1 = "=VLOOKUP('Combined ASR'!A2,HR_Data!A:BA,2,0)"
F2 = "=VLOOKUP('Combined ASR'!A2,HR_Data!A:BA,3,0)"
F3 = "=VLOOKUP('Combined ASR'!A2,HR_Data!A:BA,4,0)"
F4 = "=VLOOKUP('Combined ASR'!A2,HR_Data!A:BA,5,0)"
F5 = "=VLOOKUP('Combined ASR'!A2,HR_Data!A:BA,6,0)"
F6 = "=VLOOKUP('Combined ASR'!A2,HR_Data!A:BA,7,0)"
F7 = "=VLOOKUP('Combined ASR'!A2,HR_Data!A:BA,8,0)"

If ASR.Range("BA1") = "EMP_Status_CD" Then
ASR.Range("BA2") = F1
ElseIf ASR.Range("BA1") = "EMP_DEPARTMENT_ID" Then
ASR.Range("BA2") = F2
ElseIf ASR.Range("BA1") = "EMP_DEPARTMENT_NAME" Then
ASR.Range("BA2") = F3
ElseIf ASR.Range("BA1") = "LEVEL5_ID" Then
ASR.Range("BA2") = F4
ElseIf ASR.Range("BA1") = "LEVEL5_NAME" Then
ASR.Range("BA2") = F5
ElseIf ASR.Range("BA1") = "LEVEL6_ID" Then
ASR.Range("BA2") = F6
Else
ASR.Range("BA1") = "LEVEL6_NAME"
ASR.Range("BA2") = F7
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,103
Members
452,379
Latest member
IainTru

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