How to find the LastColumn including blanks - excel

I am trying to find the last column with data in it. Some of my columns may contain blank columns between them.
The problem with my code is it keeps reverting back to column 13 as the last used column, when in fact the last used column is 19. I want my code to start counting from L8.
How can I fix my code to include blank columns in between?
LastCol = ws.Cells.Find(What:="*", _
After:=ws.Range("L8"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False).Column
Screen Shot

The Range.Find method returns a Range object that represents the first cell where that information is found. So if the first non-empty cell is in L13, that is what would always be returned by this formula. You may choose to search backward (from L1), but there is an easier and much more straightforward way. Just use:
LastCol = ws.Cells(8, ws.Columns.Count).End(xlToLeft).Column

Start from A1 and search backwards:
Sub sjkdfhsgf()
Set ws = ActiveSheet
Set terminus = ws.Cells.Find(What:="*", _
After:=ws.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
Lastcol = terminus.Column
MsgBox Lastcol
End Sub

Related

Would like to delete the last 2 rows of a worksheet in excel that is dynamic using VBA

Good Day,
I had found a solution that looks at a table, but the file I am trying to delete the last 2 rows for is just a worksheet. The code I am referring to is,
Dim wsR2 As Worksheet
Set wsR2 = ThisWorkbook.Sheets("Journal")
Dim LastRow As Long
LastRow = wsR2.ListObjects("xJrnl").DataBodyRange.Rows.Count
wsR2.ListObjects("xJrnl").ListRows(LastRow).Delete
I'm not sure how to edit the above code to be used for a simple worksheet rather than a table. My sheet name is "Sheet1" Any assistance is appreciated.
The hardest part of your task is to reliably find the last row on the sheet. There are several ways, each with its own advantages and disadvantages. One of the solutions is below (a fragment of the solution from Find last row, column or last cell is used - I recommend reading it).
Option Explicit
Sub Del2lastRows()
Dim sh As Worksheet, LastCell As Range
Set sh = ThisWorkbook.Sheets("Sheet1")
Set LastCell = sh.Cells.Find(what:="*", _
After:=sh.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
If LastCell.Row > 1 Then
LastCell.Offset(-1).Resize(2).EntireRow.Delete
End If
' reset find dialog after using LastRow()
sh.Cells.Find what:="", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False
End Sub

Pasting Rows B2:B16 from Sheet 2 onto Newly Inserted column on Sheet 1

I am attempting to have a macro insert a column on sheet "Runs", and then paste information from sheet "Templates" onto the newly inserted column on a specific Row. I have named the range for row four as "Eight", however, info from templates is pasted onto column A, Row 4, and not the newly inserted column.
Set myWorksheet = Worksheets("Runs")
myFirstColumnT = myWorksheet.Cells.Find( _
What:="TS", _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
myLastColumnT = myWorksheet.Cells.Find( _
What:="TE", _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
For iCounter = myLastColumnT To (myFirstColumnT + 1) Step -100000000
myWorksheet.Columns(iCounter).Insert
Sheets("Templates").Select
Range("B2:B16").Copy
Sheets("Runs").Select
With Columns(iCounter).Select
Range("eight").PasteSpecial
End With
Next iCounter
The issue is your With doesn't actually do anything and if it did your Range doesn't reference it.
I've removed your loop, it has such a massive step it isn't actually looping anything. Also you don't need a loop for this.
I removed the .column from your .finds because that will cause an error if it fails, I also added in some error checking for if (when) it doesn't find anything.
I removed all instances of .Select because they aren't necessary.
Dim myworksheet As Worksheet
Dim myfirstcolumnt As Range
Dim mylastcolumnt As Range
Dim newcol As Long
Set myworksheet = Worksheets("Runs")
'I'm assuming you need this for a reason other than the loop, otherwise you can remove it
Set myfirstcolumnt = myworksheet.Cells.Find( _
What:="TS", _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious)
If myfirstcolumnt Is Nothing Then
MsgBox "TS not found"
Exit Sub
End If
Set mylastcolumnt = myworksheet.Cells.Find( _
What:="TE", _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious)
If mylastcolumnt Is Nothing Then 'Avoiding errors
MsgBox "TE not found"
Exit Sub
End If
newcol = mylastcolumnt.Column + 1 'No need to loop to find the column you're making
myworksheet.Columns(newcol).Insert 'use the new column index to add the column
Sheets("Templates").Range("b2:b16").Copy
myworksheet.Cells(4, newcol).PasteSpecial 'We know it's going in row 4 and we have the new column index now
If you want to use your named range you can do myworksheet.Cells(myworksheet.range("Eight").Row, newcol)... Though I suggest changing the name, a range called "Eight" pointing to Row 4 isn't very clear.

Define Last Row as per the report

I am working on a code that performs a certain number of steps on different reports. The reports contain different number or rows every time and in some cases , the reports also contain a hidden row below the last row with data. My code works fine on reports that have a hidden row but it does not work well on reports that do not have a hidden row. For the reports that do not have a hidden row, it leaves one row blank.
It works well until I define LR2. I would like to define LR2 in so that it does not consider the hidden row as a row containing data so that my code works uniformly on reports containing hidden row as well as not containing hidden row. Please see the image of the file that has a hidden row. In this case, row number 64 is hidden but in some cases there are no hidden rows below the grey row which is supposed to be the last row. Please assist me writing a single code to work for both scenarios
Dim LR2 As Long
LR2 = ActiveSheet.UsedRange.Rows.Count - 2
ActiveSheet.Range("A6:A" & LR2).Copy ActiveSheet.Range("B6:B" & LR2)
Application.CutCopyMode = False
ActiveSheet.Range("B6:B" & LR2).Select
Selection.Replace What:="-", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2```
UsedRange is not always a reliable to find the last row, so try something like this:
Sub Tester()
Dim ws As Worksheet, rng As Range
Dim lr As Long
Set ws = ActiveSheet
lr = LastUsedRow(ws)
If ws.Rows(lr).Hidden Then lr = lr - 1 'skip last row if hidden
Set rng = ws.Range("A6:A" & lr)
Debug.Print "copying", rng.Address
rng.Copy rng.Offset(0, 1) 'copy to colB
rng.Offset(0, 1).Replace What:="-", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
End Sub
'find the last used row on a worksheet
Function LastUsedRow(ws As Worksheet)
Dim f As Range
Set f = ws.Cells.Find(What:="*", After:=ws.Range("A1"), _
LookAt:=xlPart, LookIn:=xlFormulas, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
If Not f Is Nothing Then LastUsedRow = f.Row 'otherwise zero
End Function

Last used cell in excel is so large excel crashes when trying to delete excess columns and rows

I have a worksheet where the last used cell is said to be cells(1048381,BH). I know I'm supposed to delete all rows (and columns, but less relevant in this particular case as all columns through BH are actually be used) until the final row, but the final row is such a large number that excel (64-bit) crashes when I go to fix the problem. Any recommendations?
Thanks!
You don't actually have to delete the rows between the last valid row of data and a rogue last cell. Using Home, Editing, Clear, Clear All is sufficient and is much less calculation intensive.
Here is some code that performs the same action on a rogue populated cell.
Option Explicit
Sub resetRogueRow()
Dim lr As Long, nr As Long
With Worksheets("sheet2")
lr = .Cells.Find(What:="*", _
After:=.Cells(1), _
LookAt:=xlWhole, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
nr = .Cells.Find(What:="*", _
After:=.Cells(lr, 1), _
LookAt:=xlWhole, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
.Cells(nr + 1, "A").Resize(lr - nr, 1).EntireColumn.Clear
End With
End Sub

Excel VBA: setting cell value based on a search

I'm trying to set the ActiveCell's value as follows:
I need to look at the value of the cell that is one row above the active cell (in the same column)
I then need to find this value in the 3rd column of the 'Source - Questions' sheet; I need to start the search from the bottom going up because I need to find the last instance of this value
When I find this cell I need to take the value of the next cell that is just under the cell that was found (in the same column)
This is my code, I get an exception without any helpful information.
ActiveCell.Value = Cells.Find(What:=ActiveCell.Offset(-1, 0).Value,
After:=Sheets("Source - Questions").Cell(1000, 3), LookIn:=xlFormulas,
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious,
MatchCase:=False, SearchFormat:=False).Offset(1, 0).Value
Any help would be appreciated.
Cheers
Correct the two small errors:
Sub asdf()
Dim r As Range, s As Worksheet, v As Variant
Set s = Sheets("Source - Questions")
v = ActiveCell.Offset(-1, 0).Value
Set r = s.Cells.Find(What:=v, _
After:=s.Cells(1000, 3), _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False, _
SearchFormat:=False).Offset(1, 0)
ActiveCell.Value = r.Value
End Sub

Resources