Delete a range of columns using two named ranges - vba

I need to delete a range of rows using two named ranges
Current find the column numbers for the last column (lastColumn) and then the column 6 spots behind it (colrange)
Dim lastColumn As Integer
With ActiveSheet
lastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
colrange = lastColumn - 6
I want to delete the columns in the range colrange:lastColumn but not sure how to do this?

Range(Cells(1,colRange),Cells(1,lastColumn)).EntireColumn.Delete

you can simply use:
Range(Columns(colRange),Columns(lastColumn)).Delete
or directly:
With ActiveSheet
.Range(.Columns(.Cells(1, .Columns.Count).End(xlToLeft).Column - 6), .Columns(.Cells(1, .Columns.Count).End(xlToLeft).Column)).Delete
End With

Related

Custom selection in row with vba

My problem is pretty straight-forward: how do I define the range from a certain cell to the end of the row?
If you would know what am I trying to achieve, you would understand the situation better: I want to sort rows individually but every row's datas start at 17. column, so I'm trying to make the sorting from 17. column to end of the column count.
Thanks in advance!
Defining range in VBA: Dim rng As Range.
Setting range (since it's object, you must use Set keyword): Set rng = Range("A1:B2")
or
Set rng = Range(Cells(1, 1), Cells(2, 2)) - both are equivalent.
So, if you want to specify custom range in ONE row starting from 17th column, you must also know where the rows ends, you can do that in two ways:
Dim lastCol As Long
lastCol = Cells(rowNumber, 17).End(xlToRight).Column
'alternative, but not equivalent
lastCol = Cells(rowNumber, Columns.Count).End(xlToLeft).Column
Where rowNumber is partciular number of of a row.
Now, having lastCol we can define the range:
Set rng = Range(Cells(rowNumber, 17), Cells(rowNumber, lastCol))
Again, rowNumber is partciular number of of a row.

How to copy column from last column N number of times?

I currently have the following code, which was made by a lovely member here
Sub YearsNumberReduction()
Dim LastCol As Long
Dim DelCnt As Integer
DelCnt = Sheets("Panel").Range("E19").Value
With ThisWorkbook.Sheets("Current")
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
If LastCol >= DelCnt Then
.Range(.Cells(1, LastCol - DelCnt + 1), .Cells(1, LastCol)).EntireColumn.Delete
End If
End With
This code takes the last column, and deletes the column by the number of times specified in E19. How can I change the code, so instead of deleting a specified number of times from the last row, we can add columns instead? Essentially reversing the role of that VBA Macro.
I also have another question. In my Excel solution, I constantly add the number of years and decrease them (those years are business years and each year carries a profit, calculated by revenue-costs). On the main worksheet, I have 10 years. That is the default number of years. However, this does not change when I add or decrease years based on the function I mentioned. The profit for the 10 years is calculated by =SUM(B29:B39). How can I make the range change (e.g if I add one more year, Year 11 will be added and =SUM(B29:40) in the main spreadsheet which gives a profit overview?
Current adding of columns
Worksheets("Sheet1").Range("L1:L15").AutoFill Destination:=Worksheets("Sheet1").Range("L1").Resize(15, Worksheets("Panel").Range("E17") + 1), Type:=xlFillDefault
However, this adds from a fixed column, rather than the last. So I extend columns by 5, it will add 5, but if I extend it by 5 again, it wont extend since it has a fixed point. So I need it to change based on the last column.
EDIT
Year 1 Year 2 Year 3 Year 3 Year 3
Sales 100 115 132 132 152
Costs 30 32 33 33 35
Profit 70 84 99 99 99
Year 3 stays the same, and costs do not update (=C3*1.05) it should rise by 5%
Try the following. Assumes you can use column A to find the last used row:
Sub YearsNumberReduction()
Dim LastCol As Long
Dim DelCnt As Integer
DelCnt = Sheets("Panel").Range("E19").Value
Dim copyCol As Range
Dim DestRange As Range
Dim lastRow As Long
With ThisWorkbook.Sheets("Current")
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set copyCol = .Range(.Cells(1, LastCol), .Cells(lastRow, LastCol))
Set DestRange = .Range(.Cells(1, LastCol), .Cells(lastRow, LastCol + DelCnt))
copyCol.AutoFill Destination:= DestRange, Type:=xlFillDefault
End With
End Sub

VBA copy/paste range into rows below if there is a value in column a

I am trying to create a macro that will copy a range of values say B6:BM6 and paste it in the row below IF there is a value in column A.
So: if column A6 is populated, copy range from rows above and paste them into B6. Loop until there is no value in column A.
Sub fillEmptycells()
Dim i As Integer
Dim lastRow As Integer
With ActiveSheet
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If IsEmpty(.Cells(i, 2)) And Not IsEmpty(.Cells(i, 1)) Then
.Range(Cells(i - 1, 2), Cells(i - 1, 100)).Copy .Cells(i, 2)
End If
Next i
End With
End Sub
Can someone help me please?
At the moment you are only copying one cell, not a whole range. You need to change the range you call the Copy method on to include all of the cells you want to copy. The code in your If statement would be changed to something like:
.Range(Cells(i-1,2),Cells(i-1,x)).Copy .Cells(i,2)
Where x would be the column number of the last column you want to copy.

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.