How can I delete the whole row of an Excel sheet, if in the column G has a number that starts with 210.
I don't want to delete the row if the cell has 210 somewhere inside, but only when start with it.
Use this code:
Sub RemoveRows()
Dim i As Long
i = 1
Do While i <= ThisWorkbook.ActiveSheet.Range("G1").CurrentRegion.Rows.Count
If Left(ThisWorkbook.ActiveSheet.Range("G" & i).Formula, 3) = "210" Then
ThisWorkbook.ActiveSheet.Cells(i, 1).EntireRow.Delete
Else
i = i + 1
End If
Loop
End Sub
Sample file: https://www.dropbox.com/s/yp2cwphhhdn3l98/RemoweRows210.xlsm
To see it and run, press ALT-F11, open Module1 and press F5. Good luck!
In case you want to do it without code but purely in the UI, this is how you could do this pretty efficiently:
Insert a temporary column (e.g. right of column G) (Ctrl-Space in any cell in column H, Ctrl-+)
Fill the column with the formula =LEFT(TEXT(G1),3)="210" - this will return TRUE for all rows you look for
Apply an AutoFilter to either that new column or the full range (Ctrl-Shift-L)
Filter that column for TRUE - this way, only the rows you wish to delete remain
Select all rows and delete (Ctrl-A in any cell in the table, Shift-Space, Ctrl--)
Delete the temporary column (Ctrl-Space in any cell in column H, Ctrl--)
Done!
Related
I am trying to delete columns in the data I have based on the header values, using VBA. I am definitely a novice at this, so would appreciate any help. At the moment I have managed to find some code that can do this, except every time I run the macro it deletes some of the columns, but appears to skip some of the columns. I think because when a column is deleted the column then moves to a new location, ie F5 gets deleted so G5 moves to F5 and then manages to escape the query. This is the code
Sub DeleteSpecifcColumn()
Set MR = Range("A1:D1")
For Each cell In MR
If cell.Value = "old" Then cell.EntireColumn.Delete
Next
End Sub
This is a classical problem ,If you want to delete rows or columns always begin deleting from the end .
Sub DeleteSpecifcColumn()
For i = 4 To 1 Step -1
If Cells(1, i) = "old" Then
Cells(1, i).EntireColumn.Delete
End If
Next i
End Sub
It might be the most silly question in planet. how can I merge two cell values vertically in a repeated manner. as like this:
Column A and B has 400+ cells therefore it is impossible to do what I want to achieve manually.
Note: I want to merge B into A.
You can create a simple loop in VBA that runs through each cell in the data range then adds it to the output column
Sub Merge()
Dim data As Range
Dim cell As Range
Dim output As Range
Dim i As Integer
Set data = Range("A2:B4")
Set output = Range("D2")
i = 0
For Each cell In data
output.Offset(i, 0) = cell
i = i + 1
Next
End Sub
You can use the INDEX function to refer to each cell. If data is in A2:B4, this formula works in any column but must start in row 2 and can then be filled down:
=INDEX($A$2:$B$4,ROW()/2,MOD(ROW(),2)+1)
The formula uses the current row as a counter. On every even row it gets a value from the first column of data and on every odd row it gets a value from the second column of data. After every 2 rows it gets values from the next row of data.
I am writing a script in VBA that would remove duplicate rows in an Excel spreadsheet. However, I want it to delete duplicate rows considering only information in two columns.
In other words, I have a table with the range B:F. I want the script to remove duplicate rows considering, for each row, only the values on columns D and E. In the end, only rows which simultaneously have the exact same values on columns D and E - regardless of other columns - will be removed. How could I go about doing this? Thank you
Here is an example that does this.
Make sure you run it with the sheet you want to use up:
Sub DeleteDupes()
Dim x
For x = Cells(Rows.CountLarge, "D").End(xlUp).Row To 1 Step -1
If Cells(x, "D") = Cells(x, "E") Then
'This line deletes the row:
Cells(x, "D").EntireRow.Delete xlShiftUp
'This line highlights the row to show what would be deleted;
'Cells(x, "D").EntireRow.Interior.Color = RGB(230, 180, 180)
End If
Next x
End Sub
Results of highlighting:
Results of Delete:
I have a file coming every month which has few columns with more than 50K rows. I usually need to replace values in column B with different values such as if any field in column B contains ASD , replace it with XYZ. And there is a list of value that needs to be replaced. Also as mentioned above old value can be more than once in column B and needs to be replaced .
Any help would be great.
Thanks
Record a macro [View > Macros > Record Macro]
Do the find+replace operation
Stop recording.
Now view Macros > Edit.
You will see the newly recorded Macro which does the find+replace, and you can edit this to your exact requirements.
Try that:
Sub test()
'this will get you the last row number in column "B"
lastRow = Sheets("SheetName").Cells(Sheets("SheetName").Rows.Count,"B").End(xlUp).Row
'loop through all cells in coulmn "B" and replace text as needed
For i = 1 To lastRow
'you can add multiple lines for each values that needs to be replaced
Sheets("SheetName").Cells(i, 2).Value = Replace(Sheets("SheetName").Cells(i, 2).Value, "ABC", "XYX")
Next i
End Sub
Where 'SheetName' is your sheet's name. Hope that is what you are looking for!
I have been working on a code to copy the data from one specific range(always the same) and paste in another spreadsheet always in the row below. So basically, it starts pasting on row 11, but if I run again it will paste on the row 12 and there it goes.. The code has been working fine, but there is only one problem. It identifies the next empty row(to paste) based on the value of the column AP, but i want it to identify based on the values of all the columns between AP:BA. Thus, if there is any value on those cells, it should copy on the row below, not only if there is a value on AP. Does someone know how to change my code in order to solve this problem? Thank You very much
Sub Copy_Shanghai()
Dim count As Integer
count = 11
Do While Worksheets("Time Evolution").Range("AP" & count).Value <> ""
'<>"" means "is not empty", as long as this happens we go down looking for empty cell
count = count + 1
Loop
'Now count is row with first empty cell outside of top 10 rows in column C
Worksheets("Fill").Range("E5:P5").Copy
Worksheets("Time Evolution").Range("AP" & count).PasteSpecial xlPasteValues
End Sub