I know this is a pretty basic question, but im still working on building my VBA skills. I am in a predicament where I have made a mapping system of various reports I receive that get placed in a compiled workbook. These reports have entirely different formats etc. I have a copy/paste macro that copies columns and places them in their correct position on the compiled workbook.
I’ve come into situations however where there are a lot of duplicate / empty rows that screw up my Macro. I have used two VBA functions to solve this, one is a “delete row if reference column is blank”:
Sub DeleteBlankARows() With Application .Calculation = xlCalculationManual .ScreenUpdating = False Dim r As Long For r = Cells(Rows.Count, 6).End(xlUp).Row To 1 Step -1 If Cells(r, 6) = "" Then Rows(r).Delete Next r .Calculation = xlCalculationAutomatic .ScreenUpdating = True End With End Sub
This deletes rows where cells in column F are empty
I also use a copy/paste down macro:
Sub CopyUntilBlank() Dim last_row As Integer last_row = Range("f1").End(xlDown).Row Dim rng As Range Set rng = Range("d2:d" & last_row) For Each cell In rng.Cells cell.Activate If ActiveCell.Value = "" Then ActiveCell.Value = ActiveCell.Offset(-1, 0).Value End If Next cell End Sub
This copies and pastes down blank rows in column D until you hit a non-blank cell then re-does this until the range of values in column F.
These macros work well for me, but because I have multiple sheets like this, I would like to create a cell references that make the ranges dynamic. For instance: in the DeleteBlankRows macro, I would like to have the column reference in Cells(r,6) be determined off of a cell value in sheet1 – so for instance if the value in cell A1 on sheet 1 is 2 it would change the column reference to “2” (column B).
I would like the same to happen for the copy/paste down macro. I’m pretty sure this is just some reference to A1.Value but I don’t know how to properly write such thing.
Thank you for your support, I’ve gone quite a long way with all the support of the community.
An example using your first sub:
Sub DeleteBlankARows(colIndex as Long) Dim colIndex as long colIndex = Sheet1.Range("a1").value With Application .Calculation = xlCalculationManual .ScreenUpdating = False Dim r As Long For r = Cells(Rows.Count, colIndex).End(xlUp).Row To 1 Step -1 If Cells(r, colIndex) = "" Then Rows(r).Delete Next r .Calculation = xlCalculationAutomatic .ScreenUpdating = True End With End Sub
It’s not clear from your question which column in the second sub needs to be dynamic (or both of them?)
EDIT try this:
Sub CopyUntilBlank() Dim last_row As Long, col1 as Long, col2 as Long Dim rng as Range col1 = Sheet1.Range("a2").value col2 = Sheet1.Range("a3").value last_row = Cells(1, col1).End(xlDown).Row 'This next line is better if there's any chance ' of blanks in this column 'last_row = Cells(Rows.Count, col1).End(xlUp).Row With ActiveSheet Set rng = .Range(.Cells(2, col2), .Cells(last_row, col2)) End With For Each cell In rng.Cells If cell.Value = "" Then cell.Value = cell.Offset(-1, 0).Value End If Next cell End Sub