VBA search for value twice - embedded if statement

katekoz

Board Regular
Joined
Jan 20, 2020
Messages
91
Office Version
  1. 2016
Platform
  1. Windows
Hello - I'm trying to have a macro search one list for a value and, if it doesn't find it there, to then search a different list. I'm having problems with the fact that I'm using "For Row" twice, and I get the error "For control variable already in use." Any suggestions on how to do this double-search?

SearchColumn = 4

lr = ThisWorkbook.Sheets("Overall User Data").UsedRange.Rows.Count + ThisWorkbook.Sheets("New Data Add").UsedRange.Row
sr = ThisWorkbook.Sheets("Overall User Data").UsedRange.Row
lr2 = ThisWorkbook.Sheets("New Data Add").UsedRange.Rows.Count + ThisWorkbook.Sheets("New Data Add").UsedRange.Row
sr2 = ThisWorkbook.Sheets("New Data Add").UsedRange.Row

For Row = lr To sr Step -1
If 0 = InStr(1, UserValue, ThisWorkbook.Sheets("Overall User Data").Cells(Row, SearchColumn)) Then

For Row = lr2 To sr2 Step -1
If 0 = InStr(1, UserValue, ThisWorkbook.Sheets("New Data Add").Cells(Row, SearchColumn)) Then

ans = MsgBox("This user is not found in the dataset and their values will be blank throughout the reports. Would you still like to add this user to the metrics?", vbYesNo)
End If
End If
Next Row
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
How about
VBA Code:
Sub katekoz()
   Dim Fnd As Range
   
   With ThisWorkbook.Sheets("Overall User Data").Columns(4)
      Set Fnd = .Find(UserValue, , , xlPart, , , False, , False)
   End With
   If Fnd Is Nothing Then
      With ThisWorkbook.Sheets("New Data Add").Columns(4)
         Set Fnd = .Find(UserValue, , , xlPart, , , False, , False)
      End With
   End If
   If Fnd Is Nothing Then
      ans = MsgBox("This user is not found in the dataset and their values will be blank throughout the reports. Would you still like to add this user to the metrics?", vbYesNo)
      'do something
   End If
End Sub
 
Upvote 0
That works, thanks! I didn't know about that "find" functionality, so I'll definitely look into that to use in the future. Thanks so much!!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,004
Members
452,374
Latest member
keccles

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