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)
 

Some videos you may like

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,160
What do you want to do with the value in the cell from Column D?
 

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.
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,160
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?
 

MrThor

New Member
Joined
Aug 13, 2018
Messages
36

ADVERTISEMENT

Yes, thats correct :)
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,160
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
 

MrThor

New Member
Joined
Aug 13, 2018
Messages
36

ADVERTISEMENT

Thank you, I will try it out tommorow and then reply to you :)
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,160
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
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,594
Office Version
  1. 2007
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
 

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
 

Watch MrExcel Video

Forum statistics

Threads
1,123,259
Messages
5,600,575
Members
414,390
Latest member
plimbu

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