to find the LastRow

jdcar

New Member
Joined
Oct 9, 2021
Messages
17
Office Version
  1. 2016
Platform
  1. Windows
Hi Everyone
I have a form that has a combobox for choosing the subject, I use "labels" to show that data and a command to regist it in the worksheet
It turns out that the last line is not identified, so the information to be added is superimposed on the previously registered information. How to overcome?
I enclose an excerpt of code.
Thanks you.
VBA Code:
Private Sub CommandButton2_Click()
Dim wks As Worksheet
Dim additem As Range
Dim rng As Range

Set wks = Sheet5
   Set rng = Worksheets("sheet5").Cells(Rows.Count, "a").End(xlUp).Offset(1, 0)

rng.Offset(0, 1) = Label1.Caption
rng.Offset(0, 2) = Label2.Caption
rng.Offset(0, 3) = Label3.Caption
.....
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
How about
VBA Code:
   Set rng = Worksheets("sheet5").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)

rng.Offset(0, 0) = Label1.Caption
rng.Offset(0, 1) = Label2.Caption
rng.Offset(0, 2) = Label3.Caption
 
Upvote 0
Hi
Thanks for the quick response, but the effect is the same, just changed columns.
the code don't find the last line filled. Very strange
There will be something wrong?
 
Upvote 0
Three other possibilities I can think of (from just seeing your code):

______________________________________________________________________________________________________________________
[Possibility B] "sheet 5" does not exist.

Worksheets("sheet5") is relying on a sheet to be named "sheet5" (Case sensitive)

By default, sheets are named with a capital S. So if the tab is actually named "Sheet5", change your code to:
VBA Code:
Private Sub CommandButton2_Click()
Dim wks As Worksheet
Dim additem As Range
Dim rng As Range

Set wks = Sheet5
Set rng = Worksheets("Sheet5").Cells(Rows.Count, "a").End(xlUp).Offset(1, 0)

rng.Offset(0, 1) = Label1.Caption
rng.Offset(0, 2) = Label2.Caption
rng.Offset(0, 3) = Label3.Caption
.....

