How to delete Rows in Excel VBA - excel

Sub A()
Dim I, Q, C_Count As Integer
C_Count = Worksheets("0618").Cells.SpecialCells(xlLastCell).Column
For I = 7 To C_Count
Q = Worksheets("0618").Cells(9, I).Value
If 0 < Q And Q < 100 Then
Worksheets("sheet1").Cells(I - 1, 3).Value = Worksheets("0618").Cells(2, I).Value
Worksheets("sheet1").Cells(I - 1, 4).Value = Worksheets("0618").Cells(9, I).Value
Worksheets("sheet1").Cells(I - 1, 5).Value = Worksheets("0618").Cells(4, I).Value
End If
Next
End Sub
The result of the code
I want to delete the empty rows with vba code but don't know how to.
Could someone tell me how to do it?

This is a sample code:
Sub Delete()
Dim LastRow As Long
Dim i As Long
With ThisWorkbook.Worksheets("Sheet1")
'Find the last row of Column A Sheet1
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Loop starting from last row to 1 row
For i = LastRow To 1 Step -1
'Check if the value in column A row i is empty
If .Range("A" & i).Value = "" Then
.Rows(i).EntireRow.Delete
End If
Next i
End With
End Sub

Related

Deleting Similar Rows using Excel VBA

I am trying to write a VBA code for an Excel macro so that I can manually trigger the macro to do the following:
In the event that any two rows have:
Same value in column A
Same value in Column B
"apple" in Column C
Same value in Column D
Then I would like all of those rows to be deleted except the row with the highest value in column E.
As an example, if:
cell A1 = cell A2
cell B1 = cell B2
cell C1 and Cell C2 = "apple"
cell D1 = cell D2
Cell E1 = 5 and Cell E2 = 10
Then Row 1 gets deleted and Row 2 remains.
The overall goal is to delete similar rows.
Per a user's suggestions, this process can be aided/simplified by sorting range by c="apple",a,b,d so that rows can be analyzed consecutively.
Example of Code Outcome
I put together the following code, but I am unfamiliar with the delete row aspect and how to incorporate the highest value, but this was my best shot. The If and elseif statements are questionable.
Sub Macro()
Dim a As Range
Dim b As Range
Dim c As Range
Dim d As Range
Dim e As Range
For Each a In Range("A1:A9999")
For Each b In Range("B1:B9999")
For Each c In Range("C1:C9999")
For Each d In Range("D1:D9999")
For Each e In Range("E1:E9999")
If a.Offset(-1, 0) = a And b.Offset(-1, 0) And c.Offset(-1, 0) = c And d.Offset(-1, 0) = d And e.Offset(-1, 0) < e Then Range(a).EntireRow.Delete
ElseIf a.Offset(-1, 0) = a And b.Offset(-1, 0) And c.Offset(-1, 0) = c And d.Offset(-1, 0) = d And e.Offset(-1, 0) > e Then Range(a.Offset(-1, 0)).EntireRow.Delete
Exit For
Next a
Next b
Next c
Next d
Next e
End Sub
I hope it works.
Option Explicit
Sub RunMacro()
Dim i As Long, LastRow As Long, j As Long
Dim cellA, cellB, cellC, cellD, cellE
Dim Rng As Range
LastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To LastRow
cellA = Range("A" & i).Value
cellB = Range("B" & i).Value
cellC = Range("C" & i).Value
cellD = Range("D" & i).Value
cellE = Range("E" & i).Value
For j = LastRow To 2 Step -1
If Range("A" & j).Value = cellA And Range("B" & j).Value = cellB Then
If Range("C" & j).Value = cellC And Range("D" & j).Value = cellD Then
If cellE > Range("E" & j).Value Then
Range("E" & j).EntireRow.Delete
LastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
End If
End If
End If
Next j
Next i
LastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
With ActiveSheet
Set Rng = Range("A1", Range("E1").End(xlDown))
Rng.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5), Header:=xlYes
End With
End Sub

How to delete a row in a sheet based on values in the 3 columns

