Forum Discussion
VBA Find (What:="") tied to dropdown list box
- May 02, 2022
Hi Fred,
I see the mistake that I made. I hope that the attached version works correctly. You'll notice that there is no Teams dropdown anymore. It isn't needed because the macro loops through all teams.
You only need to select a week, then click the button.
I ran into a couple of inconsistencies:
- There were some teams with two consecutive spaces on the second sheet; I changed those to single spaces.
- The Washington team name was different between the two sheets; I corrected that.
The macro in the attached workbook loops through the teams and fills the column for the selected week.
It would also be possible to loop through the weeks if desired.
I opened the above file but code was the same as before. There's no loop in it. Maybe it didn't save. I hope you still have it an can repost. If it doesn't save maybe you could copy it to the post and I'll copy it to my worksheet.
Thanks for all you time and work. Have a great weekend.
- HansVogelaarApr 30, 2022MVP
Sorry if I messed it up. Here is the modified version:
Sub Find_TeamName_Week_Drives() Dim ws As Worksheet Dim wt As Worksheet Dim r As Long Dim m As Long Dim tm As String Dim wk As String Dim dr As String Dim rg As Range Dim n As Long Application.ScreenUpdating = False Set ws = Worksheets("DRIVEWORKSHEET") Set wt = Worksheets("DROPDOWNLISTBOX") m = wt.Range("I3").End(xlDown).Row For r = 4 To m ' Team name tm = wt.Range("I" & r).Value ' Find team Set rg = ws.Range("B:D").Find(What:=tm, LookAt:=xlPart, MatchCase:=False) If Not rg Is Nothing Then ' Week wk = wt.Range("D2").Value ' Find week Set rg = rg.Offset(1).EntireRow.Find(What:=wk, LookAt:=xlWhole, MatchCase:=False) If Not rg Is Nothing Then ' Drive dr = wt.Range("F2").Value ' Find drive Set rg = rg.Offset(0, -3) Set rg = rg.EntireColumn.Find(What:=dr, After:=rg, LookAt:=xlWhole, MatchCase:=False) If Not rg Is Nothing Then ' Get value n = rg.Offset(5).End(xlDown).Value ' Find week on DROPDOWNLISTBOX Set rg = wt.Range("3:3").Find(What:=wk, LookAt:=xlWhole, MatchCase:=False) If Not rg Is Nothing Then ' Populate cell wt.Cells(r, rg.Column).Value = n End If End If End If End If Next r Application.ScreenUpdating = True End Sub
I have attached the workbook.
- fredc2022May 01, 2022Brass Contributor
Thanks you taking the time to repost your macro. And please, don't be sorry about anything. I just appreciate the help. As you can tell,
I lack the knowledge to understand, let alone to write a competent macro.I have gone through the "Drive" worksheet and have corrected the offset spacing for the "Weeks" to be -3 on all. I don't think that would change
anything as it was for weeks 7, 8 and 12.I have ran the macro using the F8 but was unable to see where each step was in the worksheet. In the simple macros I make I'm able to see
each step.When I run the macro using Arizona Cardinals in the dropdown list box (B2) values are placed in Kansas City and Los Angeles Chargers "Weeks"
columns "L". I'm not sure why that is happening. Looking at those two teams drive numbers they do not match the actual numbers.Is there a way to watch, such as "step in" to see what the macro is doing?
- HansVogelaarMay 01, 2022MVP
Press Alt+F11 to activate the Visual Basic Editor.
If you don't see the code immediately, click the + buttons in the tree view at the left until you see Module1, then double-click Module1.
Click anywhere in the macro code.
Each time you press F8, one step will be executed.
You can view the value of the variables used in the code by hovering the mouse pointer over them.