Find cell value

MrThor

New Member
Joined
Aug 13, 2018
Messages
36
Hi

I want to find a specific cell on the same row as three different combobox values. So I want to find the row where column A is depending on combobox 1 column B on combobox 2 and C on combobox 3 and then I want the cell from Column D on the same row as those values. I have the following code which just gives me the cell depending on the first combobox which is not correct in the end:

Code:
Dim rng1 As Range
Dim year As String


Dim rng2 As Range
Dim month As String


Dim rng3 As Range
Dim day As String


Dim rownumber As Long


year = ComboBox1.Value
month = ComboBox2.Value
day = ComboBox3.Value




Set rng1 = Sheets("Sheet2").Columns("A:A").Find(What:=year, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
Set rng2 = Sheets("Sheet2").Columns("B:B").Find(What:=month, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
Set rng3 = Sheets("Sheet2").Columns("C:C").Find(What:=day, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)


rownumber = rng1.Row


Sheets("Sheet1").Cells(1, 1) = Sheets("Sheet2").Cells(rownumber, 4)
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
12,814
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
What do you want to do with the value in the cell from Column D?
 
Upvote 0

MrThor

New Member
Joined
Aug 13, 2018
Messages
36
as the code says it will appear in cell A1 in sheet 1

Code:
[COLOR=#333333]Sheets("Sheet1").Cells(1, 1) = Sheets("Sheet2").Cells(rownumber, 4)[/COLOR]

But for now the number only depends on the value from combobox1 and not the other two comboboxes. "Rownumber" should include rng1, rng2 and rng3, but I do not know how to do it.
 
Upvote 0

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
12,814
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
Let's see if I understood correctly. In sheet2 you have the year in column A, month in column B and day in column C. You want to find the row where those 3 values taken from the 3 combo boxes, are in the same row in Sheet2. Then you want column D from that row to be copied to A1 in Sheet1. Is this correct?
 
Upvote 0

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
12,814
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
Untested.
Code:
Sub MrThor()
    Application.ScreenUpdating = False
    Dim Val As String, sDate As String, i As Long, v1, srcWS As Worksheet
    Set srcWS = Sheets("Sheet2")
    sDate = ComboBox1.Value & "|" & ComboBox2.Value & "|" & ComboBox3.Value
    v1 = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 3).Value
    For i = 1 To UBound(v1, 1)
        Val = v1(i, 1) & "|" & v1(i, 2) & "|" & v1(i, 3)
        If Val = sDate Then
            Sheets("Sheet1").Cells(1, 1) = v1(i, 4)
        End If
        Exit For
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

MrThor

New Member
Joined
Aug 13, 2018
Messages
36
ADVERTISEMENT
Thank you, I will try it out tommorow and then reply to you :)
 
Upvote 0

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
12,814
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
One slight modification:
Code:
Sub MrThor()
    Application.ScreenUpdating = False
    Dim Val As String, sDate As String, i As Long, v1, srcWS As Worksheet
    Set srcWS = Sheets("Sheet2")
    sDate = ComboBox1.Value & "|" & ComboBox2.Value & "|" & ComboBox3.Value
    v1 = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 4).Value
    For i = 1 To UBound(v1, 1)
        Val = v1(i, 1) & "|" & v1(i, 2) & "|" & v1(i, 3)
        If Val = sDate Then
            Sheets("Sheet1").Cells(1, 1) = v1(i, 4)
        End If
        Exit For
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
16,613
Office Version
  1. 2013
Platform
  1. Windows
Following with the Find method:

Code:
Private Sub CommandButton1_Click()
    Dim r As Range, b As Range, celda As String, cad As String
    Set r = Sheets("Sheet2").Columns("A")
    Set b = r.Find(Val(ComboBox1.Value), LookAt:=xlWhole)
    If Not b Is Nothing Then
        celda = b.Address
        Do
            If b.Offset(0, 1).Value = Val(ComboBox2.Value) And b.Offset(0, 2).Value = Val(ComboBox3.Value) Then
                cad = b.Offset(0, 3).Value
                Sheets("Sheet1").Range("A1").Value = cad
                MsgBox "Found cell value: " & cad
                Exit Do
            End If
            Set b = r.FindNext(b)
        Loop While Not b Is Nothing And b.Address <> celda
    End If
    If cad = "" Then MsgBox "There is no information", vbExclamation
End Sub
 
Upvote 0

MrThor

New Member
Joined
Aug 13, 2018
Messages
36
Hey,

I have tested your code, the problem is that the code only search the "A2" row. So the comboboxes has to match the values for row 2, in other case it doesnt work :/ (see the red part below)

Code:
v1 = srcWS.Range("[COLOR=#ff0000]A2[/COLOR]", srcWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 4).Value
 
Upvote 0

Forum statistics

Threads
1,195,942
Messages
6,012,428
Members
441,699
Latest member
tnlbado

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
Top