Page 1 of 1

สอบถามเรื่องการ ทำ Auto id VBA ครับ

Posted: Fri Feb 23, 2018 10:56 am
by jullathep
คือผมอยากให้ เลขที่อ้างอิง ใน Sheet database มันมาแสดงบน Label ของ Sheet1 ทุกครั้งที่เวลาเรากด ตกลง
เช่น ผมกด ตกลง เลขที่อ้างอิงจะเป็น CHM 610011 อยากให้มันไปแสดงที่ Label1 ใน Sheet1 ทุกครั้งที่กดตกลง ผมจะเขียน Code อย่างไรครับ Code ตั้งต้นมาได้แบบนี้ครับ
<Sheet1>

Code: Select all

Private Sub CommandButton1_Click()
Dim ssheet As Worksheet
    Set ssheet = ThisWorkbook.Sheets("database")
    nr = ssheet.Cells(Rows.Count, 1).End(xlUp).Row + 1
ssheet.Cells(nr, 2) = Me.TextBox1
End Sub
<Sheet2 "database">

Code: Select all

Private Sub Worksheet_Change(ByVal Target As Range)
 Dim Cell As Range
  Application.EnableEvents = False
  For Each Cell In Target
    If Cell.Column = 2 Then
      Cell.Offset(, 5).Value = Now
End If
 If Cell.Column = 2 Then
      Cell.Offset(, -1).Value = "CHM  " & ((Year(Now) + 543 - 2500) * 10000) + (Range("A" & Rows.Count).End(xlUp).Row)
End If
Next Cell
Application.EnableEvents = True
End Sub
รบกวนพี่ๆ อาจารย์ทุกท่านด้วยครับ

Re: สอบถามเรื่องการ ทำ Auto id VBA ครับ

Posted: Fri Feb 23, 2018 4:09 pm
by puriwutpokin
jullathep wrote: Fri Feb 23, 2018 10:56 am คือผมอยากให้ เลขที่อ้างอิง ใน Sheet database มันมาแสดงบน Label ของ Sheet1 ทุกครั้งที่เวลาเรากด ตกลง
เช่น ผมกด ตกลง เลขที่อ้างอิงจะเป็น CHM 610011 อยากให้มันไปแสดงที่ Label1 ใน Sheet1 ทุกครั้งที่กดตกลง ผมจะเขียน Code อย่างไรครับ Code ตั้งต้นมาได้แบบนี้ครับ
<Sheet1>

Code: Select all

Private Sub CommandButton1_Click()
Dim ssheet As Worksheet
    Set ssheet = ThisWorkbook.Sheets("database")
    nr = ssheet.Cells(Rows.Count, 1).End(xlUp).Row + 1
ssheet.Cells(nr, 2) = Me.TextBox1
End Sub
<Sheet2 "database">

Code: Select all

Private Sub Worksheet_Change(ByVal Target As Range)
 Dim Cell As Range
  Application.EnableEvents = False
  For Each Cell In Target
    If Cell.Column = 2 Then
      Cell.Offset(, 5).Value = Now
End If
 If Cell.Column = 2 Then
      Cell.Offset(, -1).Value = "CHM  " & ((Year(Now) + 543 - 2500) * 10000) + (Range("A" & Rows.Count).End(xlUp).Row)
End If
Next Cell
Application.EnableEvents = True
End Sub
รบกวนพี่ๆ อาจารย์ทุกท่านด้วยครับ
ลองปรับดูว่าใข่ไหมครับ

Code: Select all

Private Sub CommandButton1_Click()
Dim c As Range
    For Each c In Sheet3.Range("b2:b100").Cells
        If TextBox1.Text = c.Value Then
        Label1 = c.Offset(, -1).Value
        End If
    Next c
End Sub

Re: สอบถามเรื่องการ ทำ Auto id VBA ครับ

Posted: Fri Feb 23, 2018 4:18 pm
by jullathep
puriwutpokin wrote: Fri Feb 23, 2018 4:09 pm
jullathep wrote: Fri Feb 23, 2018 10:56 am คือผมอยากให้ เลขที่อ้างอิง ใน Sheet database มันมาแสดงบน Label ของ Sheet1 ทุกครั้งที่เวลาเรากด ตกลง
เช่น ผมกด ตกลง เลขที่อ้างอิงจะเป็น CHM 610011 อยากให้มันไปแสดงที่ Label1 ใน Sheet1 ทุกครั้งที่กดตกลง ผมจะเขียน Code อย่างไรครับ Code ตั้งต้นมาได้แบบนี้ครับ
<Sheet1>

Code: Select all

