Formatting as a table dynamically Excel VBA - vba

I would like to format a certain range in a worksheet as a table in Excel. The formatting will always start in row 10.
In order to do so, I have written the following code:
Set rng = Range(Range("B10"), Range("B10").End(xlUp).SpecialCells(xlLastCell))
Set table = Sheets("Results").ListObjects.Add(xlSrcRange, rng, , xlYes)
table.TableStyle = "TableStyleMedium13"
As of now, the formatting is done from row 10 until the end of the worksheet - even in empty rows. However, I would like the table to be formatted only up until the last row of data and for it to do this dynamically given the fact that the amount of data will vary. How can I do this?

The code below will format all cells from "B10" until last row with data in Column B (it will also format blank rows in the middle, in case you have gaps).
Dim LastRow As Long
With Sheets("Results")
' find last row with data in Column B
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
' set Rng from B10 untill last row with data in Column B
Set Rng = Range("B10:B" & LastRow)
Set Table = .ListObjects.Add(xlSrcRange, Rng, , xlYes)
Table.TableStyle = "TableStyleMedium13"
End With

Range("B" & Rows.Count).End(xlUp)
This should work - will simply identify last populated row.

Related

Selecting all non-blank cells in variable range in Excel VBA

I have I data set on Excel. Starting at column B, it has continuous data from B3 to a variable number that periodically get larger (today it is B114, but tomorrow the data may extend to B116, for example). The data in cell B in continuous and is never deleted. For every row of continuous data in column B, I want to select column B-AG's rows as well. However, the rows after B do NOT have continuous data.
For example: There is continuous data from B3 to B120. I want to select the range B3:AG120.
The code I have written to do this in VBA is not working. It correctly stops at B120 (in this example), however, once it reaches the non-continuous data in columns C-AG, it freaks out and selects rows past 120. I am not positive why this code is not working, any help is much appreciated!
For the record, there are formulas in nearly every cell in the sheet. Only some formula populate the cell with data, however. I want to select every cell regardless of if it is populated with data IF IT IS IN MY RANGE. Otherwise, I do not want to select it. For example, past B120 there are empty cells with formulas in them. I do not want to include those in my range. But if there is an empty cell in D40 (in between B3 and AG120) I do want to include that in the selection.
Dim LR As Long, cell As Range, rng As Range
With Sheets("Sortable(2)")
LR = .Range("B" & Rows.Count).End(xlUp).Row
For Each cell In .Range("B3:B" & LR)
If cell.Value <> "" Then
If rng Is Nothing Then
Set rng = cell
Else
Set rng = Union(rng, cell)
End If
End If
Next cell
rng.Select
End With
Dim lastVal As Range, sht As Worksheet
Set sht = Sheets("Sortable(2)")
Set lastVal = sht.Columns(2).Find("*", sht.Cells(1, 2), xlValues, _
xlPart, xlByColumns, xlPrevious)
Debug.Print lastVal.Address
sht.Range("B2", lastVal).Resize(, 32).Select 'select B:AG

Excel VBA - Delete Rows Based on Criteria

