snasui wrote:
เราสามารถแบ่งย่อยได้เท่าที่ต้องการครับ
การแบ่งเป็นคำสั่งย่อย ๆ คือการสร้าง Procedure ใหม่ แล้วค่อยเรียกใช้ Procedure นั้น
ตัวอย่าง
Code: Select all
Sub MainCode()
Call Code1
Call Code2
End Sub
Sub Code1()
MsgBox "Hello."
End Sub
Sub Code2()
MsgBox "How are you?"
End Sub
ลองทดสอบเรียก MainCode ดูครับ
เรียนอาจารย์ค่ะ จากตัวอย่าง Code ของอาจารย์ด้านบนการแบ่งเป็นคำสั่งย่อยแล้วค่อยเรียกใช้
หากต้องการนำ Code นี้มาใช้กับ Code ทั้งชุดด้านล่างนี้ค่ะ
Code: Select all
Sub AutoFilter()
Windows("Ph_BookShare.xlsx").Activate
ActiveWindow.WindowState = xlNormal
ActiveWindow.WindowState = xlNormal
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
ActiveWorkbook.Save
End Sub
ความต้องการให้ Code ปลด AutoFilter ที่ไฟล์ Ph_BookShare ก่อนหากมี AutoFilter ไว้แล้วค่อยวางข้อมูลใหม่ค่ะ
ขอรบกวนช่วยจะปรับอย่างใดค่ะ
Code: Select all
Sub PasteData()
Dim wbShare As Workbook
Dim formBook As Workbook
Dim i As Integer
Dim e As Long
Dim rs As Range
Dim rt As Range
Set formBook = ThisWorkbook
Set wbShare = Workbooks("Ph_BookShare.xlsx")
With wbShare
e = wbShare.Sheets("Sheet1").Range("e" & Rows.Count).End(xlUp).Value '
End With
wbShare.Save '<== Add this line for refresh shareworkbook
Application.ScreenUpdating = False
With formBook
i = Worksheets("Enterthedata").Range("C224")
End With
With Worksheets("Template")
Set rs = .Range(.Range("A2"), .Range("AF" & i + 1))
End With
Set rt = wbShare.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
If Worksheets("Enterthedata").Range("C224") = True Then
MsgBox "Please check your data. This transaction already recorded."
Exit Sub
End If
If Worksheets("Enterthedata").Range("B204") = "" Then
MsgBox "Your data is empty. Fill your data and click record button again."
Exit Sub
End If
rs.Copy: rt.PasteSpecial xlPasteValues
wbShare.Save '<==Save after record data
formBook.Save
Application.CutCopyMode = False
Sheets("Enterthedata").Range("D2,K2,B204:B219,D204:D219,L204:L219,D221,E221,E204:F219,L204:M219,O204:O219").ClearContents
With Worksheets("Enterthedata")
.Range("N1") = .Range("N1") + 1
End With
End Sub