Page 1 of 1

Add Rows ด้วย user form

Posted: Sun Feb 02, 2014 1:01 pm
by yukioh
สะหวัดดีครับ อาจารย์

ขออนุญาตรบกวนสอบถาม VBA ครับ

I) ผมมีข้อมูลอยู่ในตาราง แบ่งออกเป็นสองสว่น
1:คือส่วนของ Monthly
2:คือส่วนของ Annually

ผมต้องการเพี่ม Row ที่อยู่ในตารางโดยมืเงือนไขอยู่ว่า:
ถ้าเราเลือก option "Monthly" ใน user form ให้เพี้ม Row ตามจำนวนที่เราต้องกาน ในส่วนของ "Monthly" หลือว่า
เราเลือก option "Annually" ใน user form ให้เพี้ม Row ตามจำนวนที่เราต้องกาน ในส่วนของ "Annually"

แต่ดูเหมือนว่าผมต้องกดปุ่ม "Add" ใน form "Add Row Details" สองครั้ง ถึงจะทำงาน

ดั่งตัวอย่างไฟล์ที่แนบมา
Add Row Test.xls
รบกวนอาจารย์ด้วยครับ

Re: Add Rows ด้วย user form

Posted: Sun Feb 02, 2014 2:01 pm
by snasui
:D Code ที่เขียนไว้แล้วชื่ออะไร ติดขัดบรรทัดใด Password สำหรับการเปิดไฟล์ชื่ออะไร ช่วยแจ้งด้วยครับ

Re: Add Rows ด้วย user form

Posted: Sun Feb 02, 2014 4:18 pm
by yukioh
ครับ อาจารย์
ขอโทษ อาจารย์ ที่ลืมเอาออก, ปุ่ม "Add" ใน user form "Add Rows Details" ครับ


แนบไฟล์ มาให้ไห่มแล้วครับ
Add Row Test.xls

Re: Add Rows ด้วย user form

Posted: Sun Feb 02, 2014 4:38 pm
by snasui
:D ตอบยังไม่ครบครับ Code นั้นติดที่บรรทัดใดครับ

Re: Add Rows ด้วย user form

Posted: Sun Feb 02, 2014 6:28 pm
by yukioh
Code ที่อยู่ข่างใน ปุ่ม "Add" ใน user form ครับ

Code: Select all

Private Sub AddBotton_Click()

If SecOptM = True Then

Dim LinesToInsert As Variant
Dim rTarget As Range
  
  With Sheets("MAIN")
  'Set rTarget = Range("a" & .Rows.Count).End(xlUp).Offset(-6, 0)
   Set r = ActiveCell
    
  Sheets("Main").Select: Range("b9").Select ' SHEET TARGET

    Do Until UCase(ActiveCell.Value) = "ANNUALLY"
        ActiveCell.Offset(1, 0).Select ' find the range named "ID"
    Loop
        ActiveCell.Offset(0, 0).Select
        intR = ActiveCell.Row
    
   'LinesToInsert = InputBox("How many Rows to insert? ") ' Ask for how many rows needs
    LinesToInsert = RowNTextBox
  
  For Each c In r
    If c.Value <> "" Then
        Do Until ActiveCell.Value = ""
            If UCase(ActiveCell.Value) = UCase(c.Value) And _
                    c.Value <> "" Then
                If Val(LinesToInsert) <= 50 Then
                    On Error Resume Next
                    r.EntireRow.Resize(Int(LinesToInsert)).Insert
                End If
            Else
                ActiveCell.Offset(1, 0).Select
            End If
        Loop
    End If
    Cells(intR, 2).Select
Next
    
    End With
End If

End Sub


ผมต้องกดปุ่ม "Add" ใน form "Add Row Details" สองครั้ง ถึงจะทำให้ Add row
ไม่รุ้ว่าเป็นเพาะอะไร

Re: Add Rows ด้วย user form

