How to have VBA to perform vlookups when command button is clicked

Zimmerman

Well-known Member
Joined
Jan 22, 2009
Messages
663
How can I get VBA to perform a vlookup in a range of cells. Right now I have this formulas in cells like below.

Cell A8 = IF(C8>"",VLOOKUP($A$7,'Employee Data'!$B$4:$D$27,2,0),"")
Cell A9 = IF(C9>"",VLOOKUP($A$7,'Employee Data'!$B$4:$D$27,2,0),"")
Cell A10 = IF(C10>"",VLOOKUP($A$7,'Employee Data'!$B$4:$D$27,2,0),"")
Cell A11 = IF(C11>"",VLOOKUP($A$7,'Employee Data'!$B$4:$D$27,2,0),"")
Cell A12 = IF(C12>"",VLOOKUP($A$7,'Employee Data'!$B$4:$D$27,2,0),"")
Cell A13 = IF(C13>"",VLOOKUP($A$7,'Employee Data'!$B$4:$D$27,2,0),"")
Cell A14 = IF(C14>"",VLOOKUP($A$7,'Employee Data'!$B$4:$D$27,2,0),"")
Cell A15 = IF(C15>"",VLOOKUP($A$7,'Employee Data'!$B$4:$D$27,2,0),"")

Cell A22 = IF(C22>"",VLOOKUP($A$21,'Employee Data'!$B$4:$D$27,2,0),"")
Cell A23 = IF(C23>"",VLOOKUP($A$21,'Employee Data'!$B$4:$D$27,2,0),"")
Cell A24 = IF(C24>"",VLOOKUP($A$21,'Employee Data'!$B$4:$D$27,2,0),"")
Cell A25 = IF(C25>"",VLOOKUP($A$21,'Employee Data'!$B$4:$D$27,2,0),"")
Cell A26 = IF(C26>"",VLOOKUP($A$21,'Employee Data'!$B$4:$D$27,2,0),"")
Cell A27 = IF(C27>"",VLOOKUP($A$21,'Employee Data'!$B$4:$D$27,2,0),"")
Cell A28 = IF(C28>"",VLOOKUP($A$21,'Employee Data'!$B$4:$D$27,2,0),"")
Cell A29 = IF(C29>"",VLOOKUP($A$21,'Employee Data'!$B$4:$D$27,2,0),"")

I'd like to be able to perform these lookups in VBA because the users start messing with and changing things. And these lookups would need to run when the user would click an OK command button on a userform.

Thanks in advance
Excel 2016
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
How about
Code:
Sub Zimmerman()
   Dim Cl As Range
   Dim Dic As Object
   
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Employee Data")
      For Each Cl In .Range("B4:B27")
         Dic.Item(Cl.Value) = Cl.Offset(, 1).Value
      Next Cl
   End With
   With Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")
      For Each Cl In .Range("C8:C15")
         If Cl.Value <> "" Then Cl.Offset(, -2).Value = Dic.Item(Range("A7").Value)
      Next Cl
      For Each Cl In .Range("C22:C29")
         If Cl.Value <> "" Then Cl.Offset(, -2).Value = Dic.Item(Range("A21").Value)
      Next Cl
   End With
End Sub
Change sheet name in red to suit
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Hate to ask another one of these but how would I incorporate the following formulas into VBA. Same concept as it has the same ranges.
Cell F8 =VLOOKUP(C8,'Pallet and Truss Data'!$A8:$F$400,5,0)
Cell F9 =VLOOKUP(C9,'Pallet and Truss Data'!$A8:$F$400,5,0)
Cell F10 =VLOOKUP(C10,'Pallet and Truss Data'!$A8:$F$400,5,0)
Cell F11 =VLOOKUP(C11,'Pallet and Truss Data'!$A8:$F$400,5,0)
Cell F12 =VLOOKUP(C12,'Pallet and Truss Data'!$A8:$F$400,5,0)
Cell F13 =VLOOKUP(C13,'Pallet and Truss Data'!$A8:$F$400,5,0)
Cell F14 =VLOOKUP(C14,'Pallet and Truss Data'!$A8:$F$400,5,0)
Cell F15 =VLOOKUP(C15,'Pallet and Truss Data'!$A8:$F$400,5,0)

Thanks again
 
Upvote 0
How about
Code:
Sub Zimmerman()
   Dim Cl As Range
   Dim Dic As Object
   
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Pallet and Truss Data")
      For Each Cl In .Range("A8:A400")
         Dic.Item(Cl.Value) = Cl.Offset(, 4).Value
      Next Cl
   End With
   With Sheets("Sheet1")
      For Each Cl In .Range("F8:F15")
         Cl.Value = Dic.Item(Cl.Offset(, -3).Value)
      Next Cl
   End With
End Sub
 
Upvote 0
My pleasure & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,478
Messages
6,125,040
Members
449,206
Latest member
Healthydogs

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