Reputation: 11
I want to speed up some of my current tasks.
The steps I saw best fit
Public Sub CopySheetAndRename()
Application.OnKey "^+k", "CopySheetAndRename"
Dim newName As String
On Error Resume Next
newName = InputBox("Enter the name for the copied worksheet")
If newName <> "" Then
ActiveSheet.Copy Before:=Sheets(1)
On Error Resume Next
ActiveSheet.Name = newName
End If
Dim lr As Long
lr = Cells(Rows.Count, 12).End(3).Row
Range("I9:I" & lr).Copy
ActiveSheet.Range("D9").PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("E9:H24").Clear
Rows("25:29").Insert Shift:=xlDown, _
CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
I have gotten thru steps 1-4 and step 6.
Assume P2 is the most current tab, and P3 is the tab that will be created. I took that screenshot after running my macro, I should have deleted that tab before uploading here.
Per the example, all workbooks (150 plus) have the same headers in rows 1-7, a blank row at 8, and the data starting in row 9. All workbooks have data starting from A9:M. The number of rows is inconsistent.
I want to start pulling data from row 9 down to the last column with data before the first blank row (above the totals line).
How do I delete sheet rows from D9:D for all cells with values = 0? This can't delete rows with blank cells, only cells = 0.
Upvotes: 0
Views: 1141
Reputation: 1033
example of the simple loop via column D to delete 0 values (not empty ones). the main row is
If ws.Cells(c, 4).Value = 0 And ws.Cells(c, 4).Value <> "" Then
Note, deleting rows via Loop is always better from the bottom to the top:
Option Explicit
Sub Macro4()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.Sheets("P3")
Dim c As Long
For c = ws.Cells(ws.Rows.Count, "D").End(xlUp).row -5 To 2 Step -1 ' note -5 is to offset last row number due to Total/Diff/Balance rows. Or simply use another "clean column" to get the last row number
If ws.Cells(c, 4).Value = 0 And ws.Cells(c, 4).Value <> "" Then
Rows(c).EntireRow.Delete
End If
Next c
End Sub
Upvotes: 0