Command Button to clear form and add more rows in Excel

aaronmendes

New Member
Joined
Jul 1, 2013
Messages
3
Hi,

I have assigned a code in Userform for the command button to update data as per the input in the Textboxes in Excel.

However, after clicking OK (command button caption name), I want the data to be cleared in the form and also after inputting more data in the form, the OK button should add additional rows in excel.

My code is below

Code:
Private Sub OK1_Click()


Dim emptyRow As Long

'Make Sheet1 Active
Sheets(1).Activate

'Determine EmptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

'Export Data to worksheet
Cells(emptyRow, 5).Value = Trades_Asset.Value
Cells(emptyRow, 6).Value = DisputeReason.Value
Cells(emptyRow, 7).Value = CartNo.Value
Cells(emptyRow, 8).Value = CallTrack.Value
Cells(emptyRow, 9).Value = MOcontact.Value
Cells(emptyRow, 10).Value = Misc_comments.Value


End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Not tested & intended only as a possible approach you could take. See if following code helps:

Code:
Private Sub OK1_Click()
    Dim emptyRow As Long
    Dim arr As Variant
    arr = Array("Trades_Asset", _
                "DisputeReason", _
                "CartNo", _
                "CallTrack", _
                "MOcontact", _
                "Misc_comments")
    'Make Sheet1 Active
    With Sheets(1)
        .Activate
        'Determine EmptyRow
        emptyRow = WorksheetFunction.CountA(.Range("A:A")) + 1
        'Export Data to worksheet
        For i = 0 To 5
            .Cells(emptyRow, i + 5).Value = Me.Controls(arr(i)).Text
            'clear textboxes
            Me.Controls(arr(i)).Text = ""
        Next i
        'insert 6 rows
        .Range(.Cells(emptyRow + 1, 1), .Cells(emptyRow + 6, 1)).EntireRow.Insert
    End With
End Sub

You will need to adust the range & number of rows you want to insert but code should give some direction for you. Also, note that I have qualified the ranges to your worksheet. Even though you have activated it, its always good practice to do this to ensure that your data does go where intended.

Hope helpful

Dave.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,194
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top