Excel restarts after running commandbutton

vbahenk

New Member
Joined
Aug 11, 2020
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hallo there,

In my form in vba there is a command button. If I try to use this to add an extra row Excel restarts.
The problem is in the .rowsource in the sub Refresh_data

I hope you can help me :)

Option Explicit
---------------------------------------------------------------------------------------------------------------------------
Private Sub CommandButton1_Click()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Database")
Dim Last_Row As Long
Last_Row = Application.WorksheetFunction.CountA(sh.Range("A:A"))

'========== Validations ==========
If Me.TextBox1.Value = "" Then
MsgBox "Please enter New/ Number", vbCritical
Exit Sub
End If
If Me.TextBox2.Value = "" Then
MsgBox "Please enter MAWB", vbCritical
Exit Sub
End If
If Me.TextBox4.Value = "" Then
MsgBox "Please enter Shipment/ Invoice", vbCritical
Exit Sub
End If
If Me.ComboBox1_Forwarder.Value = "" Then
MsgBox "Please enter Forwarder", vbCritical
Exit Sub
End If
If Me.ComboBox2_Carrier.Value = "" Then
MsgBox "Please enter Carrier/ Service", vbCritical
Exit Sub
End If
If Me.ComboBox3_Shipper.Value = "" Then
MsgBox "Please enter Shipper", vbCritical
Exit Sub
End If
If Me.ComboBox4_Code.Value = "" Then
MsgBox "Please enter Code", vbCritical
Exit Sub
End If
If Me.ComboBox5_Flow.Value = "" Then
MsgBox "Please enter Flow", vbCritical
Exit Sub
End If
'=================================

sh.Range("A" & Last_Row + 1).Value = Me.TextBox1.Value
sh.Range("B" & Last_Row + 1).Value = Me.TextBox2.Value
sh.Range("C" & Last_Row + 1).Value = Me.TextBox3.Value
sh.Range("D" & Last_Row + 1).Value = Me.TextBox4.Value
sh.Range("E" & Last_Row + 1).Value = Me.ComboBox1_Forwarder.Value
sh.Range("F" & Last_Row + 1).Value = Me.ComboBox2_Carrier.Value
sh.Range("G" & Last_Row + 1).Value = Me.ComboBox3_Shipper.Value
sh.Range("H" & Last_Row + 1).Value = Me.ComboBox4_Code.Value
sh.Range("I" & Last_Row + 1).Value = Me.ComboBox5_Flow.Value

If OptionButton1.Value = True Then
Worksheets("Database").Cells(Last_Row + 1, 10).Value = "Direct"
ElseIf OptionButton2.Value = True Then
Worksheets("Database").Cells(Last_Row + 1, 10).Value = "Broker"
End If

If OptionButton3.Value = True Then
Worksheets("Database").Cells(Last_Row + 1, 11).Value = "Yes"
ElseIf OptionButton4.Value = True Then
Worksheets("Database").Cells(Last_Row + 1, 11).Value = "No"
End If

If OptionButton5.Value = True Then
Worksheets("Database").Cells(Last_Row + 1, 12).Value = "Yes"
ElseIf OptionButton6.Value = True Then
Worksheets("Database").Cells(Last_Row + 1, 12).Value = "No"
End If

If OptionButton7.Value = True Then
Worksheets("Database").Cells(Last_Row + 1, 13).Value = "Yes"
ElseIf OptionButton8.Value = True Then
Worksheets("Database").Cells(Last_Row + 1, 13).Value = "No"
End If

Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox4.Value = ""
Me.ComboBox1_Forwarder.Value = ""
Me.ComboBox2_Carrier.Value = ""
Me.ComboBox3_Shipper.Value = ""
Me.ComboBox4_Code.Value = ""
Me.ComboBox5_Flow.Value = ""
Me.OptionButton1.Value = ""
Me.OptionButton2.Value = ""
Me.OptionButton3.Value = ""
Me.OptionButton4.Value = ""
Me.OptionButton4.Value = ""
Me.OptionButton6.Value = ""
Me.OptionButton7.Value = ""
Me.OptionButton8.Value = ""

Call Refresh_data
End Sub

---------------------------------------------------------------------------------------------------------------------------

Sub Refresh_data()

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Database")
Dim Last_Row As Long
Last_Row = Application.WorksheetFunction.CountA(sh.Range("A:A"))

With Me.ListBox1
.ColumnHeads = True
.ColumnCount = 13
.ColumnWidths = "60,70,70,65,60,55,55,60,60,55,55,55,55"
.RowSource = "Database!A2:M" & Last_Row
End With

Me.ListBox1.TopIndex = ListBox1.ListCount - 1
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I tried .RowSource = "Database!A2:M250" and that worked fine. So there is something with finding the last row.
 
Upvote 0
p70tNdLGsmbYYRa9CrA8RGeu3O8gBEazXq6pFVJR49hj2dFnx385ybP8Ko7-ax1kQnYK2PGXT9VJ_7aNRFBMoFJs8a2n8oCiCkxb_dxNH-X4bQlYt9YKQG95tpQl4ux3VVnpT-k
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

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