chipsworld
Board Regular
- Joined
- May 23, 2019
- Messages
- 169
- Office Version
- 365
Hi all...
Have one I just can't figure out...
Have a listbox in a form that is used to copy multiselections to a different sheet than the one with source data.
I am trying to figure out how to copy to last row on new sheet and how to delete from original sheet.
Copying over data works great, but writes over existing data on second use...want to just put at lastrow + 1, and I have no idea how to delete the data form the source sheet...
Here is the code so far...
Source sheet is "UC Details"
Moving to "Completed UCs"
Have one I just can't figure out...
Have a listbox in a form that is used to copy multiselections to a different sheet than the one with source data.
I am trying to figure out how to copy to last row on new sheet and how to delete from original sheet.
Copying over data works great, but writes over existing data on second use...want to just put at lastrow + 1, and I have no idea how to delete the data form the source sheet...
Here is the code so far...
Source sheet is "UC Details"
Moving to "Completed UCs"
VBA Code:
Private Sub cmdremove_Click()
Dim lItem As Long, lRows As Long, lCols As Long
Dim bSelected As Boolean
Dim lColLoop As Long, lTransferRow As Long
Dim ws As Worksheet
Dim lstrw As Double
Dim rw As Double
Set ws = Sheets("Completed UCs")
With ws
lstrw = .Range("A" & Rows.Count).End(xlUp).Row
rw = lstrw + 1
End With
'Pass row & column count to variables
'Less 1 as "Count" starts at zero
lRows = lstmain.ListCount - 1
lCols = lstmain.ColumnCount - 1
'Ensure they have at least 1 row selected
For lItem = 0 To lRows
'At least 1 row selected
If lstmain.Selected(lItem) = True Then
'Boolean flag
bSelected = True
'Exit for loop
Exit For
End If
Next
'At least 1 row selected
If bSelected = True Then
With Sheets("Completed UCs").Range("A2", Sheets("Completed UCs").Cells(lRows + 1, 4 + lCols)) 'Transfer to range
For lItem = 0 To lRows
If lstmain.Selected(lItem) = True Then 'Row selected
'Increment variable for row transfer range
lTransferRow = lTransferRow + 1
'Loop through columns of selected row
For lColLoop = 0 To lCols
'Transfer selected row to relevant row of transfer range
.Cells(lTransferRow, lColLoop + 1) = lstmain.List(lItem, lColLoop)
'Uncheck selected row
lstmain.Selected(lItem) = False
Next lColLoop
End If
Next
End With
Else
MsgBox "There are no line items selected!", vbCritical
End If
End Sub