Page 1 of 1

สอบถามเรื่องการค้นหาข้อมูล format date ครับ

Posted: Thu Jun 07, 2012 3:34 pm
by phenocalypse
ตัวโมดูลที่ผมทำก็คือ FindCell2 เป็นการค้นหาข้อมูลใน Sheet Plan ที่ตรงกับเซลล์ C4 ใน Sheet Copy ซึ่งรับค่ามาจากโมดูล Calendar ปัญหาคือเมื่อถึงบรรทัด Set Rng... จะเกิด Error "Invalid Procedure Call..." ครับ

ตามไฟล์ข้างล่างครับ

Re: สอบถามเรื่องการค้นหาข้อมูล format date ครับ

Posted: Thu Jun 07, 2012 8:07 pm
by snasui
:D เนื่องจากการหาวัน เวลาด้วย .Find มีปัญหาเกียวกับ Fomat ที่เราจัดให้กับเซลล์เข้ามาเกี่ยวข้องครับ ลองดูตัวอย่างการค้นหาโดยการกำหนด Format เข้าไปช่วยตามด้านล่างครับ ซึ่งค่าที่เจอคือ A5 ในชีท Plan

Code: Select all

Sub FindCell2()
    Dim FindString As Date
    Dim Rng As Range
    FindString = Sheets("copy").Range("C4").Value
    FindString = DateSerial(Year(FindString) - 543, Month(FindString), Day(FindString))
    'FindString = CStr(FindString)
    If Trim(FindString) <> "" Then
        MsgBox FindString
        With Sheets("Plan").Range("A1:AF8")
            Set Rng = .Find(What:=Format(FindString, "d/m/yyyy"), _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                Application.Goto Rng, True
            Else
                MsgBox "Nothing found"
            End If
        End With
    Else
        MsgBox ("No Data!!!")
    End If
End Sub

Re: สอบถามเรื่องการค้นหาข้อมูล format date ครับ

Posted: Fri Jun 08, 2012 11:28 am
by phenocalypse
ขอถามต่อครับ ช่วยแนะแนวทางปรับ method find ให้ค้นหาข้อมูลชนิดวันที่ด้วยได้ไหมครับ

Re: สอบถามเรื่องการค้นหาข้อมูล format date ครับ

Posted: Fri Jun 08, 2012 7:28 pm
by snasui
:D ลองดูตัวอย่างตามด้านล่างครับ

Code: Select all

Sub FindCell2()
    Dim FindString As Date
    Dim Rng As Range
    FindString = Sheets("copy").Range("C4").Value
    If Trim(FindString) <> "" Then
        With Sheets("Plan").Range("A1:AF8")
            Set Rng = .Find(What:=DateValue(FindString), _
                LookIn:=xlFormulas)
            If Not Rng Is Nothing Then
                Application.Goto Rng, True
            Else
                MsgBox "Nothing found"
            End If
        End With
    Else
        MsgBox ("No Data!!!")
    End If
End Sub
ดูเพิ่มเติมที่นี่ครับ :arrow: http://www.cpearson.com/excel/DateTimeVBA.htm

Re: สอบถามเรื่องการค้นหาข้อมูล format date ครับ

Posted: Sun Jun 10, 2012 11:08 am
by phenocalypse
snasui wrote::D ลองดูตัวอย่างตามด้านล่างครับ

Code: Select all

Sub FindCell2()
    Dim FindString As Date
    Dim Rng As Range
    FindString = Sheets("copy").Range("C4").Value
    If Trim(FindString) <> "" Then
        With Sheets("Plan").Range("A1:AF8")
            Set Rng = .Find(What:=DateValue(FindString), _
                LookIn:=xlFormulas)
            If Not Rng Is Nothing Then
                Application.Goto Rng, True
            Else
                MsgBox "Nothing found"
            End If
        End With
    Else
        MsgBox ("No Data!!!")
    End If
End Sub
ดูเพิ่มเติมที่นี่ครับ :arrow: http://www.cpearson.com/excel/DateTimeVBA.htm
ขอบคุณมากครับ