Excel 2010 delete every 57th (and 4 more) rows - excel

I need a macro code to delete every 58th row and 4 more rows after the 57th row.
i.e I need to delete row numbers 58,59,60,61 and 62nd (5 total).
I need to repeat this pattern on the entire sheet. So in effect after the macro deletes the 58,59,60,61 and 62nd rows, it needs to delete 115,116,117,118 and 119th row (difference of 57 between them 115-57 = 58).
I tried looking up on the internet, but could not find an article matching my needs. Any help will be appreciated.
Thanks.

For a non-code method you could choose a blank column beside your data and try these steps:
Select the range e.g. A57:A60 and enter X with ctrl+enter to fill the cells
Select the repeating range e.g. A1:A60, and fill down to the end of the data (or use copy/paste)
Press Ctrl+Shift+\ to select all the X's (goto special... column differences or use autofilter)
Choose Delete > Sheet Rows from the Cells section of the Home Tab
You're done. This also preserves the undo stack so you can backtrack if required.

Next time you post a question, you would get better attention and feedback if at least you tried something, anything really. A good first step is to run the macro recorder in Excel, delete a few rows and check the code that has been generated. From there, the final solution is not very far.
Sub deleteRows()
Dim i As Long
Dim maxRow As Long
maxRow = ActiveSheet.UsedRange.Rows.Count
For i = 58 To maxRow Step 52 'Step 52, not 57, because we delete 5 rows each time
Range(Rows(i), Rows(i + 4)).Delete Shift:=xlUp
Next i
End Sub

Related

Add columns next to the cell I clicked in before running the macro

I hope everyone is doing well?
I am currently working on an Excel VBA task (my first time working with VBA, so I am sorry if I do ask silly questions). One thing I am researching for almost 2 days now and I can't find a solution anywhere is how to add columns in an Excel sheet via VBA.
To be more precisely: I don't want to add columns in one specific position (e.g. "A:B") but rather to click in a cell, run the macro and it will add 77 columns right next to this cell.
E.g. say I click into cell B2 and run the macro, I would like to have 77 empty columns from column C onwards. I was looking into "ActiveCell" as well but didn't get anywhere.
The macro I found that was the most helpful so far is this:
Sub C_SpaltenEinfügen()
Dim Start As Integer
Start = 2
Range(Cells(1, Start), Cells(1, Start + 77)).EntireColumn.Insert
End Sub
But I am not able to change it from Start = 2 to ActiveCell or anything close to that, so it will only start from column B. As the data set is quite large (currently from Column A to ARW), calculating all starting points wouldn't be very efficent.
I really do appreciate your time and help!
Thanks so much in advance,
Lea
Note that if you want 77 rows added, you would add current through 76 rows, because the first row is the initial insert. This would do that, via double clicking on Row 1:
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Row = 1 Then
Range(Columns(Target.Column + 1), Columns(Target.Column + 1 + 76)).Insert
Cancel = True
End If
End Sub
You stated that you wanted this to be to the right if where you select, so the .Column+1 offsets the column so you can work with that on the right, e.g., you double click on B1 and you insert 77 columns in front of column C. I left the +1 on the remainder of the range for clarity.
The use of Cancel=True after the insert is so you don't enter the cell you double clicked to begin editing it.
Note where I selected the BeforeDoubleClick... After selecting the sheet module for Sheet1 (Sheet1), I used the drop downs, select Worksheet in the left, then the right I selected BeforeDoubleClick:

VBA: Deleting rows that have a blank in a certain column and making a button in Excel

So I am trying to write VBA code to delete an entire row in Excel if a cell in that row in a certain column is blank. I am completely new to VBA, so I literally just had to learn as I coded. I think I have the methodology down, but it always leaves one last row with a blank in that specific column in the worksheet. In some cases, it leaves 5 rows with a blank in that specific column. Any help fixing would be appreciated, thank you!
I also want to have it where I can click on a button in the workbook, more than likely on a separate sheet, and it will do the deleting rows methodology for a specific sheet. If that is possible, am I able to move all of that methodology from one from workbook to another?
I've already tried some ways of implementing both of these using VBA, but this is my first time ever using VBA, so a lot is still new to me.
Private Sub Button_Click()
Dim LR As Long
Application.ScreenUpdating = False
For LR = Sheets("Test2").Range("AB" & Rows.Count).End(xlUp).Row To 2 Step 1
If Range("AB" & LR).Value = "" Then
Rows(LR).EntireRow.Delete
End If
Next LR
Application.ScreenUpdating = True
End Sub
I expect all the rows with blanks in that column to be deleted when I press the button in the worksheet.
If the blank cells in that column are truly blank and not zero length strings returned by formulas (e.g. "") then SpecialCells can remove them all at once.
Private Sub Button_Click()
Intersect(Me.Columns("AB"), Me.Cells.SpecialCells(xlCellTypeBlanks)).EntireRow.Delete
End Sub
For your own code, you should step backwards in your loop (Step -1). Further, you should not look at column AB for the last cell. If there were blanks in the last row or rows, they would be ignored.

