Delete multiple rows across multiple sheets containing the same keyword - excel

I have an excel file that contains around 50 sheets, all sheets have the same columns.
However, i would like to delete the entire rows that contains the same keyword "MARTIN" located in the 6th/last column in all the sheets.
I have tried to select all sheets at once and then delete the rows but it only worked for one sheet.
So if there is a way to do that either through an excel option or vba i'm all ears to suggestions.
Thank you

You could try:
Option Explicit
Sub test()
Dim ws As Worksheet
Dim LastRow As Long, i As Long
'Loop all sheets
For Each ws In ThisWorkbook.Worksheets
With ws
'Find the last row of column 6
LastRow = .Cells(.Rows.Count, 6).End(xlUp).Row
'Loop all rows in column 6 starting from the last one
For i = LastRow To 1 Step -1
'If the value is "MARTIN" then delete
If .Cells(i, 6).Value = "MARTIN" Then
.Rows(i).EntireRow.Delete
End If
Next i
End With
Next ws
End Sub

A quicker way to do it is to use shortcuts to delete the row and then go to the next sheet:
Say you want to delete the 6th row:
Go to the 6th row and hit Shift+Space this will highlight the entire row
To delete the row Ctrl + - (Ctrl and the minus sign)
Then to go from sheet one to sheet two you Ctrl + Page Down btn
If you really want to save time and know VBA you can create a short script. Unfortunately, I'm on my mac so I can't test one out right now but I found a good link that gives you a step by step here
Hope this helps!

Related

Copy all values in Column D from unknown number of worksheets with unknown number of rows into a single Column A of values in a master worksheet

This is my first question on SO and I'd really appreciate the direct help:
I have an Excel file with multiple worksheets. In Column D of each worksheet, I've created insert SQL statements based on each row's dynamic data. I'd now like to copy all of the insert statements that I've created in Column D on each worksheet into one big column so I can run them all into my database.
Thanks SO much for the help :)
To implement:
Create new sheet titled Master
Give cell A1 a header name
Open VBE
Insert Module
Copy and paste code as is into module
The code will:
Loop through each sheet
Copy cells in Col D from D2 To Last Non-Blank Row
Paste values only on Master in next available Non-Blank Row in Col A
Loop 2 - 3 for all sheets
Option Explicit
Sub MyDearMacro()
Dim ws As Worksheet, Master As Worksheet
Set Master = ThisWorkbook.Sheets("Master")
Dim CopyRange As Range, PasteRange As Range
Application.ScreenUpdating = False
For Each ws In Worksheets
If ws.Name <> "Master" Then
Set CopyRange = ws.Range("D2:D" & ws.Range("D" & ws.Rows.Count).End(xlUp).Row)
Set PasteRange = Master.Range("A" & Master.Rows.Count).End(xlUp).Offset(1)
CopyRange.Copy: PasteRange.PasteSpecial xlPasteValues
End If
Next ws
Application.ScreenUpdating = True
MsgBox "Please take the tour page of Stack OverFlow and show an attempt to solve on your next post.", vbCritical
End Sub
Closing Notes:
You will need to amend this if you do not want duplicates (if they exist). You may also need to amend this if your SQL Statements do not cover every row in the copy range (you may end up with blank cells separating your Statements). AKA once loop is done, remove blanks and duplicates in Col A on Master sheet.

Data copy to 2 different sheets depending on cell value

I have 2 main sheets ( "kids" and "parents") and another 3 sheets that I use for data input. What I want the code to do is to copy the values of the 3 sheets from column A TO L and transfer them to the main sheets depending on the value chosen in column D. So if I insert data to one of the 3 sheets and I have as a value in column D "kids" it should transfer it to SHEET "KIDS" if I choose value "parents" the data should go to sheet "parents". In any case, I do want it to remove duplicates and not copy the same data each time I open the file.
Private Sub Workbook_Open()
Dim i As Long, lastRow As Long, ws As Worksheet
Sheets("Kids").Range("A3:L500").ClearContents
For Each ws In Worksheets
If ws.Name <> ("Kids") Then
ws.Activate
lastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, "D").Value = "Kids" Then
Rows(i).Copy Destination:=Sheets("Kids").Range("A" & Rows.Count).End(xlUp).Offset(1)
ElseIf ws.Cells(i, "D").Value = "Parents" Then
Rows(i).Copy Destination:=Sheets("Parents").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i
End If
Next ws
End Sub
So this is the code I have used but it doesn't remove duplicates for main sheet "parents". Something is wrong...
Your question is not conveying total situation of your problem as mentioned by #Nicolas. However to help you with your problem go to the code posted by #fbonetti in the following thread to remove duplicates.
[delete-all-duplicate-rows-excel-vba:]Delete all duplicate rows Excel vba
Further to transfer Copy Data to Another Excel WorkBook Based on Criteria Using VBA to do is the following:
1. Identify data in a workbook sheet based on a text criterium and a date criterium or condition using a looping process
2. Select the specific data which meets the two or multiple conditions
3. Copy the identified data
4. Open another workbook
5. Find the first blank row in a specific worksheet in the workbook
6. Paste the data in the identified blank or empty row – erow
7. Save the workbook
8. Close the workbook
Go through the link mentioned below.
http://www.exceltrainingvideos.com/copy-data-to-another-excel-workbook-based-on-criteria-using-vba/
HTH

