Forum Discussion
JT_2021
Aug 08, 2021Copper Contributor
VBA code Two buttons merge
I need to merge these two buttons into one, i am very new to this and keep breaking the program 😉 Private Sub cmdAdd_Click() 'dimension the variable Dim addme As Range, cNum As Integer Dim x A...
HansVogelaar
Aug 08, 2021MVP
Try this:
Private Sub cmdAdd_Click()
'dimension the variable
Dim addme1 As Range, addme2 As Range, cNum As Integer
Dim x As Integer, y As Integer, Ck As Boolean
'set variables
Set addme1 = Sheet4.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0)
Set addme2 = Sheet3.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0)
cNum = 19
'run the for loop
For x = 0 To Me.lstMulti.ListCount - 1
'add condition statement
If Me.lstMulti.Selected(x) Then
Ck = True
'handle addme1
For y = 0 To cNum
addme1.Offset(0, y) = Me.lstMulti.List(x, y)
Next y
Set addme1 = addme1.Offset(1, 0)
'handle addme2
addme2 = Me.lstMulti.List(x)
addme2.Offset(0, 1) = Me.lstMulti.List(x, 3)
addme2.Offset(0, 2) = Me.lstMulti.List(x, 1)
addme2.Offset(0, 3) = Me.lstMulti.List(x, 14)
addme2.Offset(0, 4) = Me.lstMulti.List(x, 15)
addme2.Offset(0, 5) = Me.lstMulti.List(x, 16)
addme2.Offset(0, 5) = Me.lstMulti.List(x, 17)
Set addme2 = addme2.Offset(1, 0)
'clear the selected row
lstMulti.Selected(x) = False
End If
Next x
'send a message if nothing is selected
If Not Ck Then
MsgBox "There is nothing selected"
End If
End Sub