VBA Macro Delete Rows based on blank column

HazatB

New Member
Joined
Sep 19, 2017
Messages
32
Hello,

I have developed this code to perform the task of copying data from one workbook into another and being able to adjust for more or less rows. So far from help here I have gotten nearly to my desired result. The two issues I am having is (1) If the Previous data that is getting cleared out has less rows than the new data if may take several re-runs of the code for it to properly populate & (2) When attempting to delete blank rows based on column A data being empty it has an error if there is nothing to delete which I would assume requires me to put an IF in the code but I have had problems with that, The code is pasted below.

LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row

Rows(LR).Delete

Workbooks.OpenText Filename:= _
"K:\1900\dwprod1900\data\export\general\before_n_after_remap_audit_umroi.txt", _
Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 2), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1)), _
TrailingMinusNumbers:=True
Windows("UMROI_Standard Cost Audit Reports.xlsm").Activate

Dim LastRow As Long
Dim wks1 As Worksheet
Dim wks2 As Worksheet

Set wks2 = Workbooks("UMROI_Standard Cost Audit Reports.xlsm").Worksheets("Before n After Remap Review")
Set wks1 = Workbooks("before_n_after_remap_audit_umroi.txt").Worksheets("before_n_after_remap_audit_umro")

LastRow = Workbooks("before_n_after_remap_audit_umroi.txt").Worksheets("before_n_after_remap_audit_umro").Range("A:E").Find("*", , xlValues, xlPart, xlByRows, xlPrevious, False).Row
LastRow = Workbooks("before_n_after_remap_audit_umroi.txt").Worksheets("before_n_after_remap_audit_umro").Range("F:G").Find("*", , xlValues, xlPart, xlByRows, xlPrevious, False).Row
LastRow = Workbooks("UMROI_Standard Cost Audit Reports.xlsm").Worksheets("Before n After Remap Review").Range("A:E").Find("*", , xlValues, xlPart, xlByRows, xlPrevious, False).Row
LastRow = Workbooks("UMROI_Standard Cost Audit Reports.xlsm").Worksheets("Before n After Remap Review").Range("I:J").Find("*", , xlValues, xlPart, xlByRows, xlPrevious, False).Row

Worksheets("Before n After Remap Review").Range("A7:E" & LastRow).Select
Selection.ClearContents

Worksheets("Before n After Remap Review").Range("I7:J" & LastRow).Select
Selection.ClearContents

Workbooks("before_n_after_remap_audit_umroi.txt").Worksheets("before_n_after_remap_audit_umro").Range("A1:E" & LastRow).Copy
Workbooks("UMROI_Standard Cost Audit Reports.xlsm").Worksheets("Before n After Remap Review").Range("A7:E" & LastRow).PasteSpecial

Application.CutCopyMode = False

Workbooks("before_n_after_remap_audit_umroi.txt").Worksheets("before_n_after_remap_audit_umro").Range("F1:G" & LastRow).Copy
Workbooks("UMROI_Standard Cost Audit Reports.xlsm").Worksheets("Before n After Remap Review").Range("I7:J" & LastRow).PasteSpecial

Application.CutCopyMode = False
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("F7").AutoFill Destination:=Range("F7:F" & LastRow), Type:=xlFillDefault
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("G7").AutoFill Destination:=Range("G7:G" & LastRow), Type:=xlFillDefault
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("K7").AutoFill Destination:=Range("K7:K" & LastRow), Type:=xlFillDefault
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("L7").AutoFill Destination:=Range("L7:L" & LastRow), Type:=xlFillDefault
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("N7").AutoFill Destination:=Range("N7:N" & LastRow), Type:=xlFillDefault

With wks2
LastRow = .Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
wks2.Range("A7:E" & LastRow).Value = .Range("A7:E" & LastRow).Value
wks2.Range("I7:J" & LastRow).Value = .Range("I7:J" & LastRow).Value
End With

With wks2
With .Cells(LastRow + 1, 4)
.Formula = "=SUM(D7:D" & LastRow & ")"
.Resize(, 3).FillRight
End With
.Calculate
.Parent.Activate
With .Cells(LastRow + 1, 9)
.Formula = "=SUM(I7:I" & LastRow & ")"
.Resize(, 3).FillRight
End With
.Calculate
.Parent.Activate
With .Cells(LastRow + 1, 7)
.Offset(1).Formula = Replace("=IF($D@LastRow + 1=0,IF($F@LastRow + 1=0,0,IF($F@LastRow + 1<>0,1,$F@LastRow + 1/$D@LastRow + 1)),$F@LastRow + 1/$D@LastRow + 1)", "@LastRow + 1", LastRow + 1)
.Resize(, 1).FillRight
End With
With .Cells(LastRow + 1, 12)
.Offset(1).Formula = Replace("=IF($I@LastRow + 1=0,IF($K@LastRow + 1=0,0,IF($K@LastRow + 1<>0,1,$K@LastRow + 1/$I@LastRow + 1)),$K@LastRow + 1/$I@LastRow + 1)", "@LastRow + 1", LastRow + 1)
.Resize(, 1).FillRight
End With
With .Cells(LastRow + 1, 14)
.Offset(1).Formula = "=(L" & LastRow + 2 & " - G" & LastRow + 2 & ")"
.Resize(, 1).FillRight
End With
End With

Application.ScreenUpdating = True

Set wks1 = Nothing
Set wks2 = Nothing

Range("G" & LastRow + 2).Select
Selection.Cut
Range("G" & LastRow + 1).Select
ActiveSheet.Paste
Range("L" & LastRow + 2).Select
Selection.Cut
Range("L" & LastRow + 1).Select
ActiveSheet.Paste
Range("N" & LastRow + 2).Select
Selection.Cut
Range("N" & LastRow + 1).Select
ActiveSheet.Paste

Range("A7:A" & LastRow).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top