listing text-based data from column C in multiple sheets into a single column in a master sheet in excel

I have a workbook filled with text-based data on several different sheets. All the sheets use the same headings, but have different text in the columns. I would like to be able to list the information contained in the column C from all of the sheets in a single column in a new sheet.
Is there a way to get all of that data into a single column without having to copy and past from nearly 100 different sheets?
Using VBA, here is a solution that works. You just need to insert a module into your workbook and run this. F11 > Insert > Module. Copy and paste this code, and hit play.
This will create a new worksheet with whatever name you define under newSheet. Then take the contents of EVERY worksheet Row C (after header), no matter how many you have or what their name is, and add them to the new one. I could see a problem if you exceed 1,000,000 rows.. Other than that, if there are any sheets you DON'T want to perform this on, we would add them as exceptions specifically by name in the If statement.
TESTED:
Private Sub CopyAllSheetsCol()
Dim WS As Worksheet
Dim newSheet As String
Dim lastRow As Long 'Last Row on source Sheet
Dim tRow As Long 'target row
newSheet = "Compiled" 'name can be changed here
Sheets.Add.Name = newSheet
tRow = 2 'Set the target Row to 2, Set the Header Row manually
For Each WS In ActiveWorkbook.Worksheets
If WS.Name <> newSheet Then 'Making sure we are only working with pre-existing sheets
lastRow = Sheets(WS.Name).Range("C2").End(xlDown).Row 'get last row of Column C on each Worksheet
For r = 2 To lastRow 'Loop through all rows skipping header
Sheets(newSheet).Cells(tRow, "C") = Sheets(WS.Name).Cells(r, "C") 'Copy to newSheet
tRow = tRow + 1 'Increment target row by 1
Next r
End If
Next
End Sub
edit: touched up explanation

Excel 2007 Copy range values and paste to another range (Same Sheet) first empty row

Total Newbie here. Need help making this simple code work. Every time I run the macro I need it to find the next open row in the target row and paste. Obviously the first time I run, it work but the next time it pastes it on top of the previous paste. The number of rows I need it to paste will vary, so it needs to check the space then paste what it's copying each time I run the code.
Sub Save()
Range("A19:I100").Copy Destination:=Range("N5:V1000")
End Sub
Thanks in advance
Try to use next code:
Sub Save()
Dim lastrow As Long
lastrow = Cells(Rows.Count, "N").End(xlUp).Row
Range("A19:I100").Copy Destination:=Range("N" & lastrow + 1)
End Sub

Re: Take a value (that is summed) in multiple sheets and insert into a master sheet

Re: Creating a master sheet from multiple sheets.
Multiple sheet description: table with many rows and columns. Columns headings are identical but rows vary. Each sheet is a date.
Task: to take a single value from a specific column (always happens to be column M). the value I want is the total of that column. Take this summed value and insert into a master sheet.
My attempt so far is:
Sub append_master_sheet()
Dim wAppend As Worksheet, wSheet As Worksheet
Dim LastRow As Long
Set wAppend = Worksheets("Master")
For Each wSheet In Worksheets
If wSheet.Name <> wAppend.Name Then
LastRow = WorksheetFunction.Max(3, wAppend.Cells(65536, 2).End(xlUp).Row)
wSheet.UsedRange.Resize(, 13).Copy Destination:=wAppend.Cells(LastRow, 2)
End If
Next wSheet
End Sub
1). it takes all 13 columns rather than only the 13th column. (I see that is because I have set it at 13 as I do not know how to cycle through the preceding columns and skip them to only return the 13th column data (and within this column return the total of the column, not the discrete line items
2) Besides returning all the data which is a problem, it actually consistently skips the final value in the column M.
Can you advise how to amend above code to
1) only return the summed value from column M in the multiple sheets (calendar dates) and insert into master.
thanks,
N
Is this what you are trying (UNTESTED)
Like I mentioned in the comment above, see THIS link on how to find a last row in a column.
I have commented the code so that you will not have a problem understanding it. But if you do, simply post back :)
Note: I am assuming that the last cell in Col M has the SUM
Option Explicit
Sub append_master_sheet()
Dim wAppend As Worksheet, wSheet As Worksheet
Dim wApLRow As Long, wShLRow As Long
Set wAppend = ThisWorkbook.Worksheets("Master")
'~~> Get the last row where the ouput should be placed
wApLRow = wAppend.Range("B" & wAppend.Rows.Count).End(xlUp).Row + 1
For Each wSheet In Worksheets
If wSheet.Name <> wAppend.Name Then
With wSheet
'~~> Fuind the last row in Col M which has the sum
wShLRow = .Range("M" & .Rows.Count).End(xlUp).Row
'~~> Copy over the values to Master Sheet
wAppend.Range("B" & wApLRow).Value = .Range("M" & wShLRow).Value
'~~> Increment the row for next output
wApLRow = wApLRow + 1
End With
End If
Next wSheet
End Sub

Resources