Insert empty rows in between fields or move entries - excel

I have a list of items in column A (starting from A12) I want to know the best way to do this. I want there to be a 7 row gap between all the entries. These entries will have additions so some code that perhaps says if text here then add 7 rows below until no more. Or is there a way to code it so it just pushes the entries into different fields (7+ down). What would the code look like?
Basic adding row is:
Range("A13").EntireRow.Insert

So, along with #Plutian and his code to insert new rows, I edited this to filldown the new rows with data from each cell that is not blank. Hope this helps.
Sub numberf()
Application.ScreenUpdating = False
Dim lastrow As Integer
lastrow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
Do While lastrow > 12
Range("A" & lastrow).EntireRow.Resize(7).Insert
lastrow = lastrow - 1
Debug.Print lastrow
Loop
lastrow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
Dim i As Long
i = 1
For i = 12 To lastrow Step 8
If Cells(i, 1).Value <> "" Then
Range("A" & i).Resize(8).EntireRow.FillDown
End If
Next i
Application.ScreenUpdating = True
End Sub

Sub numberf()
Application.ScreenUpdating = False
Dim lastrow As Integer
lastrow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
Do While lastrow > 2
Range("A" & lastrow).EntireRow.Resize(7).Insert
lastrow = lastrow - 1
Loop
Application.ScreenUpdating = True
End Sub
As I suggested, a reverse loop that does the trick. Props to #MilesFett with the resize option, as my first idea was to loop 7 times inserting a row. This is much cleaner.

Related

Delete all rows except the ones with a specific value

How, based on column values can I keep certain rows and delete the rest? Is it possible to delete everything except the value I want to keep?
Right now I repeat the values I want to delete, but this is not the best solution. Do you think it is possible to delete everything except the value I want to keep?
I would really appreciate your help. I am a beginner.
With ActiveSheet
FirstRow = 4
LastRow = 10000
For Row = LastRow To FirstRow Step -1
If .Range("A" & Row).Value = "ITT1" Then
.Range("A" & Row).EntireRow.Delete
End If
Next Row
End With
You need to negate your If statement using <> or Not:
Just use
If .Range("A" & Row).Value <> "ITT1" Then
or
If Not .Range("A" & Row).Value = "ITT1" Then
to delete all rows except the ones with ITT1 as value in column A.
Note that instead of hard coding the last row LastRow = 10000 you can determine it using something like:
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' get last used row in column A
So if you only have 100 rows the loop does not start at 10000 but just loops over the rows that actually have data.
So It should look something like:
With ActiveSheet
Const FirstRow As Long = 4
Dim LastRow As Long
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' get last used row in column A
Dim Row As Long
For Row = LastRow To FirstRow Step - 1
If Not .Range("A" & Row).Value = "ITT1" Then
.Range("A" & Row).EntireRow.Delete
End If
Next Row
End With
If it has to delete a lot of rows this might get a bit slow. So I recommend to collect the rows to delete in a variable using Union and delete them all at once in the end, which is much faster:
With ActiveSheet
Const FirstRow As Long = 4
Dim LastRow As Long
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' get last used row in column A
Dim RowsToDelete As Range ' here we collect which rows to delete
Dim Row As Long
For Row = LastRow To FirstRow Step - 1
If Not .Range("A" & Row).Value = "ITT1" Then
If RowsToDelete Is Nothing Then
' set first row
Set RowsToDelete = .Range("A" & Row).EntireRow
Else
' add all the other rows
Set RowsToDelete = Application.Union(RowsToDelete, .Range("A" & Row).EntireRow)
End If
End If
Next Row
If Not RowsToDelete Is Nothing Then
RowsToDelete.Delete
Else
MsgBox "No rows to delete were found."
End If
End With
If you collect all rows to delete in a variable first you even don't need to run the loop backwards Step - 1 and just use a normal forward loop if you like:
For Row = FirstRow To LastRow

Changing column value based on another column using vba in excel

