First used row in my selected column - vba

I want to clear all cells in my worksheet from first used to last used row in my selected column, but first I must know the first used row in my selected column. How can I find the first row in "X" column? (X may = {A, B, C, ...})

Try
firstUsedRow = Worksheets("Sheet1").Cells(1, 1).End(xlDown).Row
Where the second argument in Cells(1, 1) is the column number (e.g., A=1, B=2, etc.). Sheet1 is the worksheet name of the worksheet in question.
It is possible, though unlikely in your case, that the first row is row 1. In this case, the above code will actually select your last row of the used range. As a safety measure for this case, identify the last row and make sure your first row is either 1 or the row generated by the above code. So
finalRow = Worksheets("Sheet1").Cells(Worksheets("Sheet1").UsedRange.Rows.Count, 1).End(xlUp).Row
if finalRow = firstUsedRow then
firstUsedRow = 1
End If
All together
firstUsedRow = Worksheets("Sheet1").Cells(1, 1).End(xlDown).Row
finalRow = Worksheets("Sheet1").Cells(Worksheets("Sheet1").UsedRange.Rows.Count, 1).End(xlUp).Row
if finalRow = firstUsedRow then
firstUsedRow = 1
End If

Kindly see the code below. It will skip over all the blank rows in column A represented by second argument of Cells as 1. Then assign the row positions for variables firstRow and lastRow. Last clear the values from that range.
Sub ClearCells()
Dim i As Integer
Dim firstRow As Integer
Dim lastRow As Integer
i = 1
Do While Cells(i, 1) = ""
i = i + 1
Loop
firstRow = i
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Range(Cells(firstRow, 1), Cells(lastRow, 1)).ClearContents
End Sub

See below with no loop.
Sub ClearCells()
Dim firstRow As Integer
Dim lastRow As Integer
firstRow = Cells(1, 1).End(xlDown).Row
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Range(Cells(firstRow, 1), Cells(lastRow, 1)).ClearContents
End Sub

Related

Iterate through matrix dynamically with filter

I have a matrix with lots of rows which do have different row lengths. As you can see in the image below, row 3 ends at column T, whereas row 8 ends at column L. What I am trying to do is to loop through every row of the matrix with the fleet name "Taxi" and sum up every entry.
How can I accomplish this? The main problem I face is with iterating through the rows and finding the end of the row dynamically and how to filter the table beforehand by the fleetname.
Code does what you want
Dim sht As Worksheet
Dim LastColumn As Long
Dim LastRow as Long
Dim i as Long
Dim dblSum as Double 'your sum
Set sht = ThisWorkbook.ActiveSheet
With sht
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i=2 to LastRow
If .Cells(i,1).Value = "Taxi" Then
LastColumn = .Cells(i, .Columns.Count).End(xlToLeft).Column
dblSum = Application.Sum(.Range(.Cells(i, 2), .Cells(i, LastColumn)))
End if
Next i
End with
Set sht = Nothing

Excel VBA - selecting range to last completely blank row

I have some VBA code that needs to select a range from A84 to X. I'm using this code to select that range to the last row in the data.
Dim Lastrow As Integer
Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Range("A84:X" & Lastrow).Select
This stops at the first row with a blank cell in the A column. I need it to check columns A through Z, not just A. For example, if there is a value in column A on rows 84 to 94, but for row 95, there's only data in column F or R, it won't include row 95. How can this look at columns A:Z to determine whether or not the row is blank?
simply set Lastrow to
Lastrow = Range("A:Z").Find("*", , , , xlByRows, xlPrevious).Row
I'm all but certain there is a better way, but one such way is to loop through you columns and find the last row of each and then determine which one of those is the greatest.
maxRow = 0
for i=1 to 24 'A through X
if maxRow < ActiveSheet.Cells(Rows.Count, i).End(xlUp).Row then
maxRow = ActiveSheet.Cells(Rows.Count, i).End(xlUp).Row
end if
next i
Then maxRow is what you want lastrow to be.
You could iterate through all of the columns and compare the last row of each column. You could then change the lastRow variable whenever you find a column with more rows like so:
Dim i As Integer
Dim lastRow As Integer
lastRow = 1
For i = 1 To 26
Dim lst As Integer
lst = Cells(Rows.Count, i).End(xlUp).Row
If lst > lastRow Then
lastRow = lst
End If
Next
Range("A84:X" & lastRow).Select
You can make use of the usedrange function, which returns the complete range of used cells on your sheet:, as follows:
Worksheets("worksheetName").UsedRange.lastRow
This would give you the last used row, not just the last row with data in the first column

Identify a given range with VBA

