Why is this code not working

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
VBA Code:
Private Sub Add_Towbar_to_Jobcard_Click()

Dim r As Long
Dim i As Long
Dim wsDest As Worksheet
Set wsDest = ThisWorkbook.Sheets("Job Card Master")
wsDest.Activate

Sheets("Sheet2").Range("A2:O49").Copy wsDest.Range("A127")
r = 2

For i = 0 To Me.ListBox1.ListCount - 1
   If Me.ListBox1.Selected(i) Then
   wsDest.Cells(r, 1).Value = Me.ListBox1.List(i, 0)
   wsDest.Cells(r, 2).Value = Me.ListBox1.List(i, 1)
   wsDest.Cells(r, 3).Value = Me.ListBox1.List(i, 2)
   wsDest.Cells(r, 4).Value = Me.ListBox1.List(i, 3)
   wsDest.Cells(r, 5).Value = Me.ListBox1.List(i, 4)
  
   r = r + 1
   End If
Next



End Sub
 
Last edited by a moderator:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Yes, but how?
Are you using rowsource, list, additems?
 
Upvote 0
I cannot replicate the problems you are getting.
Make sure that the control source property of the listbox is empty.
Also it is better to populate the listbox using list, rather than rowsource. What is the range you are using for rowsource?
 
Upvote 0
I cannot replicate the problems you are getting.
Make sure that the control source property of the listbox is empty.
Also it is better to populate the listbox using list, rather than rowsource. What is the range you are using for rowsource?
=On Second sheet in workbook Range $A$1:$O$49
 
Upvote 0
Ok, clear the rowsource property & put this in the initialize event
VBA Code:
Me.ListBox1.List = Sheets("Sheet2").Range("A2:O49").Value
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,867
Messages
6,122,002
Members
449,059
Latest member
mtsheetz

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