Re: ขอรบกวนช่วยเรื่อง Code VBA เมื่อเทียบค่าตรงกัน
Posted: Sat Jun 08, 2013 6:14 pm
คลังคำตอบแห่งความรู้จากคนไทย เพื่อโลกที่ต้องการเข้าใจในสิ่งเล็ก ๆ อย่างลึกซึ้ง
https://snasui.com/
With... แต่ไม่ได้ปิดด้วย End With ลองตรวจสอบดูว่าเปิด With ไว้ตรงไหนและยังไม่ปิดด้วย End With ครับsuka wrote:อาจารย์คะ ขอรบกวนเรื่อง Code ให้ตรวจสอบหากชีท Form เซลล์ L4+L6 บวกกันแล้วยอดไม่ตรงกับยอดในเซลล์ J8 ชีท Form จะไม่สามารถกดปุ่ม Record ได้ค่ะ
ชือ BeenArL อยู่ที่ Module1 ค่ะ
ขอบคุณค่ะ
Code: Select all
Sub BeenArL()
Dim rSource As Range
Dim rTarget As Range
Dim rs As Range ' ปุ่มบันทึกรับชำระ ชีท Form
Dim rt As Range
Dim i As Integer
With Sheets("Form")
Set rSource = .Range("B3:B47")
End With
With Sheets("Database")
Set rTarget = .Range("D2", .Range("D" & Rows.Count).End(xlUp))
End With
Application.Calculation = xlCalculationManual
For Each rs In rSource
For Each rt In rTarget
If rt = rs Then rt.Offset(0, 25) = "Y"
Next rt
Next rs
Application.Calculation = xlCalculationAutomatic
With ActiveSheet
i = (.Range("L4") + .Range("L6"))
If i <> .Range("J8") Then
MsgBox "โปรดตรวจจำนวนเงินและบันทึกใหม่"
Exit Sub
End If
End With
Application.ScreenUpdating = False
Sheets("TemBilling").Range("A12:O12").Copy
Sheets("AR").Range("A" & Rows.Count).End(xlUp) _
.Offset(1, 0).PasteSpecial xlPasteValues
Sheets("TemBilling").Range("P12:W12").Copy
Sheets("Report").Range("A" & Rows.Count).End(xlUp) _
.Offset(1, 0).PasteSpecial xlPasteValues
Sheets("Form").Range("H1,J2,I4:L4,L6,G4").ClearContents
With Sheets("Form")
.Range("J6") = .Range("J6") + 1
End With
Application.ScreenUpdating = True
End SubCode: Select all
With ActiveSheet
i = (.Range("L4") + .Range("L6"))
If i <> .Range("J8") Then
MsgBox "โปรดตรวจจำนวนเงินและบันทึกใหม่"
Exit Sub
End If
End WithCode: Select all
For Each rs In rSource
For Each rt In rTarget
If rt = rs Then rt.Offset(0, 25) = "Y"
Next rt
Next rsCode: Select all
Sub BeenArL()
Dim rSource As Range
Dim rTarget As Range
Dim rs As Range ' ปุ่มบันทึกรับชำระ ชีท Form
Dim rt As Range
Dim i As Integer
With Sheets("Form")
Set rSource = .Range("B3:B47")
End With
With Sheets("Database")
Set rTarget = .Range("D2", .Range("D" & Rows.Count).End(xlUp))
End With
With ActiveSheet
i = (.Range("L4") + .Range("L6"))
If i <> .Range("J8") Then
MsgBox "โปรดตรวจจำนวนเงินและบันทึกใหม่"
Exit Sub
End If
End With
Application.Calculation = xlCalculationManual
For Each rs In rSource
For Each rt In rTarget
If rt = rs Then rt.Offset(0, 25) = "Y"
Next rt
Next rs
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = False
Sheets("TemBilling").Range("A12:O12").Copy
Sheets("AR").Range("A" & Rows.Count).End(xlUp) _
.Offset(1, 0).PasteSpecial xlPasteValues
Sheets("TemBilling").Range("P12:W12").Copy
Sheets("Report").Range("A" & Rows.Count).End(xlUp) _
.Offset(1, 0).PasteSpecial xlPasteValues
Sheets("Form").Range("H1,J2,I4:L4,L6,G4").ClearContents
With Sheets("Form")
.Range("J6") = .Range("J6") + 1
End With
Application.ScreenUpdating = True
End SubCode: Select all
Sub BeenArL()
Dim rSource As Range
Dim rTarget As Range
Dim rs As Range ' ปุ่มบันทึกรับชำระ ชีท Form
Dim rt As Range
Dim i As Integer
With Sheets("Form")
Set rSource = .Range("B3:B47")
End With
With Sheets("Database")
Set rTarget = .Range("D2", .Range("D" & Rows.Count).End(xlUp))
End With
Application.Calculation = xlCalculationManual
With ActiveSheet
i = (.Range("L4") + .Range("L6"))
If i <> .Range("J8") Then
MsgBox "โปรดตรวจจำนวนเงินและบันทึกใหม่"
Exit Sub
End If
End With
For Each rs In rSource
For Each rt In rTarget
If rt = rs Then rt.Offset(0, 25) = "Y"
Next rt
Next rs
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = False
Sheets("TemBilling").Range("A12:O12").Copy
Sheets("AR").Range("A" & Rows.Count).End(xlUp) _
.Offset(1, 0).PasteSpecial xlPasteValues
Sheets("TemBilling").Range("P12:W12").Copy
Sheets("Report").Range("A" & Rows.Count).End(xlUp) _
.Offset(1, 0).PasteSpecial xlPasteValues
Sheets("Form").Range("H1,J2,I4:L4,L6,G4").ClearContents
With Sheets("Form")
.Range("J6") = .Range("J6") + 1
End With
Application.ScreenUpdating = True
End SubDim i As DoubleDim i As Variant เนื่องจาก Decimal อยู่ภายใต้ Variant อีกที ไม่สามารถประกาศเป็น Decimal ตรง ๆ ได้ แต่สำหรับงานนี้ผมคิดว่าประกาศเป็น Double ก็พอแล้ว