Page 1 of 1

ขออนุญาตสอบถาม Code VBA บันทึกข้อมูล

Posted: Thu Apr 12, 2018 10:46 am
by jullathep

Code: Select all

Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = Worksheets("DataAdd")
emptyrow = WorksheetFunction.CountA(ws.Range("C:C")) + 1
ws.Cells(emptyrow, 1).Value = Sheet3.Range("A" & Rows.Count).End(xlUp)
ws.Cells(emptyrow, 2).Value = Sheet3.Label5.Caption
ws.Cells(emptyrow, 3).Value = Sheet3.Range("B" & Rows.Count).End(xlUp)
ws.Cells(emptyrow, 4).Value = Sheet3.Range("F" & Rows.Count).End(xlUp)
End Sub
ขอสอบถามครับ ผมมีรายการใน Sheet Report อยู่ ผมกดบันทึกให้มันส่งค่าใน คอลัม รายการไปบันทึกใน Sheet DataAdd
ปรากฏว่า มันรายการที่ 2 แต่รายการที่ 1 ไม่มาด้วยผมจะแก้ไข้ Code อย่างไรครับ
Sheet Report & DataAdd

Re: ขออนุญาตสอบถาม Code VBA บันทึกข้อมูล

Posted: Thu Apr 12, 2018 7:49 pm
by puriwutpokin
jullathep wrote: Thu Apr 12, 2018 10:46 am

Code: Select all

Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = Worksheets("DataAdd")
emptyrow = WorksheetFunction.CountA(ws.Range("C:C")) + 1
ws.Cells(emptyrow, 1).Value = Sheet3.Range("A" & Rows.Count).End(xlUp)
ws.Cells(emptyrow, 2).Value = Sheet3.Label5.Caption
ws.Cells(emptyrow, 3).Value = Sheet3.Range("B" & Rows.Count).End(xlUp)
ws.Cells(emptyrow, 4).Value = Sheet3.Range("F" & Rows.Count).End(xlUp)
End Sub
ขอสอบถามครับ ผมมีรายการใน Sheet Report อยู่ ผมกดบันทึกให้มันส่งค่าใน คอลัม รายการไปบันทึกใน Sheet DataAdd
ปรากฏว่า มันรายการที่ 2 แต่รายการที่ 1 ไม่มาด้วยผมจะแก้ไข้ Code อย่างไรครับ
Sheet Report & DataAdd
ลองปรับเป็น

Code: Select all

Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim Lrow  As Integer
Set ws = Worksheets("DataAdd")
Lrow = Sheet3.Range("A" & Sheet3.Rows.Count).End(xlUp).Row
emptyrow = WorksheetFunction.CountA(ws.Range("C:C")) + 1
Sheet3.Range("A10:A" & Lrow).Copy: ws.Cells(emptyrow, 1).PasteSpecial xlPasteValues
ws.Cells(emptyrow, 1).Offset(, 1).Resize(Lrow - 9, 1) = Sheet3.Label5.Caption
 Sheet3.Range("B10:B" & Lrow).Copy: ws.Cells(emptyrow, 3).PasteSpecial xlPasteValues
 Sheet3.Range("F10:F" & Lrow).Copy: ws.Cells(emptyrow, 4).PasteSpecial xlPasteValues
End Sub

Re: ขออนุญาตสอบถาม Code VBA บันทึกข้อมูล

Posted: Wed Apr 18, 2018 8:00 am
by jullathep
puriwutpokin wrote: Thu Apr 12, 2018 7:49 pm
jullathep wrote: Thu Apr 12, 2018 10:46 am

Code: Select all

Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = Worksheets("DataAdd")
emptyrow = WorksheetFunction.CountA(ws.Range("C:C")) + 1
ws.Cells(emptyrow, 1).Value = Sheet3.Range("A" & Rows.Count).End(xlUp)
ws.Cells(emptyrow, 2).Value = Sheet3.Label5.Caption
ws.Cells(emptyrow, 3).Value = Sheet3.Range("B" & Rows.Count).End(xlUp)
ws.Cells(emptyrow, 4).Value = Sheet3.Range("F" & Rows.Count).End(xlUp)
End Sub
ขอสอบถามครับ ผมมีรายการใน Sheet Report อยู่ ผมกดบันทึกให้มันส่งค่าใน คอลัม รายการไปบันทึกใน Sheet DataAdd
ปรากฏว่า มันรายการที่ 2 แต่รายการที่ 1 ไม่มาด้วยผมจะแก้ไข้ Code อย่างไรครับ
Sheet Report & DataAdd
ลองปรับเป็น

Code: Select all

Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim Lrow  As Integer
Set ws = Worksheets("DataAdd")
Lrow = Sheet3.Range("A" & Sheet3.Rows.Count).End(xlUp).Row
emptyrow = WorksheetFunction.CountA(ws.Range("C:C")) + 1
Sheet3.Range("A10:A" & Lrow).Copy: ws.Cells(emptyrow, 1).PasteSpecial xlPasteValues
ws.Cells(emptyrow, 1).Offset(, 1).Resize(Lrow - 9, 1) = Sheet3.Label5.Caption
 Sheet3.Range("B10:B" & Lrow).Copy: ws.Cells(emptyrow, 3).PasteSpecial xlPasteValues
 Sheet3.Range("F10:F" & Lrow).Copy: ws.Cells(emptyrow, 4).PasteSpecial xlPasteValues
End Sub
ขอบคุณมากครับ