A
aintlifegrand79
My problem is that I have a code that searches for dat on multiple pages but
has multiple entries that maybe the same under the first search condition
therefore my code needs to check a secondary search condition but when my
code tries to do this on some pages it doesn't work. So here is how it
works, I have a userform (ufRepInfo) which has many text/check boxes on it
that are populated from 5 different worksheets (Sheet1 (Zip Codes
00000-19999), Sheet2 (Zip Codes 20000-39999), Sheet3 (Zip Codes 40000-59999),
Sheet4 (Zip Codes 60000-79999), Sheet5 (Zip Codes 80000-99999)). Also on the
userform (ufRepInfo) is a textbox (tbZipCode), a combobox (cbMarket), and a
commandbutton (cbFindButton). The goal is that when the user enters a zip
code into (tbZipCode) then chooses 1 of 5 markets in (cbMarket) and clicks
(cbFindButton) the code should first search for the zip code on the correct
sheet, there might be multiple entries for one zip code, then it should
search within those entries to find the entry for that zip code that has a
"x" in the row that corresponds to the selected market (cbMarket). My code
was working when I had just 2 worksheets but as I have gone along entering
data I have found that I had about 3 entries per zip code and needed put the
data on 5 worksheets. I have no problem populating the userform (ufRepInfo)
if their is only 1 entry for a zip code but when their is multiple entries it
doesn't populate. Hope this makes sense and thanks for your help. Here is
my code:
Private Sub cbFindButton_Click()
'Find Rep Info
Dim ws As Worksheet
If tbZipCode.Value < 20000 Then
Set ws = Sheet1
ElseIf tbZipCode.Value < 40000 Then
Set ws = Sheet2
ElseIf tbZipCode.Value < 60000 Then
Set ws = Sheet3
ElseIf tbZipCode.Value < 80000 Then
Set ws = Sheet4
ElseIf tbZipCode.Value >= 80000 Then
Set ws = Sheet5
End If
With ws
Select Case cbMarket
Case "Industrial Drives"
cbMarketCol = 18
Case "Municipal Drives (W&E)"
cbMarketCol = 19
Case "HVAC"
cbMarketCol = 20
Case "Electric Utility"
cbMarketCol = 21
Case "Oil and Gas"
cbMarketCol = 22
End Select
RowCount = 1
Do While .Range("A" & RowCount) <> ""
If .Range("A" & RowCount) = Val(tbZipCode.Value) And _
.Cells(RowCount, cbMarketCol) <> "" Then
Set Rep = .Range("A" & RowCount)
tbRepNumber.Value = Rep.Offset(0, 1).Value
tbRepName.Value = Rep.Offset(0, 2).Value
tbRepAddress.Value = Rep.Offset(0, 3).Value
tbRepState.Value = Rep.Offset(0, 4).Value
tbRepZipCode.Value = Rep.Offset(0, 5).Value
tbRepBusPhone.Value = Rep.Offset(0, 6).Value
tbRepCellPhone.Value = Rep.Offset(0, 7).Value
tbRepFax.Value = Rep.Offset(0, 8).Value
tbSAPNumber.Value = Rep.Offset(0, 9).Value
tbRegionalManager.Value = Rep.Offset(0, 10).Value
tbRMAddress.Value = Rep.Offset(0, 11).Value
tbRMState.Value = Rep.Offset(0, 12).Value
tbRMZipCode.Value = Rep.Offset(0, 13).Value
tbRMBusPhone.Value = Rep.Offset(0, 14).Value
tbRMCellPhone.Value = Rep.Offset(0, 15).Value
tbRMFax.Value = Rep.Offset(0, 16).Value
If Rep.Offset(0, 17).Value = "x" Then cbIndustrialDrives = True
If Rep.Offset(0, 18).Value = "x" Then cbMunicipalDrives = True
If Rep.Offset(0, 19).Value = "x" Then cbHVAC = True
If Rep.Offset(0, 20).Value = "x" Then cbElectricUtility = True
If Rep.Offset(0, 21).Value = "x" Then cbOilGas = True
If Rep.Offset(0, 22).Value = "x" Then cbMediumVoltage = True
If Rep.Offset(0, 23).Value = "x" Then cbLowVoltage = True
If Rep.Offset(0, 24).Value = "x" Then cbAfterMarket = True
tbInclusions.Value = Rep.Offset(0, 25).Value
tbExclusions.Value = Rep.Offset(0, 26).Value
End If
RowCount = RowCount + 1
Loop
End With
End Sub
has multiple entries that maybe the same under the first search condition
therefore my code needs to check a secondary search condition but when my
code tries to do this on some pages it doesn't work. So here is how it
works, I have a userform (ufRepInfo) which has many text/check boxes on it
that are populated from 5 different worksheets (Sheet1 (Zip Codes
00000-19999), Sheet2 (Zip Codes 20000-39999), Sheet3 (Zip Codes 40000-59999),
Sheet4 (Zip Codes 60000-79999), Sheet5 (Zip Codes 80000-99999)). Also on the
userform (ufRepInfo) is a textbox (tbZipCode), a combobox (cbMarket), and a
commandbutton (cbFindButton). The goal is that when the user enters a zip
code into (tbZipCode) then chooses 1 of 5 markets in (cbMarket) and clicks
(cbFindButton) the code should first search for the zip code on the correct
sheet, there might be multiple entries for one zip code, then it should
search within those entries to find the entry for that zip code that has a
"x" in the row that corresponds to the selected market (cbMarket). My code
was working when I had just 2 worksheets but as I have gone along entering
data I have found that I had about 3 entries per zip code and needed put the
data on 5 worksheets. I have no problem populating the userform (ufRepInfo)
if their is only 1 entry for a zip code but when their is multiple entries it
doesn't populate. Hope this makes sense and thanks for your help. Here is
my code:
Private Sub cbFindButton_Click()
'Find Rep Info
Dim ws As Worksheet
If tbZipCode.Value < 20000 Then
Set ws = Sheet1
ElseIf tbZipCode.Value < 40000 Then
Set ws = Sheet2
ElseIf tbZipCode.Value < 60000 Then
Set ws = Sheet3
ElseIf tbZipCode.Value < 80000 Then
Set ws = Sheet4
ElseIf tbZipCode.Value >= 80000 Then
Set ws = Sheet5
End If
With ws
Select Case cbMarket
Case "Industrial Drives"
cbMarketCol = 18
Case "Municipal Drives (W&E)"
cbMarketCol = 19
Case "HVAC"
cbMarketCol = 20
Case "Electric Utility"
cbMarketCol = 21
Case "Oil and Gas"
cbMarketCol = 22
End Select
RowCount = 1
Do While .Range("A" & RowCount) <> ""
If .Range("A" & RowCount) = Val(tbZipCode.Value) And _
.Cells(RowCount, cbMarketCol) <> "" Then
Set Rep = .Range("A" & RowCount)
tbRepNumber.Value = Rep.Offset(0, 1).Value
tbRepName.Value = Rep.Offset(0, 2).Value
tbRepAddress.Value = Rep.Offset(0, 3).Value
tbRepState.Value = Rep.Offset(0, 4).Value
tbRepZipCode.Value = Rep.Offset(0, 5).Value
tbRepBusPhone.Value = Rep.Offset(0, 6).Value
tbRepCellPhone.Value = Rep.Offset(0, 7).Value
tbRepFax.Value = Rep.Offset(0, 8).Value
tbSAPNumber.Value = Rep.Offset(0, 9).Value
tbRegionalManager.Value = Rep.Offset(0, 10).Value
tbRMAddress.Value = Rep.Offset(0, 11).Value
tbRMState.Value = Rep.Offset(0, 12).Value
tbRMZipCode.Value = Rep.Offset(0, 13).Value
tbRMBusPhone.Value = Rep.Offset(0, 14).Value
tbRMCellPhone.Value = Rep.Offset(0, 15).Value
tbRMFax.Value = Rep.Offset(0, 16).Value
If Rep.Offset(0, 17).Value = "x" Then cbIndustrialDrives = True
If Rep.Offset(0, 18).Value = "x" Then cbMunicipalDrives = True
If Rep.Offset(0, 19).Value = "x" Then cbHVAC = True
If Rep.Offset(0, 20).Value = "x" Then cbElectricUtility = True
If Rep.Offset(0, 21).Value = "x" Then cbOilGas = True
If Rep.Offset(0, 22).Value = "x" Then cbMediumVoltage = True
If Rep.Offset(0, 23).Value = "x" Then cbLowVoltage = True
If Rep.Offset(0, 24).Value = "x" Then cbAfterMarket = True
tbInclusions.Value = Rep.Offset(0, 25).Value
tbExclusions.Value = Rep.Offset(0, 26).Value
End If
RowCount = RowCount + 1
Loop
End With
End Sub