I have to delete a row if the of the column values of Column C, Column D , Column E of the same row is zero.
for example.
ColumnA Column B ColumnC ColumnD ColumnE
row1- abc xyz 0 abs abx
row2- wqe tuy 0 0 0
row3 uhiu khj kjh khk 0
here I have to delete the row 2 only because values of all column c , D , E are zero
Please help
A reverse loop should do the job. Try the below:
Option Explicit
Public Sub DeleteRows()
Dim i As Long, count As Long, lastRow As Long
' Replace Sheet1 with your sheetname
With ThisWorkbook.Worksheets("Sheet2")
' Change C with your most consistent column letter
' (a column that has data always to make sure there's no possibility to miss the last row due to empty cells)
lastRow = .Cells(.Rows.count, "C").End(xlUp).Row
' We do a reverse loop to not screw up the index
For i = lastRow To 2 Step -1
If .Range("C" & i).Value = "0" And .Range("D" & i).Value = "0" And .Range("E" & i).Value = "0" Then
.Range("C" & i).EntireRow.Delete
count = count + 1
End If
Next i
End With
' Display some message
If count > 0 Then
MsgBox "Done!" & vbCrLf & "Deleted " & count & " row(s).", vbInformation + vbOKOnly, "Success"
Else
MsgBox "No matches found for deletion", vbInformation + vbOKOnly, "Success"
End If
End Sub
Try,
Sub test()
Dim vDB, vR()
Dim Ws As Worksheet, toWs As Worksheet
Dim i As Long, n As Long
Dim j As Integer
Set Ws = ActiveSheet
vDB = Ws.UsedRange
For i = 1 To UBound(vDB, 1)
If vDB(i, 3) = 0 And vDB(i, 4) = 0 And vDB(i, 5) = 0 Then
Else
n = n + 1
ReDim Preserve vR(1 To 5, 1 To n)
For j = 1 To 5
vR(j, n) = vDB(i, j)
Next j
End If
Next i
Set toWs = Sheets.Add '<~~ set your sheet
With toWs
.Cells.Clear
.Range("a1").Resize(n, 5) = WorksheetFunction.Transpose(vR)
End With
End Sub
Try this code, fast and easy code.
Sub deleterow()
Dim i As Integer
i = 2
LastR = Cells(Rows.Count, 1).End(xlUp).row
For i = LastR To 2 Step -1
If Cells(i, 3).value = "0" And Cells(i, 4).value = "0" And Cells(i, 5).value = "0" Then
Cells(i, 1).EntireRow.delete
End If
Next i
End Sub

Copy and pasting all values from one sheet to another

I have a macro that creates a sheet full of data. I recently added new sheets so that unique values can go into each one. For example if a row contains "Pole Change Out" then that entire row is copy and pasted into the "Pole Change Out" sheet. there are 4 different sheets. My problem is, since some values are determined by a formula in vba, some values are not moving into the new sheet.
Sub copy_paste_based_on_cell_interior_rgb()
Dim LastRow As Long
Dim i As Long, j As Long
'Find the last used row in a Column: column A in this example
With Worksheets("Make-Ready")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
'first row number where you need to paste values in Sheet1'
With Worksheets("Pole Change Out")
j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With
For i = 1 To LastRow
With Worksheets("Make-Ready")
If .Cells(i, 27).Value = "Pole Change-Out" Then
.Rows(i).Copy Destination:=Worksheets("Pole Change Out").Range("A" & j)
j = j + 1
ElseIf .Cells(i, 27).Value = "New Midspan Pole" Then
.Rows(i).Copy Destination:=Worksheets("Midspan Poles").Range("A" & j)
j = j + 1
ElseIf .Cells(i, 104).Value = "Yes" Then
.Rows(i).Copy Destination:=Worksheets("Anchor Replacement").Range("A" & j)
j = j + 1
End If
End With
Next i
End Sub
As #scottCraner and the others pointed out. You are trying to use the first empty cell variable from one sheet on the other two sheets. The update to your code will automatically update the first blank cell for each sheet.
Sub copy_paste_based_on_cell_interior_rgb()
Dim LastRow As Long
Dim i As Long ', j As Long
'Find the last used row in a Column: column A in this example
With Worksheets("Make-Ready")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
'first row number where you need to paste values in Sheet1'
'With Worksheets("Pole Change Out")
' j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
'End With
For i = 1 To LastRow
With Worksheets("Make-Ready")
If .Cells(i, 27).Value = "Pole Change-Out" Then
.Rows(i).Copy Destination:=Worksheets("Pole Change Out").Cells(Rows.Count, 1).End(xlUp).Offset(1)
'j = j + 1
ElseIf .Cells(i, 27).Value = "New Midspan Pole" Then
.Rows(i).Copy Destination:=Worksheets("Midspan Poles").Cells(Rows.Count, 1).End(xlUp).Offset(1)
'j = j + 1
ElseIf .Cells(i, 104).Value = "Yes" Then
.Rows(i).Copy Destination:=Worksheets("Anchor Replacement").Cells(Rows.Count, 1).End(xlUp).Offset(1)
'j = j + 1
End If
End With
Next i
End Sub

