Page 1 of 1

คำสั่ง Range.Rows และ Range.Columns

Posted: Wed Dec 19, 2018 9:17 am
by Leng
สวัสดีครับผมอยากสอบถามเรื่องโค๊ดหน่อยครับ
คือผมต้องการพิมพ์ข้อมูลในTextbox 1 ใน sheet 1 ครับ เพื่อค้นหาว่าข้อมูล ใน Textbox 1 อยู่ใน Cell ไหนของ sheet 1 และให้ทำสีไว้ในช่องนั้น
เช่น
ถ้าผมพิมพ์คำว่า A ในTextbox1 ก็ให้ค้นหาว่า sheet 1 คำว่า A อยู่ตรงในใน Sheet 1 บ้างครับ และทำสีไว้

Code: Select all

Private Sub TextBox1_Change()
    Dim i As Integer
    Dim sel As Range
    Set sel = [a1:ak500]
    sel.Interior.ColorIndex = 0
    For i = 1 To sel.Rows.Count
        If sel.Rows(i).Cells(i).Value = [TextBox1].Value Then
            sel.Rows(i).Interior.ColorIndex = 40
        End If
    Next i
End Sub

Re: คำสั่ง Range.Rows และ Range.Columns

Posted: Wed Dec 19, 2018 10:40 am
by puriwutpokin
Leng wrote: Wed Dec 19, 2018 9:17 am สวัสดีครับผมอยากสอบถามเรื่องโค๊ดหน่อยครับ
คือผมต้องการพิมพ์ข้อมูลในTextbox 1 ใน sheet 1 ครับ เพื่อค้นหาว่าข้อมูล ใน Textbox 1 อยู่ใน Cell ไหนของ sheet 1 และให้ทำสีไว้ในช่องนั้น
เช่น
ถ้าผมพิมพ์คำว่า A ในTextbox1 ก็ให้ค้นหาว่า sheet 1 คำว่า A อยู่ตรงในใน Sheet 1 บ้างครับ และทำสีไว้

Code: Select all

Private Sub TextBox1_Change()
    Dim i As Integer
    Dim sel As Range
    Set sel = [a1:ak500]
    sel.Interior.ColorIndex = 0
    For i = 1 To sel.Rows.Count
        If sel.Rows(i).Cells(i).Value = [TextBox1].Value Then
            sel.Rows(i).Interior.ColorIndex = 40
        End If
    Next i
End Sub
ประมาณนี้หรือเปล่าครับ เข้าใจถูกหรือไม่ลองดูนะครับ

Code: Select all

Private Sub TextBox1_Change()
    Dim rng As Range, cell As Range
    Set rng = [a1:ak500]
    rng.Interior.ColorIndex = 0
For Each cell In rng
If cell.Value = [TextBox1].Value Then
cell.Interior.ColorIndex = 40
 End If
Next cell
End Sub

Re: คำสั่ง Range.Rows และ Range.Columns

Posted: Wed Dec 19, 2018 1:05 pm
by Leng
ได้แล้วครับแต่พอผมเพิ่ม ปุ่ม CommandButton1 เพื่อให้เป็นปุ่มคลิ๊กเพื่อค้นหาดันไม่ขึ้นครับต้องแกยังไงหรอครับ

Code: Select all

Private Sub CommandButton1_Click()
    Dim rng As Range, cell As Range
    Set rng = [a1:ak500]
    rng.Interior.ColorIndex = 0
For Each cell In rng
If cell.Value = TextBox1.Value Then
cell.Interior.ColorIndex = 40
 End If
Next cell
End Sub

Re: คำสั่ง Range.Rows และ Range.Columns

Posted: Wed Dec 19, 2018 2:47 pm
by puriwutpokin
Leng wrote: Wed Dec 19, 2018 1:05 pm ได้แล้วครับแต่พอผมเพิ่ม ปุ่ม CommandButton1 เพื่อให้เป็นปุ่มคลิ๊กเพื่อค้นหาดันไม่ขึ้นครับต้องแกยังไงหรอครับ

Code: Select all

Private Sub CommandButton1_Click()
    Dim rng As Range, cell As Range
    Set rng = [a1:ak500]
    rng.Interior.ColorIndex = 0
For Each cell In rng
If cell.Value = TextBox1.Value Then
cell.Interior.ColorIndex = 40
 End If
Next cell
End Sub
ก็รันได้ปกติ นะครับ เพียงแต่ว่า ตัวอักษร ที่ใส่เข้าไปต้องไม่มีช่องว่างครับ ซึ่งในไฟล์ตัวอย่างนั้น ตัว a ใน TextBox1 มีช่องว่างอยู่ด้านหน้าครับ

Re: คำสั่ง Range.Rows และ Range.Columns

Posted: Wed Dec 19, 2018 5:22 pm
by Leng
ทำไมพอใช้เป็นตัวเลขถึงไม่ขึ้นหรอครับ

Re: คำสั่ง Range.Rows และ Range.Columns

Posted: Wed Dec 19, 2018 8:22 pm
by puriwutpokin
Leng wrote: Wed Dec 19, 2018 5:22 pm ทำไมพอใช้เป็นตัวเลขถึงไม่ขึ้นหรอครับ
ปรับเป็น

Code: Select all

Private Sub CommandButton1_Click()
    Dim rng As Range, cell As Range
    Set rng = [a1:ak500]
    rng.Interior.ColorIndex = 0
For Each cell In rng
If cell.Value = TextBox1.Text Then
cell.Interior.ColorIndex = 40
 End If
Next cell
End Sub