I have a list of part numbers as shown below, each one has a range start and end value which needs to replace the *** in the part number.
I am trying to identify the range start and end value, look them up on the range list on sheet2 (RangeTable) and then concatenate that with the part number individually, so for each part number in column A, a range value defined in column B and C will be concatenated on sheet3 (PartNumbers).
The code is not identifying the Range Start and End value and is creating a part number for all values on the Range Table.
Sub CompilePartNums()
Dim WS1 As Worksheet
Dim WS2 As Worksheet
Dim WS3 As Worksheet
Dim LR1 As Long
Dim LR2 As Long
Dim LR3 As Long
Dim Ctr1 As Long
Dim Ctr2 As Long
Set WS1 = Worksheets("Tabelle1")
Set WS2 = Worksheets("RangeTable")
Set WS3 = Worksheets("PartNumbers")
With WS1
LR1 = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With WS2
LR2 = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
LR3 = 2
For Ctr1 = 2 To LR1
For Ctr2 = 2 To LR2
Select Case WS2.Cells(Ctr2, 1)
Case WS1.Cells(Ctr1, 2) To WS1.Cells(Ctr1, 3)
WS3.Cells(LR3, 1) = replace(WS1.Cells(Ctr1, 1), "***", WS2.Cells(Ctr2, 1))
LR3 = LR3 + 1
End Select
Next
Next
End Sub
Sheet1 (Tabelle1) Looks like this and continues down to ROW1210
Sheet2 (RangeTable) looks like this, the values in this need to stay in this order which are not sequential
Sheet3 (PartNumbers) is the results that are output, it should stop at 390 however it continues thru all the RangeTable values.
I'll post this as an "answer" for the moment, simply because it is too complicated to explain in a comment, but I think it might have something to do with your issue.
You have a Select Case statement of:
Select Case WS2.Cells(Ctr2, 1)
Case WS1.Cells(Ctr1, 2) To WS1.Cells(Ctr1, 3)
WS3.Cells(LR3, 1) = replace(WS1.Cells(Ctr1, 1), "***", WS2.Cells(Ctr2, 1))
LR3 = LR3 + 1
End Select
That statement is effectively equivalent to
If WS2.Cells(Ctr2, 1) >= WS1.Cells(Ctr1, 2) And _
WS2.Cells(Ctr2, 1) <= WS1.Cells(Ctr1, 3) Then
WS3.Cells(LR3, 1) = replace(WS1.Cells(Ctr1, 1), "***", WS2.Cells(Ctr2, 1))
LR3 = LR3 + 1
End If
And that statement is basically saying that if a record in column A of the RangeTable sheet is greater than or equal to the value in column B of the Tabelle1 sheet and less than or equal to the value in column C of the Tabelle1 sheet, then create a new cell in column A of the PartNumbers sheet with a value of column A of the Tabelle1 sheet (with any "***" in that value replaced by the value in column A of the RangeTable sheet).
Considering that none of your sheets have anything in column B or C, I can't understand how your code managed to write anything to the PartNumbers sheet ?!?!

Excel VBA: How to Find a Value in a Range of Columns and Move the Row Left 1

I run a daily report of our account details and the headers on the Excel file are always mis-aligned. I would like a macro to help with this, but I am a beginner with macros and don't even know where to start.
On any active sheet, I would like to find a specific word in column B ("Trx Date") and move the row over 1 to the left. The reports will always have the same column format, but the rows can fluctuate depending on the amount of detail.
"Trx Date" is the first header title, but it starts in column B and all the data starts in column A. There is other information in column B so the word "Trx Date" would need to be found first. Any ideas? I am new to the site so it will not allow me to upload an image. Thank you all!
This is what I was trying with no luck.....
Dim Firstrow As Long
Dim Lastrow As Long
Dim FinalRow As Long
With ActiveSheet.Select
Firstrow = 7
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).row
For FinalRow = Lastrow To Firstrow Step -1
With .Cells(FinalRow, "B")
If Not IsError(.value) Then
Select Case .value
Case Is = "Trx Date": .Offset(, -1).Delete Shift:=xlLeft
End Select
End If
End With
Next FinalRow
End With
This should work
Dim Firstrow As Long
Dim Lastrow As Long
Dim FinalRow As Long
Firstrow = 7
Lastrow = Cells(Rows.Count, 2).End(xlUp).Row
For FinalRow = Lastrow To Firstrow Step -1
With Cells(FinalRow, 2)
Select Case .Value
Case "TRX Date": Cells(FinalRow, 1).Delete Shift:=xlToLeft
End Select
End With
Next FinalRow

VBA Delete all cells in each row except the last used cell

Trying to write a script to delete all cells in a row except the last used cell, then move all remaining data to the first column.
See below:
|--| is an empty cell
|XX| is a cell with unneeded data
|DATA| is the cell with Data I require.
Before:
|--|--|--|XX|XX|XX|XX|DATA|
|--|--|XX|XX|XX|DATA|
After
|DATA|
|DATA|
Assuming you meant in Excel:
Process:
Loop over the sheet
Set a tempVal variable to the value of the lastColumn
Loop through the columns to the last column for that row, clearing each cell
Set the value of column A to the tempVal
TESTED:
Sub GetLastColumnValue()
Dim lastRow As Long
Dim lastCol As Long
Dim sheet As String
Dim tempVal As String
sheet = "Sheet1"
lastRow = Sheets(sheet).Range("A" & Rows.Count).End(xlUp).row 'Using Range()
For lRow = 2 To lastRow
lastCol = Sheets(sheet).Cells(lRow, Columns.Count).End(xlToLeft).Column
tempVal = Sheets(sheet).Cells(lRow, lastCol).Text
For lCol = 1 To lastCol
Sheets(sheet).Cells(lRow, lCol).ClearContents
Next lCol
Sheets(sheet).Cells(lRow, 1) = tempVal
Next lRow
End Sub