Microsoft Excel VBA Scripting: Recursive column matching

Sub NewMacro()
Dim endRow As Long
endRow = Sheet1.Range("A999999").End(xlUp).Row
For i = 1 To endRow
If Sheet1.Range("A" & i).Value = Sheet1.Range("F" & i).Value Then
Sheet1.Range("K" & i).Value = "Yes" Else
Sheet1.Range("K" & i).Value = "No"
End If
Next i
End Sub
This will compare column A with column F and displays the result in column K.
What I need is if this value is true, then like the above it should compare column B with column G, column C with column H and so on......and should display the results in next column. Please help.
I think you need a loop on the columns:
Sub NewMacro()
Dim endRow As Long
Dim i As Long
Dim c As Long
With Sheet1
endRow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 1 To endRow
For c = 1 To 5
If .Cells(i, c).Value = .Cells(i, c + 5).Value Then
.Cells(i, c + 10).Value = "Yes"
Else
.Cells(i, c + 10).Value = "No"
End If
Next c
Next i
End With
End Sub
This compares column A with F, column B with G, column C with H, column D with I and column E with J. Results are placed in columns K, L, M, N and O respectively.
This is equivalent to using the formula =IF(A1=F1,"Yes","No") in cell K1 and copying it across and down.
And a version which will update columns with "Yes", but stop as soon as it reaches a "No":
Sub NewMacro()
Dim endRow As Long
Dim i As Long
Dim c As Long
With Sheet1
endRow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 1 To endRow
For c = 1 To 5
If .Cells(i, c).Value = .Cells(i, c + 5).Value Then
.Cells(i, c + 10).Value = "Yes"
Else
.Cells(i, c + 10).Value = "No"
Exit For
End If
Next c
Next i
End With
End Sub

Move every 15th Column to a New Row

I have an excel sheet but all the data came in on Row 1. I need to move every 16th Column to the next row. So my data is supposed to be in Columns 1 thru Column 15. I'm not very Excel savvy so please bear with me.
Sub dividde_16()
No_of_columns = Cells(1, Columns.Count).End(xlToLeft).Column
No_of_rows = Int(No_of_columns / 15) + 1
For i = 1 To No_of_rows
For j = 1 To 15
Cells(i + 1, j) = Cells(i * 15 + j)
Next
Next
Range(Cells(1, 16), Cells(1, No_of_columns)) = ""
End Sub
You might be able to try something like this:
Sub Move_to_Columns()
Dim lR As Long, R As Range
lR = Range("A" & Rows.Count).End(xlUp).Row
Set R = Range("A1", "A" & lR)
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each C In R
If C.Row Mod 16 = 0 Then
C.Offset(-1, 1).Value = C.Value
C.ClearContents
End If
Next C
Application.Calculation = xlCalculationAutomatic
End Sub
But with columns instead of rows.

Resources