Find last row

johnster

New Member
Joined
May 31, 2018
Messages
43
Why does my code not work? It always writes in row A

The code in green works fine.

Private Sub CommandButton100_Click()
Dim lItem As Long
Dim IRange As Integer

For lItem = 0 To ListBox1.ListCount - 1
ListBox1.ColumnCount = 2
With Worksheets("Sheet6")
.Cells(lItem + 5, 1) = ListBox1.List(lItem, 0)
.Cells(lItem + 5, 2) = ListBox1.List(lItem, 1)
End With

With Sheets("Histo")
Item = Worksheets("Histo").Cells(Rows.Count, 1).End(xlUp).Value
.Cells(lItem + 1, 1) = Date
.Cells(lItem + 1, 2) = ListBox1.List(lItem, 0)
.Cells(lItem + 1, 3) = ListBox1.List(lItem, 1)
End With
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Maybe:
Code:
Item = Worksheets("Histo").Cells(Rows.Count, 1).End(xlUp).Row
bye
 
Upvote 0
You are using lItem when your last row is using ltem in the Cells part.
 
Upvote 0
I think MARK is suggesting not to use "Item" as the name of the variable, because Item is a keyword in vba
So use LItem = etc etc
 
Upvote 0
I think MARK is suggesting not to use "Item" as the name of the variable, because Item is a keyword in vba
So use LItem = etc etc

Thx for your help but now he copies only the first column.

With Sheets("Histo")
lItem = Worksheets("Histo").Cells(Rows.Count, 1).End(xlUp).Row
.Cells(lItem + 1, 1) = Date
.Cells(lItem + 1, 2) = ListBox1.List(LlItem, 0)
.Cells(lItem + 1, 3) = ListBox1.List(LlItem, 1)
.Cells(lItem + 1, 4) = TextBox2.Value
End With
 
Last edited:
Upvote 0
Thx for your help but now he copies only the first column.

With Sheets("Histo")
lItem = Worksheets("Histo").Cells(Rows.Count, 1).End(xlUp).Row
.Cells(lItem + 1, 1) = Date
.Cells(lItem + 1, 2) = ListBox1.List(LlItem, 0)
.Cells(lItem + 1, 3) = ListBox1.List(LlItem, 1)
.Cells(lItem + 1, 4) = TextBox2.Value
End With
One moment...
You cannot expect that we (at least "me") understand what you are trying to obtain from a code that don't do that.

So either explain what you wish to obtain or ask yourself why somewhere you use LItem and somewhere else LLItem

Bye
 
Upvote 0
Try
Code:
With Sheets("Histo")
nItem = Worksheets("Histo").Cells(Rows.Count, 1).End(xlUp).Value
.Cells(nItem + 1, 1) = Date
.Cells(nItem + 1, 2) = ListBox1.List(lItem, 0)
.Cells(nItem + 1, 3) = ListBox1.List(lItem, 1)
End With
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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