I am trying to create a macro button that will help me update the the value in the AE column to "N" if the value in the same row of the H column is "REPO".
I am not sure why my code doesn't work properly and just seems to select the AE column when I run it instead of changing the values to "N"
Sub Change_Repo_Risk_to_N()
Sheets("expo").Select
Dim LastRow As Long
Dim i As Long
LastRow = Range("H" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Range("H" & i).Value = "REPO" Then
Range("AE" & i).Value = "N"
End If
Next i
End Sub
Probably mistake due to one if these 3:
Lack of Trim()
Lack of UCase() (Option Compare Text is an alternative of this one)
Select() is too slow and does not refer correctly to the worksheet (try to avoid it)
Try this one:
Sub ChangeRepoRiskToN()
With Worksheets("expo")
Dim lastRow As Long
Dim i As Long
lastRow = .Range("H" & Rows.Count).End(xlUp).Row
For i = 2 To lastRow
If Trim(UCase(.Range("H" & i).Value)) = "REPO" Then
.Range("AE" & i).Value = "N"
End If
Next i
End With
End Sub

How to delete the rows based in excel sheet using column values

I have excel with 5 different sheets.
sheet3 and sheet4 i want delete rows based on the single column cell value.
in sheet 3 i want to delete rows based on H column cell values if H2="#N/A" and H503="#N/A" then delete entire rows.
in sheet 4 i want to delete rows based on b column cell values if B2="320857876",B3="32085678",B4="12133435" the delete the entire rows where B column cell values starts with 302.
and i want to delete all Data from 'C' column
My excel sheet is like this
Using excel file
Sub Create()
Dim LastRow As Long
Dim i As Long
LastRow = Range("B10000").End(xlUp).Row
For i = LastRow To 1 Step -1
If Range("B" & i) = "#N/A" Then
Range("B" & i).EntireRow.Delete
End If
Next
End Sub
You've got a few requirements there and your code is fairly light but regarding the #N/A part of it, you can't just test for that text using the value approach, which is the default property returned for a range object.
Sub Create()
Dim LastRow As Long, i As Long
LastRow = Range("B10000").End(xlUp).Row
For i = LastRow To 1 Step -1
If Range("B" & i).Text = "#N/A" Then
Range("B" & i).EntireRow.Delete
End If
Next
End Sub
... you need to use .Text to get that to work, or, If IsError(Range("B" & i)) Then is another approach.
The rest of your requirements is just logic. The rest of your code is relatively sound so you just need to work through it.
I hope that helps.
Sub delete_rows()
Dim sheet As Worksheet, cell As Range
Count = 1
For Each sheet In ThisWorkbook.Worksheets
If Count = 3 Then
lastrow = sheet.Cells(sheet.Rows.Count, "H").End(xlUp).Row
Set Rng = sheet.Range("H1:H" & lastrow)
For i = Rng.Cells.Count To 1 Step -1
If Application.WorksheetFunction.IsNA(Rng(i).Value) Then
Rng(i).EntireRow.Delete
ElseIf Rng(i).Value = "#NA" Then
Rng(i).EntireRow.Delete
End If
Next
ElseIf Count = 4 Then
lastrow = sheet.Cells(sheet.Rows.Count, "B").End(xlUp).Row
Set Rng = sheet.Range("B1:B" & lastrow)
Debug.Print (Rng(4).Text)
If Rng(2).Value = "320857876" And Rng(3).Value = "32085678" And Rng(4).Value = "12133435" Then
For i = Rng.Cells.Count To 1 Step -1
If Left(Rng(i).Value, 3) = "302" Then
Rng(i).EntireRow.Delete
End If
Next
End If
lastrow = sheet.Cells(sheet.Rows.Count, "C").End(xlUp).Row
Set Rng = sheet.Range("C1:C" & lastrow)
For Each cell In Rng
cell.Value = ""
Next cell
End If
Count = Count + 1
Next
End Sub

Excel VBA Delete Rows Containing certain text. Code not working

I have a macro that opens a workbook. I want it to then look into column C and if it finds anything with the text "Draft", delete the entire row. This is my code which does not appear to give me any errors but it does not delete the rows like I want. What am I missing?
enter code here
Dim i As Long
Dim FinalRow As Long
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
With Worksheets("Archer Search Report")
For i = 2 To FinalRow
If Range("C" & i).Value = "Draft" Then
Rows(i).Delete
End If
Next i
End With
Try this:
Sub DeleteRows()
Dim i As Long, finalRow As Long
finalRow = Cells(Rows.Count, 1).End(xlUp).Row
With Worksheets("Archer Search Report")
For i = finalRow To 2 Step -1
If Range("C" & i).Value = "Draft" Then
Range("C" & i).EntireRow.Delete
End If
Next i
End With
End Sub
Notes:
It's best to work backwards (Step -1) when deleting otherwise it messes with the row count
I prefer EntireRow.Delete

Hide all red cells in Excel

I want to hide all RED cells within a worksheet by pressing on a command button. How can I do this by using a Macro? I also want another button to unhide them. Currently I have the following which isn't working:
Sub HideRow()
Dim i As Long
Dim LASTROW As Long
Application.ScreenUpdating = False
LASTROW = Cells(Rows.Count, 2).End(xlUp).Row
For i = 1 To LASTROW
If Range("C" & i).Interior.ColorIndex = 3 Then
Range("C" & i).EntireRow.Hidden = True
End If
Next i
Application.ScreenUpdating = True
End Sub
Thanks,
Are you sure LASTROW is valid, Maybe you mess up columns?
You search last row by Cells(Rows.Count, 2), 2 means column B.
But you check color in column C Range("C" & i)
BTW you could just use Cells(i,3) instead of Range("C" & i)

Resources