EXCEL TOOLS
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
[code]
และปิดด้วย [/code]
ตัวอย่างเช่น [code]dim r as range[/code]
เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)Code: Select all
LastRow = Sheets("edit and delete").Range("G" & Rows.Count).End(xlUp).Row
Code: Select all
ListBox1.RowSource = "G2:I2000"
Code: Select all
Private Sub CommandButton1_Click() 'Show Data
ListBox1.ColumnCount = 3
ListBox1.RowSource = "G2:I2000"
End Sub
Code: Select all
Private Sub ComboBox1_Change()
For i = 2 To Application.WorksheetFunction.CountA(Sheets("edit and delete").Range("G:G"))
If Sheets("edit and delete").Cells(i, 3).Value = Me.ComboBox1.Value Then
Me.ListBox1.AddItem Cells(i, 1).Value
Me.ListBox1.AddItem Cells(i, 2).Value
Me.ListBox1.AddItem Cells(i, 3).Value
End If
Next i
End Sub
Code: Select all
If Sheets("edit and delete").Cells(i, 3).Value = Me.ComboBox1.Value Then
Code: Select all
Cells(i, 2) = TextBox1.Text
Cells(i, 3) = TextBox2.Text
Code: Select all
Private Sub CommandButton1_Click() 'save
Dim i As Integer
For i = 2 To Sheets("edit and delete").Range("B" & Rows.Count).End(xlUp).Row
If Cells(i, 7) = Label4.Caption Then
Cells(i, 2) = TextBox1.Text
Cells(i, 3) = TextBox2.Text
End If
Next i
MsgBox "Saved!", vbInformation
'Unload Me
End Sub
Code: Select all
Cells(i, 2).Value = ""
Selection.delete
Cells(i, 3).Value = ""
Selection.delete
Code: Select all
Private Sub DeleteSelected_Click()
Dim i As Integer
If MsgBox("Are you sure you want to delete the row?", vbYesNo + vbQuestion, "Delete row") = vbYes Then
For i = 1 To Sheets("edit and delete").Range("B" & Rows.Count).End(xlUp).Row
If Cells(i, 2) = ListBox1.List(ListBox1.ListIndex) Then
Cells(i, 2).Select
Cells(i, 2).Value = ""
Cells(i, 2).Select
Cells(i, 2).Value = ""
Selection.delete
Cells(i, 3).Value = ""
Selection.delete
End If
Next i
End If
End Sub
LastRow = Sheets("edit and delete").Range("G" & Rows.Count).End(xlUp).Row
CommandButton1_Click()
ให้เขียนขึ้นมาก่อนแล้วลองปรับกับ ListBox1.RowSource = "G2:I2000"
มาให้ดูว่ามีแนวคิดที่จะ LastRow มาใช้กับ Statement อย่างไรครับsnasui wrote: Wed Jun 03, 2020 7:38 amถามตอบให้ผ่านไปทีละข้อครับ
ข้อแรก ไม่พบว่ามีการเขียนLastRow = Sheets("edit and delete").Range("G" & Rows.Count).End(xlUp).Row
ในคำสั่งCommandButton1_Click()
ให้เขียนขึ้นมาก่อนแล้วลองปรับกับListBox1.RowSource = "G2:I2000"
มาให้ดูว่ามีแนวคิดที่จะ LastRow มาใช้กับ Statement อย่างไรครับ
Code: Select all
ListBox1.RowSource = Sheets("edit and delete").Range(Cells(lastlow, 7), Cells(lastlow, 9)).Address
Code: Select all
Private Sub CommandButton1_Click()
Dim lastlow As Long
lastlow = Sheets("edit and delete").Range("G" & Rows.Count).End(xlUp).Row
ListBox1.ColumnCount = 3
ListBox1.RowSource = Sheets("edit and delete").Range(Cells(lastlow, 7), Cells(lastlow, 9)).Address
End Sub
ListBox1.RowSource = Sheets("edit and delete").Range(Cells(2, 7), Cells(lastlow, 9)).Address
snasui wrote: Wed Jun 03, 2020 10:26 amควรแนบไฟล์ล่าสุดมาด้วยจะได้ดูต่อไปจากนั้นและควรเป็นเช่นนี้เสมอ ไม่เช่นนั้นผมต้องมาขอให้แนบไฟล์ล่าสุดอยู่ทุกครั้งแทนที่จะได้ตอบทันทีครับ
จากตัวอย่างที่โพสต์มา ปรับเป็นตามนี้ครับListBox1.RowSource = Sheets("edit and delete").Range(Cells(2, 7), Cells(lastlow, 9)).Address
Code: Select all
Private Sub ComboBox1_Change()
For i = 2 To Application.WorksheetFunction.CountA(Sheets("edit and delete").Range("G:G"))
If Sheets("edit and delete").Cells(i, 3).Value = Me.ComboBox1.Value Then
Me.ListBox1.AddItem Cells(i, 1).Value
Me.ListBox1.AddItem Cells(i, 2).Value
Me.ListBox1.AddItem Cells(i, 3).Value
End If
Next i
End Sub
Code: Select all
If Sheets("edit and delete").Cells(i, 3).Value = Me.ComboBox1.Value Then
Code: Select all
Dim arr() As Variant, j As Integer, k As Integer
Me.ListBox1.RowSource = ""
Me.ListBox1.ColumnCount = 3
k = Application.CountIf(Sheets("edit and delete").Range("G:G"), Me.ComboBox1.Text)
ReDim arr(0 To k, 2)
For i = 2 To Application.WorksheetFunction.CountA(Sheets("edit and delete").Range("G:G"))
If Sheets("edit and delete").Cells(i, "I").Value = Me.ComboBox1.Value Then
arr(j, 0) = Sheets("edit and delete").Cells(i, "G").Value
arr(j, 1) = Sheets("edit and delete").Cells(i, "H").Value
arr(j, 2) = Sheets("edit and delete").Cells(i, "I").Value
j = j + 1
End If
Next i
Me.ListBox1.List() = arr
snasui wrote: Wed Jun 03, 2020 12:52 pmตัวอย่าง Code ครับ
Code: Select all
Dim arr() As Variant, j As Integer, k As Integer Me.ListBox1.RowSource = "" Me.ListBox1.ColumnCount = 3 k = Application.CountIf(Sheets("edit and delete").Range("G:G"), Me.ComboBox1.Text) ReDim arr(0 To k, 2) For i = 2 To Application.WorksheetFunction.CountA(Sheets("edit and delete").Range("G:G")) If Sheets("edit and delete").Cells(i, "I").Value = Me.ComboBox1.Value Then arr(j, 0) = Sheets("edit and delete").Cells(i, "G").Value arr(j, 1) = Sheets("edit and delete").Cells(i, "H").Value arr(j, 2) = Sheets("edit and delete").Cells(i, "I").Value j = j + 1 End If Next i Me.ListBox1.List() = arr
Code: Select all
Private Sub CommandButton2_Click()
UserForm2.Hide
Unload UserForm2
End Sub
Code: Select all
Private Sub CommandButton4_Click() 'edit selected
On Error GoTo ErrorHandler:
UserForm2.Label4 = ListBox1.List(ListBox1.ListIndex)
UserForm2.TextBox1.Text = ListBox1.Column(0, ListBox1.ListIndex)
UserForm2.TextBox2.Text = ListBox1.Column(1, ListBox1.ListIndex)
UserForm2.Show
ErrorHandler:
MsgBox "No Data or Click the info"
Exit Sub
Resume Next
End Sub
snasui wrote: Wed Jun 03, 2020 6:08 pmตัวอย่างในการนำ Array ไปกำหนดค่าในกับ Range คิดว่าพบได้จำนวนมากครับ
อย่างเช่นตัวอย่างนี้ wordpress/search-multiple-sheets/
ตัวอย่างอื่น ๆ ลองดูที่นี่ครับ Assign array to ranges
snasui wrote: Wed Jun 03, 2020 6:15 pmตราบใดที่ยังใช้ข้อมูลจาก UserForm2 อยู่จะต้องไม่ Unload ครับ
ให้ Mark Statement Unload ทิ้งไป
เมื่อใดที่เลิกใช้หรือจบงาน ค่อยไป Unload ครับ
Code: Select all
'Unload UserForm2
Code: Select all
Private Sub CommandButton2_Click()
UserForm2.Hide
'Unload UserForm2
End Sub
Code: Select all
UserForm2.Hide
Unload UserForm2
Code: Select all
Private Sub CommandButton4_Click() 'edit selected
On Error GoTo ErrorHandler:
UserForm2.Label4 = ListBox1.List(ListBox1.ListIndex)
UserForm2.TextBox1.Text = ListBox1.Column(0, ListBox1.ListIndex)
UserForm2.TextBox2.Text = ListBox1.Column(1, ListBox1.ListIndex)
UserForm2.Show
Exit Sub
ErrorHandler:
MsgBox "No Data or Click the info"
Resume Next
End Sub
snasui wrote: Wed Jun 03, 2020 10:36 pmกรณีการดัก Error หากไม่พบว่ามี Error ก็ให้ออกจาก Procedure แต่หาก Error ค่อยกระโดดไปทำงานใน Code ที่ใช้ดักจับ Error ต่อครับ
ตัวอย่างการปรับ Code
Code: Select all
Private Sub CommandButton4_Click() 'edit selected On Error GoTo ErrorHandler: UserForm2.Label4 = ListBox1.List(ListBox1.ListIndex) UserForm2.TextBox1.Text = ListBox1.Column(0, ListBox1.ListIndex) UserForm2.TextBox2.Text = ListBox1.Column(1, ListBox1.ListIndex) UserForm2.Show Exit Sub ErrorHandler: MsgBox "No Data or Click the info" Resume Next End Sub
Code: Select all
Private Sub DeleteSelected_Click()
Dim i As Integer
If MsgBox("Are you sure you want to delete the row?", vbYesNo + vbQuestion, "Delete row") = vbYes Then
For i = 1 To Sheets("edit and delete").Range("B" & Rows.Count).End(xlUp).Row
If Cells(i, 2) = ListBox1.List(ListBox1.ListIndex) Then
Cells(i, 2).Select
Cells(i, 2).Value = ""
Cells(i, 3).Select
Cells(i, 3).Value = ""
Cells(i, 2).Value = ""
Selection.delete
Cells(i, 3).Value = ""
Selection.delete
End If
Next i
End If
End Sub
Code: Select all
Cells(i, 2).Value = ""
Selection.delete
Cells(i, 3).Value = ""
Selection.delete
Selection.delete
เป็น Selection.clearcontents
จะได้ไม่ทำให้เซลล์ที่เกี่ยวข้องติด #REF ครับ