I have a report that I pull everyday that is placed in a very awekward format. It's contains a variable row count by 4 columns organized into unofficial tables based on the Name of each employee.
What I have is an employee name in column B preceded 2 blank rows above and followed by 1 blank row of data below.
What I want to accomplish is loop through the data, identify cells in column B <> blank, delete the entire 2 rows below that cell, and delete the entire 1 row above that cell.
Below is what I have so far. not much:
Sub test()
Dim currentSht As Worksheet
Dim startCell As Range
Dim lastRow As Long
Dim lastCol As Long
Dim i as integer
Set currentSht = ActiveWorkbook.Sheets(1)
Set startCell = currentSht.Range("A1")
lastRow = startCell.SpecialCells(xlCellTypeLastCell).Row
lastCol = startCell.SpecialCells(xlCellTypeLastCell).Column
For i = lastRow To 1
If Cells(i, "B").Value <> "" Then
End Sub
without making major changes to your code, try this:
For i = lastRow To 1 Step - 1
If Cells(i, "B").Value <> "" Then
Range(Cells(i, "B").Offset(1), Cells(i, "B").Offset(2)).EntireRow.Delete 'delete two below
Cells(i, "B").Offset(-1).EntireRow.Delete ' delete one above
You already get to your non-blank cell (ie Cells(i,"b")). To reference a range in relation to a cell you already have, use OFFSET.
So, and in this order, you select a range of cells from one below your cell Offset(1) to two cells below Offset(2)'. Change this range toENTIREROW` for those cells, and delete.
Then you select the cell above Offset(-1), select the ENTIREROW and delete.
as per your question narrative you'd possibly need to delete all rows that has a blank cell in column "B"
should that be the issue than you could (disclaimer: test it on a copy sheet!) simply go like follows:
Sub test()
With ActiveWorkbook.Sheets(1)
.Range("A1", .Cells(.Rows.Count, "A").End(xlUp)).Offset(, 1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With
End Sub

Selecting range without knowing number of rows or columns having data in Excel/VBA

I am looking for code for two different types of selection. One code would select in an L shape all of the rows in one column and all of the columns in one row. In the example of having data in the range A1:A10, and data in row 10 only from col A - K. The selection would look like an L. How can you do this without knowing how many rows or columns have data in them?
The second code would have the same data, but need to select the whole range A1:K10 in that example, but the code would need to select whatever range had the data.
i found the answer. i have to do a union. here is the code with the union at the end.
Sub mywork()
Dim ws As Worksheet
Dim lRow As Long, lCol As Long
Dim rng As Range
'~~> Set this to the relevant worksheet
Set ws = [Sheet1]
With ws
'~~> Get the last row and last column
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
lCol = .Cells(lRow, .Columns.Count).End(xlToLeft).Column
'~~> Set the range
Set rng = .Range(.Cells(lRow, 1), .Cells(lRow, lCol))
End With
Set rng = Application.Union(Range("A1:A" & lRow), rng)
rng.Select
End Sub
activesheet.usedrange.address should tell you the used range.
In your case something like this should work: [sheet1].usedrange.select (Replaces all the code in the module)
The benefit here is the fact that you are not hard coding "A1:A" against the last identified cell, works well if you have blank rows at the top.

Replacing a cell value with another in the last row of a sheet

I am trying to replace a value in the first column in the last row of a spreadsheet with another value. For example I have a workbook with two sheets called Input and Data. I have a value on the input sheet in cell A1. I would like this value to replace the value located in column A in the last row with a value in the Data sheet. I know how to locate last row of the worksheet using the code below, but I am not sure how to input a value located in this last row. What can I add to this in order to retrieve the value I have in the sheet Input in cell A1, and replace the value in column A in the last row of the Data sheet
Dim LR1 As Long
'Selects the last row with values
With Sheets("Data")
LR1 = .Range("A" & .Rows.Count).End(xlUp).Row
End With
Full solution below:
Dim LR1 As Long
With Sheets("Data")
LR1 = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A" & LR1) = Sheets("Input").Range("A1")
End With

Check which is the last cell in a specific column which has a value and select the cell below it in excel vba

Hello I am trying to write some code to get the last cell in a specific column (e.g Column A) which has a value. I only know this:
Cells(3, 2).Value = quantity
To determine the last row use below formula
lastRow = Cells(rows.count,1).End(xlUp).row
rows.count = total number of rows in that version of Excel (65k in
2003 etc) , i.e the last row in that verison of Excel
1 on the right of rows.count = Column A, 2 would be coulmn B and so on
xlUp roughly means it will search bottom to up
You can search more if you would like on this command.
Now once you know the last row, then to fetch the value of it use
Variable = Cells(lastRow,1).Value
something like this which checks:
there is at least one value in column A (else no selection)
that the last value is not in the last row (avoiding an error when trying to offset a row)
code
Sub LastRow()
Dim rng1 As Range
Set rng1 = Columns("A").Find("*", [a1], xlValues, , xlByRows, xlPrevious)
If Not rng1 Is Nothing Then
If rng1.Row <> Rows.Count Then rng1.Offset(1, 0).Activate
End If
End Sub
If you want to find the row for the last cell in a range it is better to use the find function.
As an example: For a specific column n; use the code below to set it to quantity
Dim n As Long, quantity As Long
n = 3
quantity = 5000
Cells(Columns(n).Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, _
SearchDirection:=xlPrevious).Row, n).Value = quantity