Longest Column not finding last couple of rows

Rackette

New Member
Joined
Jul 2, 2019
Messages
37
Good morning/afternoon.
If someone has some time to help, I would appreciate it. :)

So, I've been using this code to find my longest row/column range so I can put 'all borders' around each cell in that range. However, on this last report, it doesn't seem to see the last two rows. Not sure why. My header row is on row 5 and there is something in every cell in column A and there are 10 columns.

I found this two lines, that sound like they should/might work, but I don't know how to substitute them in.
UsedCol = ThisWorkbook.ActiveSheet.UsedRange.SpecialCells(x1CellTypeLastCell).Column
UsedRow = ThisWorkbook.ActiveSheet.UsedRange.SpecialCells(x1CellTypeLastCell).Row


This is what works most of the time but won't see the last two rows of my current data range.

Code:
Sub AllBordersEvenBlanks5()


Application.ScreenUpdating = False
Dim lngLstCol As Long, lngLstRow As Long

lngLstRow = ActiveSheet.UsedRange.Rows.Count
lngLstCol = ActiveSheet.UsedRange.Columns.Count
For Each rngCell In Range("A5:A" & lngLstRow)
    If rngCell.Value > "" Then
        r = rngCell.Row
        C = rngCell.Column
        Range(Cells(r, C), Cells(r, lngLstCol)).Select
            With Selection.Borders
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
    End If
Next

End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Does it work if you put this line at the beginning of your code?
Code:
ActiveWorkbook.Save
I think if data is added after the last save and before the code is run, the Used Range may not be updated until the file is saved again.
So saving it at the beginning of your code may fix your issue.
 
Upvote 0
If you want to put a border on every cell in the usedrange try
Code:
Sub Rackette()
   ActiveSheet.UsedRange.Borders.Weight = xlThin
End Sub
 
Upvote 0
Just to point out a few things....
Code:
lngLstRow = ActiveSheet.UsedRange.Rows.Count

Counts the number of rows in the usedrange so unless your data started in row 1 you would get an incorrect number in
Code:
Range("A5:A" & lngLstRow)


For instance in the below


Book1
ABC
1
2
3aaaaaaaaaaaa
4aaaaaaaaaaaa
5aaaaaaaaaaaa
6
7
8
9
Sheet1



The usedrange is A3:C5 so there are only 3 rows in the usedrange. This means if you done Range("A1:A" & ActiveSheet.UsedRange.Rows.Count) it would only include A1:A3.
Which was probably your issue.


In the copy of sample lines you posted the red 1 should be a lowercase L.
Code:
UsedCol = ThisWorkbook.ActiveSheet.UsedRange.SpecialCells(x[COLOR="#FF0000"]1[/COLOR]CellTypeLastCell).Column
UsedRow = ThisWorkbook.ActiveSheet.UsedRange.SpecialCells(x[COLOR="#FF0000"]1[/COLOR]CellTypeLastCell).Row


Finally if memory retention in an issue with the usedrange then you could try

Code:
Sub GetRange()
	Dim LstRw As Long, LstCo As Long
	LstRw = Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
	LstCo = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
	Range(Cells(5, "A"), Cells(LstRw, LstCo)).Borders.Weight = xlThin
End Sub
 
Last edited:
Upvote 0
As an addendum to Mark858's post, the UsedRange includes formatted cells empty or not.
 
Upvote 0
As an addendum to Mark858's post, the UsedRange includes formatted cells empty or not.

Another reason why I rarely use it :ROFLMAO:


I should have also stated that the code I posted ignores cells with formulas that return "" as a result, if you wanted them included you would use...
Code:
Sub GetRange()
	Dim LstRw As Long, LstCo As Long
	LstRw = Cells.Find("*", , [COLOR="#FF0000"]xlFormulas[/COLOR], , xlByRows, xlPrevious).Row
	LstCo = Cells.Find("*", , [COLOR="#FF0000"]xlFormulas[/COLOR], , xlByColumns, xlPrevious).Column
	Range(Cells(5, "A"), Cells(LstRw, LstCo)).Borders.Weight = xlThin
End Sub
 
Upvote 0
Thank you to all of you for taking the time to help address my issue.
Mark858s code that includes would have a "" formula result, seems to have fixed the problem.
It amazes me how much I have learned from the people who have asked the questions on this forum and from the people who have taken time to respond and answer those questions.
It is very appreciated!
-Christine
 
Upvote 0
You're welcome, thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,557
Latest member
richa mishra

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