VBA to copy all rows from one sheet and paste it to new sheet n times

I have sheet which varies in terms of number of rows. But has 72 columns[A to BT].
Can some one please provide me VBA code to copy the X number of rows to a new sheet in the same workbook n number of times?
Below is the code I have but,Here I am able to copy the 1200 rows but not able to paste it.
Sub test()
nrows = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
Sheet1.Rows("1200").Copy
Sheet2.Rows("A1").Select
ActiveSheet.Paste
End Sub
I have even tried
Sheet2.Rows("1").Select
In both the cases I get error "424" Object required. Need help to resolve this issue,later I would like to put a loop to copy it to n sheet to n number of times, each time I would paste it to X*n+1 row, where X is number of rows and n is number of times the rows to be copied. Please assist me to copy for one iteration and if you would give me any pointers to loop I would appreciate the same.
Rather than selecting an entire row or rows for pasting, indicate only the top, left cell of the target range. (Note that if you try to do what you want to do as a user Excel will tell you the target Range has to match in size to the source Range. So as a user you'd click only the one cell before pasting. Same thing in VBA.)
Also, there's no need to actually select the target range in order to paste to it.
Sub test()
nrows = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
Sheet1.Rows("1200").Copy
Sheet2.Range("A1").PasteSpecial
End Sub

Excel VB Macro - Find and Delete row with specific data plus the 4 rows above it

I cannot for the life of me figure out how to do this!!
I am trying to create a macro, which will be placed on the Quick Access Toolbar to find and delete a row where Column P = "Service to Claim Count 1" plus the 4 rows above it.
The file is several thousand entries long so I need it to go through the entire document and delete all 5 rows (the main row with the search data, plus the 4 above).
I hope you can help! :)
Sub delRows()
Dim txtCel As Range
Dim timesFound As Long, i As Long
timesFound = WorksheetFunction.CountIf(Range("P:P"), "Service to Claim Count 1")
For i = 1 To timesFound
Set txtCel = Columns(16).Find(what:="Service to Claim Count 1")
txtCel.Offset(-4).Select
Range(txtCel, txtCel.Offset(-4)).EntireRow.Delete
Next i
End Sub
Pretty straight forward. It finds the cell with the text, then deletes that row, and the four above it.

Insert the same Non-Blank Rows every nth row

I need to be able to insert the same 5 rows of data between every row on an Excel sheet. The 5 rows are not blank but contain specific data that needs to be repeated. The source of the 5 rows could be on a second sheet or rows 2 through 6 on sheet 1, which ever works best. Copy and pasting manually unfortunately is not an option as there are hundreds of lines. Is anyone able to provide some guidance as to how to accomplish this task?
You can use a formula in your sheet 2 like below:
=INDIRECT("Sheet1!"&ADDRESS(FLOOR(ROW()-1;5)/5+1;1))
By using this formula you will have each rows 5 times dynamically, that as soon as any change on sheet1; data in sheet 2 will be updated.
If you want to make them static Copy your data and Paste them with Values Only option.
You need to use a macro to do this:
Sub PasteRangeEveryNthRow()
Dim RW As Long, i As Long
RW = Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Range("A1:A5").Copy
For i = 1 To RW Step 1
Range("A" & i).PasteSpecial Paste:=xlPasteFormulas
Next i
End Sub
This code presupposes that you are copying the range A1:A5 from SHEET2 and pasting it every one line until the last data in the column A.
basically, you need to press ALT+F11 to open the VBA window in excel , then go to INSERT>MODULE and paste this code there. Then run this macro from the developer tab in excel.
if you don't see the developer tab in your excel, go to the options and enable it.

Resources