Page 1 of 1

สอบถามการอ้างอิงข้ามsheet ด้วย VBA

Posted: Fri Jun 24, 2022 11:37 pm
by thanadul0816
จากไฟล์และโค้ดนี้

Code: Select all

Sub cmdSave_Click()
    Dim r As Long
    If Application.CountIf(Range("s:s"), TextBox1.Text) > 0 Then
        MsgBox "รหัสนี้มีแล้ว!!!"
        TextBox1.Value = ""
               Exit Sub
                End If
   
    'If Application.CountIf(Range("b:b"), TextBox2.Text) > 0 Then
        'MsgBox "ชื่อซ้ำ!!!"
        'Exit Sub
    'End If
    With Sheets("Sheet1")
        r = .Range("d" & Rows.Count).End(xlUp).Row + 1
        .Cells(r, 4) = TextBox1.Text
            
      End With
End Sub
ในบรรทัดนี้

Code: Select all

 If Application.CountIf(Range("s:s"), TextBox1.Text) > 0 Then
เดิมผมเช็ครายการที่คอลัมน์ S:S ของ Sheet1
สิ่งที่ผมต้องการคือ ปรับโค้ดให้อ้างอิงจากคอลัมน์ S:S ของ Sheet2 ครับ

Re: สอบถามการอ้างอิงข้ามsheet ด้วย VBA

Posted: Sat Jun 25, 2022 6:43 am
by snasui
:D ปรับให้อ้างอิง Sheet2 เป็น If Application.CountIf(Worksheets("Sheet2").Range("b:b"), TextBox2.Text) > 0 Then ครับ

Re: สอบถามการอ้างอิงข้ามsheet ด้วย VBA

Posted: Tue Jun 28, 2022 9:49 pm
by thanadul0816
ได้ผลลัพธ์ตามต้องการครับ ขอบคุณครับ