Posted: Sun Feb 02, 2014 6:32 pm
by snasui
:shock: ทั้งหมดที่ยกมานั้น บรรทัดใดที่ไม่ทำงาน หรือทำงานไม่ตรงกับที่ต้องการครับ ผมต้องการให้คุณเจาะจงลงไปให้ให้ได้ว่าที่เขียนมาแล้วนั้นติดขัดตรงบรรทัดไหน อย่างไร

สามารถยก Code มาทั้งชุดได้ แต่ต้องระบุให้ชัดว่าตรงไหนที่มีปัญหา เช่น เกิด Error หรือทำงานไม่ตรงกับที่ต้องการ

Re: Add Rows ด้วย user form

Posted: Sun Feb 02, 2014 6:58 pm
by yukioh
ครับ อาจารย์
ขอโทษ อาจารย์

ตรงนี้ครับ มันจะ loop ไปเรีอยຯ และ กระโดดไปที่ End Sub ครับ

Code: Select all


For Each c In r
    If c.Value <> "" Then
        Do Until ActiveCell.Value = ""
            If UCase(ActiveCell.Value) = UCase(c.Value) And _
                    c.Value <> "" Then
                If Val(LinesToInsert) <= 50 Then
                    On Error Resume Next
                    r.EntireRow.Resize(Int(LinesToInsert)).Insert
                End If
            Else
                ActiveCell.Offset(0, 0).Select
            End If
        Loop
    End If
    Cells(intR, 2).Select


Re: Add Rows ด้วย user form

Posted: Sun Feb 02, 2014 7:41 pm
by snasui
:D ดูตัวอย่างการปรับ Code ตามด้านล่างครับ

Code: Select all

Private Sub AddBotton_Click()
    Dim x$, y&
    If (SecOptM = True Or SecOptA = True) _
        And RowNTextBox <> "" Then
        With Sheets("MAIN")
            x = IIf(SecOptM, "Annually", "Total")
            y = Application.Match(x, .Range("b:b"), 0)
            .Range("b" & y).Resize(RowNTextBox).EntireRow.Insert
        End With
    End If
End Sub

Re: Add Rows ด้วย user form

Posted: Sun Feb 02, 2014 7:51 pm
by yukioh
ขอบคุณ อาจารย์ ครับ

รบกวนอาจารย์ ช่วยอธิบาย Code ให้ด้วยได้ไหมครับ ?

Re: Add Rows ด้วย user form

Posted: Sun Feb 02, 2014 7:59 pm
by snasui
:D คุณไม่เข้าใจบรรทัดไหนให้ถามบรรทัดนั้นครับ

Re: Add Rows ด้วย user form

Posted: Sun Feb 02, 2014 8:21 pm
by yukioh
บรรทัดนี้ครับ

Dim x$, y&

x = IIf(SecOptM, "Annually", "Total")
y = Application.Match(x, .Range("b:b"), 0)
Range("b" & y).Resize(RowNTextBox).EntireRow.Insert

Re: Add Rows ด้วย user form

Posted: Sun Feb 02, 2014 8:44 pm
by snasui
:D Dim x$, y& มีค่าเท่ากับ Dim s As String, y as Long
x = IIf(SecOptM, "Annually", "Total") เป็นการกำหนดค่าให้กับตัวแปร x โดยมีเงื่อนไขว่า หาก SecOptM ถูกเลือก ให้ x มีค่าเป็น "Annualy" หาก SecOptM ไม่ถูกเลือก ให้ x มีค่าเป็น "Total"
y = Application.Match(x, .Range("b:b"), 0) เป็นการกำหนดค่าให้กับตัวแปร y โดยให้ไปค้นว่าค่า x อยู่ในคอลัมน์ b ในลำดับที่เท่าไร ก็ให้ y มีค่าเท่านั้น
Range("b" & y).Resize(RowNTextBox).EntireRow.Insert เป็นการแทรกบรรทัดใน คอลัมน์ b บรรทัดที่ y จำนวน RowNTextBox บรรทัด

Re: Add Rows ด้วย user form

Posted: Wed Feb 05, 2014 10:44 pm
by yukioh
ขอบคุณครับ อาจารย์