(Unlike the "a" in Cells(), which case sensitivity doesn't matter.)

______________________________________________________________________________________________________________________
[Possibility C] The sheet code name is not the same as the sheet name.

Temporarily put this sub in in a normal module and run it.
VBA Code:
Sub Test__SheetName_is_SheetCodeName()
MsgBox Worksheets("sheet5").Name = Worksheets("sheet5").CodeName
End Sub
If it returns False, then you probably have something like this in your Worksheets list in the VBA project module for that Workbook.
sheetname.PNG


From the start of this Workbook's "life", you have either renamed or deleted (maybe both) sheets, so that the sheet code names have become off-sync with the sheet names (the names that you see in the sheet tab names).

As an analogy,
Sheet code name is to absolute cell reference as sheet name is to named range.

When you did the following assignment,
VBA Code:
Set wks = Sheet5
you set wks equal to the sheet code name. If a later line of code down in the sub (or one of the subs/functions it callses), your code relies on the result that Sub Test__SheetName_is_SheetCodeName is True.

If you want to set it to the sheet name, you need to do the following instead (you need quotes)
VBA Code:
Set wks = Worksheets("sheet5")


To fix this issue, you need to therefore set wks equal to the sheet name (not sheet code name)
VBA Code:
Private Sub CommandButton2_Click()
Dim wks As Worksheet
Dim additem As Range
Dim rng As Range

Set wks = Worksheets("sheet5")
Set rng = wks.Cells(Rows.Count, "a").End(xlUp).Offset(1, 0)

rng.Offset(0, 1) = Label1.Caption
rng.Offset(0, 2) = Label2.Caption
rng.Offset(0, 3) = Label3.Caption
.....
______________________________________________________________________________________________________________________
[Possibility D] You need the last non-blank row in the entire worksheet, rather than just the last non-blank row in Column A.

By the looks of this portion of your code,
VBA Code:
Set rng = Worksheets("sheet5").Cells(Rows.Count, "a").End(xlUp).Offset(1, 0)
rng.Offset(0, 1) = Label1.Caption
rng.Offset(0, 2) = Label2.Caption
rng.Offset(0, 3) = Label3.Caption

It relies on the fact that Column A is the column whose last used row is the largest row number. But if this is not necessarily the case, then you need to put some type of function like the following (many people prefer Range.Find, but if the following doesn't slow down your form, it's definitely much "safer" to use) in a standard code module permanently for future use in your Workbook.
VBA Code:
Sub Test__Last_Blank_Row_In_Sheet()
MsgBox Last_Blank_Row_In_Sheet(ActiveSheet.Name)
End Sub
Function Last_Blank_Row_In_Sheet(sheetName As String)

Dim numberOfUsedColumns As Integer, previousLargestLastRow As Long, i As Integer

With Worksheets(sheetName)
    numberOfUsedColumns = .UsedRange.Columns.Count + .UsedRange.Column - 1
    previousLargestLastRow = 1
    i = 1
    Do While i <= numberOfUsedColumns
        If .Cells(Rows.Count, i).End(xlUp).Row > previousLargestLastRow Then previousLargestLastRow = .Cells(Rows.Count, i).End(xlUp).Row
        i = i + 1
    Loop
End With

Last_Blank_Row_In_Sheet = previousLargestLastRow

End Function

And therefore, your original code in the userform would need to be changed to:
VBA Code:
Private Sub CommandButton2_Click()
Dim wks As Worksheet
Dim additem As Range
Dim rng As Range

Set wks = Sheet5
Set rng = Worksheets("sheet5").Range("A" & Last_Blank_Row_In_Sheet("sheet5") + 1)

rng.Offset(0, 1) = Label1.Caption
rng.Offset(0, 2) = Label2.Caption
rng.Offset(0, 3) = Label3.Caption
.....


And by the way, note that:
VBA Code:
Worksheets("sheet5")
and
VBA Code:
Sheets("sheet5")
are equivalent. (Just to shorten your code from here on out, if you want!)
 
Upvote 0
Worksheets("sheet5") is relying on a sheet to be named "sheet5" (Case sensitive)
Wrong, it is not case sensitive. Also Sheets & Worksheets are not necessarily equivalent.
 
Upvote 0
Wrong, it is not case sensitive.
Oh, right. You cannot name two sheets with the same sequence of characters (no matter the case of the letters). (But no big deal. That misconception I had doesn't break code or cause bugs. Just an unnecessary personal self-limitaion from my end, I guess.)

So that eliminates Possibility B. Let's see if it's possibility C or D.

Sheets & Worksheets are not necessarily equivalent.
Unless someone is going to use them to do a Worksheet count:

Where:

VBA Code:
MsgBox ThisWorkbook.Sheets.Count
counts the number of both the Worksheets and chart sheets, whereas:

VBA Code:
MsgBox ThisWorkbook.Worksheets.Count
gives you just the number of the Worksheets,


yes, they are equivalent because you cannot name a chart sheet the same name as a Worksheet without Excel asking you if you want to embed the chart in the sheet that you want to move it to.

So unless I am missing something, for this purpose of passing something into it to get a sheet object, they are equivalent.

If I'm missing something, can you please explain what? (To eliminate confusion, as that seems to be your motivation for posting that response?)
 
Last edited:
Upvote 0
To eliminate confusion, as that seems to be your motivation for posting that response?
My motivation was to point out the fact that you were making inaccurate & misleading statements.
And I stand by what I said Worksheets & Sheets are NOT necessarily the same.

Now let's wait for the OP to respond, rather than getting sidetracked.
 
Upvote 0
My motivation was to point out the fact that you were making inaccurate & misleading statements.
And I stand by what I said Worksheets & Sheets are NOT necessarily the same.

Now let's wait for the OP to respond, rather than getting sidetracked.
You have my curiosity up. What is the difference in Worksheets and Sheets
 
Upvote 0

Forum statistics

Threads
1,224,872
Messages
6,181,499
Members
453,047
Latest member
charlie_odd

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