How to detect a blank range and paste data?
Our take
I have a document that I am trying to format so it works as a source for a pivot table.
Currently it has too much data in column A, so my Macro goes through column A to insert cells to adjust data over so it can be properly nested in categories. This creates blank cells where I then want to copy the data above it and paste so everything is labeled correctly on the pivot table. I'd like to paste to the entire range of blank cells in columns A and B, so I don't need to check each cell each time. This spreadsheet is over 5k lines, so it takes ~10 minutes to run currently.
Original Data:
| Award/Grant | Budget | Current Actuals | LTD Actuals |
|---|---|---|---|
| AWD-154 | 50,000 | 12,500 | 40,000 |
| GR234 | 25,000 | 600 | 1,500 |
| Obj-123 | 400 | 20 | 250 |
Column corrected data:
| Award | Grant | Object | Budget | Current Actuals | LTD Actuals |
|---|---|---|---|---|---|
| AWD-154 | 50,000 | 12,500 | 40,000 | ||
| GR234 | 25,000 | 600 | 1,500 | ||
| Obj-123 | 400 | 20 | 250 |
Now I copy the AWD cell to the one below it, next to Grant, and delete the original line. since I don't care about the AWD total row.
| Award | Grant | Object | Budget | Current Actuals | LTD Actuals |
|---|---|---|---|---|---|
| AWD-154 | GR234 | 25,000 | 600 | 1,500 | |
| Obj-123 | 400 | 20 | 250 |
I can have anywhere from 2 - 10+ Obj rows here, so I'd rather paste to the entire range instead of line by line as I'm doing now.
.... ThirdDone: Cells(13, 2).Select limit = 0 Do Until limit = 200 If ActiveCell.Value = "" Then ActiveCell.Offset(-1, 0).Select ActiveCell.Copy ActiveCell.Offset(1, 0).Select ActiveCell.PasteSpecial xlPasteAllMergingConditionalFormats ActiveCell.Offset(1, 0).Select limit = 0 Else If ActiveCell.Value = "Total" Then GoTo ThirdDone Else limit = limit + 1 ActiveCell.Offset(1, 0).Select End If End If Loop End Sub [link] [comments]
Read on the original site
Open the publisher's page for the full experience
Related Articles
- Xlookup reference is shifting over each time after I use MacroHey everyone, I have my main table where I want to display the cleaned up data. Essentially I'm using xlookup references from sheet two. When I run my macro in sheet two, for some reason my xlookup on sheet 1 moves over every single time. For example... this is what the xlookup formula looks at before vs after the Macro Before Macro - =XLOOKUP(A3,MTD!$A$3:$A$15,MTD!$B$3:$H$15) After Macro - =XLOOKUP(A3,MTD!$A$3:$A$15,MTD!$B$3:$F$15) Why would it move from H15 to F15? I'm a little lost why that would be. Sub Macro5() ' ' Macro5 Macro ' ' Range("A2").Select ActiveCell.FormulaR1C1 = "Market" Range("B2").Select ActiveCell.FormulaR1C1 = "Date" Range("E2:J16").Select Selection.Cut Range("C2").Select ActiveSheet.Paste Range("A2:H2").Select Selection.Copy Range("A19").Select ActiveSheet.Paste Windows("Overview Bullet-MTD.csv").Activate Application.Left = 1551.25 Application.Top = 147.25 Windows("Weekly Exec Updates - Lead Pacing (Working File) (1).xlsx").Activate Range("A20").Select Application.CutCopyMode = False ActiveCell.FormulaR1C1 = "MO" Range("A21").Select ActiveCell.FormulaR1C1 = "LA" Range("A22").Select ActiveCell.FormulaR1C1 = "AR" Range("A23").Select ActiveCell.FormulaR1C1 = "MS" Range("B20").Select ActiveCell.Formula2R1C1 = _ "=XLOOKUP(RC[-1],R[-17]C[-1]:R[-4]C[-1],R[-17]C:R[-4]C[6])" Range("B20").Select Selection.AutoFill Destination:=Range("B20:B23"), Type:=xlFillDefault Range("B20:B23").Select Range("B21").Select ActiveCell.Formula2R1C1 = _ "=XLOOKUP(RC[-1],R[-18]C[-1]:R[-5]C[-1],R[-18]C:R[-5]C[6])" Range("B22").Select ActiveCell.Formula2R1C1 = _ "=XLOOKUP(RC[-1],R[-19]C[-1]:R[-6]C[-1],R[-19]C:R[-6]C[6])" Range("B23").Select ActiveCell.Formula2R1C1 = _ "=XLOOKUP(RC[-1],R[-20]C[-1]:R[-7]C[-1],R[-20]C:R[-7]C[6])" Range("B3:B16").Select Selection.NumberFormat = "m/d/yyyy" Range("E3:E16").Select Selection.NumberFormat = "0.0%" Selection.NumberFormat = "0%" Range("G3:G16").Select Selection.NumberFormat = "0.0%" Selection.NumberFormat = "0%" Range("C20:D23").Select Selection.NumberFormat = "#,##0" Range("F20:F23").Select Selection.NumberFormat = "#,##0" Range("G20:G23").Select Selection.Style = "Percent" Sheets("YTD").Select Windows("Overview Bullet-YTD.csv").Activate Windows("Weekly Exec Updates - Lead Pacing (Working File) (1).xlsx").Activate ActiveWindow.ScrollColumn = 2 ActiveWindow.ScrollColumn = 3 ActiveWindow.ScrollColumn = 4 Range("M2:M16").Select Selection.Cut ActiveWindow.ScrollColumn = 3 ActiveWindow.ScrollColumn = 2 ActiveWindow.ScrollColumn = 1 Range("D2").Select ActiveSheet.Paste Range("D2").Select ActiveCell.FormulaR1C1 = "Actual Leads" Range("K2:K16").Select Selection.Cut Range("E2").Select ActiveSheet.Paste ActiveWindow.ScrollColumn = 2 ActiveWindow.ScrollColumn = 3 ActiveWindow.ScrollColumn = 4 Range("L2:L16").Select Selection.Cut Range("F2").Select ActiveSheet.Paste Range("H2:H16").Select Selection.Cut Range("G2").Select ActiveSheet.Paste Range("I2:V17").Select Selection.ClearContents ActiveWindow.ScrollColumn = 7 ActiveWindow.ScrollColumn = 6 ActiveWindow.ScrollColumn = 5 ActiveWindow.ScrollColumn = 4 ActiveWindow.ScrollColumn = 3 ActiveWindow.ScrollColumn = 2 ActiveWindow.ScrollColumn = 1 Range("A2:G2").Select Selection.Copy Range("A22").Select ActiveSheet.Paste Range("A23").Select Application.CutCopyMode = False ActiveCell.FormulaR1C1 = "MO" Range("A24").Select ActiveCell.FormulaR1C1 = "LA" Range("A25").Select ActiveCell.FormulaR1C1 = "AR" Range("A26").Select ActiveCell.FormulaR1C1 = "MS" Range("B23").Select ActiveCell.Formula2R1C1 = _ "=XLOOKUP(RC[-1],R[-20]C[-1]:R[-7]C[-1],R[-20]C:R[-7]C[5])" Range("B23").Select Selection.AutoFill Destination:=Range("B23:B26"), Type:=xlFillDefault Range("B23:B26").Select Range("B24").Select ActiveCell.Formula2R1C1 = _ "=XLOOKUP(RC[-1],R[-21]C[-1]:R[-8]C[-1],R[-21]C:R[-8]C[5])" Range("B25").Select ActiveCell.Formula2R1C1 = _ "=XLOOKUP(RC[-1],R[-20]C[-1]:R[-7]C[-1],R[-20]C:R[-7]C[5])" Range("B25").Select ActiveCell.Formula2R1C1 = _ "=XLOOKUP(RC[-1],R[-22]C[-1]:R[-9]C[-1],R[-22]C:R[-9]C[5])" Range("B26").Select ActiveCell.Formula2R1C1 = _ "=XLOOKUP(RC[-1],R[-23]C[-1]:R[-10]C[-1],R[-23]C:R[-10]C[5])" Range("A2:G16").Select Selection.AutoFilter ActiveSheet.Range("$A$2:$G$16").AutoFilter Field:=1, Criteria1:=Array("AL", _ "CA", "HI", "NM", "NNE", "OHWVKY", "PA", "TW", "TX"), Operator:=xlFilterValues Range("A3").Select ActiveWorkbook.Worksheets("YTD").AutoFilter.Sort.SortFields.Clear ActiveWorkbook.Worksheets("YTD").AutoFilter.Sort.SortFields.Add2 Key:=Range( _ "A2:A16"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("YTD").AutoFilter.Sort .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Sheets("MTD").Select Range("A2:H16").Select Selection.AutoFilter Range("A3").Select ActiveSheet.Range("$A$2:$H$16").AutoFilter Field:=1, Criteria1:=Array("AL", _ "CA", "HI", "NM", "NNE", "OHWVKY", "PA", "TW", "TX"), Operator:=xlFilterValues ActiveWorkbook.Worksheets("MTD").AutoFilter.Sort.SortFields.Clear ActiveWorkbook.Worksheets("MTD").AutoFilter.Sort.SortFields.Add2 Key:=Range( _ "A2:A16"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("MTD").AutoFilter.Sort .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With End Sub submitted by /u/_mavricks [link] [comments]
- Copying and pasting from templateI want to build some VBA that lets me Copy and Paste (As special values) from one template to another. The source template looks *extremely* similar to the destination template, but the destination template may add columns before the data I want to copy and paste without the source template doing the same. How would you approach this? I can see Power query or Identifier Columns being the best bet, but both source and destination already have a decent bit of chug on them, so trying to keep as light as possible. TLDR; Want to Hard Paste (can’t have a link to an external book at the end) Columns B-K from source workbook sheet1 into D-M of recipient workbook sheet1 . Source B-K may become C-L and Recipient D-M may become E-N at any moment and want the code to not worry about that. Feels like It should be super simple but can't think of the most efficient way to do it submitted by /u/Codenamerondo1 [link] [comments]
- Trying to remove invisible characters (go to special -> blanks not finding all blank cells)Hi everyone, I’m at my wits end with this one. I’ve copy and pasted data a large chunk of data from one excel sheet to another that has a blank line or two between meaningful data. I want to remove all those blank cells, but when I use go to special -> blanks it says there isn’t any. I’ve tried using CLEAN, tried using TRIM, tried using both, tried using CODE to detect what character is in the cell, it comes back as #VALUE. The way the data was pulled was with the formula IFERROR(TEXTBEFORE(A1, “ - “,1)””). It isn’t treating “” as blank for whatever reason. Maybe an extra important detail: the original file is a .xls; new book is .xlsx. Using Office 365. Any help is appreciated! submitted by /u/newblood310 [link] [comments]