Private Sub CommandButton1_Click()
Dim ssheet As Worksheet
    Set ssheet = ThisWorkbook.Sheets("database")
    nr = ssheet.Cells(Rows.Count, 1).End(xlUp).Row + 1
ssheet.Cells(nr, 2) = Me.TextBox1
End Sub
<Sheet2 "database">

Code: Select all

Private Sub Worksheet_Change(ByVal Target As Range)
 Dim Cell As Range
  Application.EnableEvents = False
  For Each Cell In Target
    If Cell.Column = 2 Then
      Cell.Offset(, 5).Value = Now
End If
 If Cell.Column = 2 Then
      Cell.Offset(, -1).Value = "CHM  " & ((Year(Now) + 543 - 2500) * 10000) + (Range("A" & Rows.Count).End(xlUp).Row)
End If
Next Cell
Application.EnableEvents = True
End Sub
รบกวนพี่ๆ อาจารย์ทุกท่านด้วยครับ
ลองปรับดูว่าใข่ไหมครับ

Code: Select all

Private Sub CommandButton1_Click()
Dim c As Range
    For Each c In Sheet3.Range("b2:b100").Cells
        If TextBox1.Text = c.Value Then
        Label1 = c.Offset(, -1).Value
        End If
    Next c
End Sub
ขอบคุณครับ เดี๋ยวลองเทส

Re: สอบถามเรื่องการ ทำ Auto id VBA ครับ

Posted: Fri Feb 23, 2018 4:21 pm
by jullathep
jullathep wrote: Fri Feb 23, 2018 4:18 pm
puriwutpokin wrote: Fri Feb 23, 2018 4:09 pm
jullathep wrote: Fri Feb 23, 2018 10:56 am คือผมอยากให้ เลขที่อ้างอิง ใน Sheet database มันมาแสดงบน Label ของ Sheet1 ทุกครั้งที่เวลาเรากด ตกลง
เช่น ผมกด ตกลง เลขที่อ้างอิงจะเป็น CHM 610011 อยากให้มันไปแสดงที่ Label1 ใน Sheet1 ทุกครั้งที่กดตกลง ผมจะเขียน Code อย่างไรครับ Code ตั้งต้นมาได้แบบนี้ครับ
<Sheet1>

Code: Select all

Private Sub CommandButton1_Click()
Dim ssheet As Worksheet
    Set ssheet = ThisWorkbook.Sheets("database")
    nr = ssheet.Cells(Rows.Count, 1).End(xlUp).Row + 1
ssheet.Cells(nr, 2) = Me.TextBox1
End Sub
<Sheet2 "database">

Code: Select all

Private Sub Worksheet_Change(ByVal Target As Range)
 Dim Cell As Range
  Application.EnableEvents = False
  For Each Cell In Target
    If Cell.Column = 2 Then
      Cell.Offset(, 5).Value = Now
End If
 If Cell.Column = 2 Then
      Cell.Offset(, -1).Value = "CHM  " & ((Year(Now) + 543 - 2500) * 10000) + (Range("A" & Rows.Count).End(xlUp).Row)
End If
Next Cell
Application.EnableEvents = True
End Sub
รบกวนพี่ๆ อาจารย์ทุกท่านด้วยครับ
ลองปรับดูว่าใข่ไหมครับ

Code: Select all

Private Sub CommandButton1_Click()
Dim c As Range
    For Each c In Sheet3.Range("b2:b100").Cells
        If TextBox1.Text = c.Value Then
        Label1 = c.Offset(, -1).Value
        End If
    Next c
End Sub
ขอบคุณครับ เดี๋ยวลองเทส
หลังจากลองปรับแล้วไม่มีอะไรเกิดขึ้นเลยครับ พี่

Re: สอบถามเรื่องการ ทำ Auto id VBA ครับ

Posted: Fri Feb 23, 2018 4:22 pm
by jullathep
jullathep wrote: Fri Feb 23, 2018 4:21 pm
jullathep wrote: Fri Feb 23, 2018 4:18 pm
puriwutpokin wrote: Fri Feb 23, 2018 4:09 pm
ลองปรับดูว่าใข่ไหมครับ

Code: Select all

Private Sub CommandButton1_Click()
Dim c As Range
    For Each c In Sheet3.Range("b2:b100").Cells
        If TextBox1.Text = c.Value Then
        Label1 = c.Offset(, -1).Value
        End If
    Next c
End Sub
ขอบคุณครับ เดี๋ยวลองเทส
หลังจากลองปรับแล้วไม่มีอะไรเกิดขึ้นเลยครับ พี่
ขออภัยครับได้แล้วครับ เมื่อกี้พลาดนิดหน่อยครับ ขอบคุณมากครับ