Add a new row and Format the new row added - excel

I have a sheet where the number of rows is dynamic. I am trying to add a macro which adds a new row after each active row and it should add the text "No Show" under column C of each new row added and the it should add the cell value E5 under D column.
Here is the example below:
Current Sheet:
After the Macro: (Test in E5 is Holiday)
I have a macro to add new empty rows but not sure how to integrate the other pieces of it.
Sub Insert_Blank_Rows()
Selection.End(xlDown).Select
Do Until ActiveCell.Row = 1
ActiveCell.EntireRow.Insert shift:=xlDown
ActiveCell.Offset(-1, 0).Select
Loop
End Sub

Sub FFF()
Dim r&, vE5
vE5 = [E5]: r = Cells(Rows.Count, 1).End(xlUp).Row + 1
While r > 1
Rows(r).Insert
Cells(r, 1).Resize(, 4) = Array(Cells(r - 1, 1).Resize(, 2), "No Show", vE5)
r = r - 1
Wend
End Sub

loop backwards:
Option Explicit
Sub Insert_Blank_Rows()
Dim iRow As Long
Dim myText As String
myText = Range("E5").Text
With Selection
For iRow = .Rows.Count To 1 Step -1
.Rows(iRow + 1).EntireRow.Insert shift:=xlDown
With .Rows(iRow + 1)
.Range("A1:B1").Value = .Offset(-1).Range("A1:B1").Value
.Range("C1:D1").Value = Array("No Show", myText)
End With
Next
End With
End Sub

if i understood your question you can:
in this example i suppose that you have in cell E5 the text Holiday.
i tried at no change your code
EDITED THE IMAGE and CODE
(because before i used E1 cell and i don't write AB... into new column)
BEFORE EXECUTE THE MACRO
AFTER MACRO
Sub Insert_Blank_Rows()
Dim text, textCell_E5 As String
Dim myRow As Long
text = "no Show" ' this thext goes into column C
textCell_E5 = Cells(5, 5) ' Holiday
ActiveSheet.Range("A1").Select ' or cells(1,1).Activate
Selection.End(xlDown).Select
myRow = ActiveCell.Row + 1
Cells(myRow, 1).Offset(0, 2) = text
Cells(myRow, 1).Offset(0, 3) = textCell_E5
Cells(myRow, 1).Offset(0, 0) = Cells(myRow, 1).Offset(-1, 0)
Cells(myRow, 1).Offset(0, 1) = Cells(myRow, 1).Offset(-1, 1)
Do Until ActiveCell.Row = 1
ActiveCell.EntireRow.Insert shift:=xlDown
myRow = ActiveCell.Row ' get the current row
Cells(myRow, 1).Offset(0, 2) = text ' write into column C the no Show
Cells(myRow, 1).Offset(0, 3) = textCell_E5 ' add Holiday Text
Cells(myRow, 1).Offset(0, 0) = Cells(myRow, 1).Offset(-1, 0) 'write into column A (new row)
Cells(myRow, 1).Offset(0, 1) = Cells(myRow, 1).Offset(-1, 1) ' write into column B (new row)
ActiveCell.Offset(-1, 0).Select
Loop
End Sub
I Tried the code and works.
Hope this helps

Related

Copying a value down into inserted blank rows over multiple columns

I have a sheet with multiple columns and in column A there is data where I have removed the duplicates.
This is the code to insert nine blank lines below each of the unique values.
Sub RowAdder()
Dim i As Long, col As Long, lastRow As Long
col = 1 lastRow = Cells(Rows.Count, col).End(xlUp).Row
For i = lastRow To 3 Step -1
If Cells(i - 1, col) <> Cells(i, col) Then
Range(Cells(i, col).EntireRow, Cells(i + 8, col).EntireRow).Insert shift:=xlDown
End If
Next I
End Sub
I need to adapt this code to copy the values of each unique value to the blank lines below for column A to C.
On the last line I need the value to be copied down into 9 blank rows.
maybe you'ar after something like this
Sub RowAdder()
Dim i As Long, col As Long
col = 1
With Range(Cells(2, col), Cells(Rows.Count, col).End(xlUp))
For i = .Rows(.Rows.Count).Row To 3 Step -1
If Cells(i - 1, col) <> Cells(i, col) Then Range(Cells(i, col).EntireRow, Cells(i + 8, col).EntireRow).Insert shift:=xlDown
Next i
With .Resize(.Rows.Count + 9)
With .SpecialCells(xlCellTypeBlanks)
.FormulaR1C1 = "=R[-1]C"
Intersect(.EntireRow, Range("B:C")).FormulaR1C1 = "=RC[-1]"
End With
With Intersect(.EntireRow, Range("A:C"))
.Value = .Value
End With
End With
End With
End Sub

Highlight rows based pf column criteria VBA

Im trying to write a VBA script to compare two = rows and have the spreadsheet highlight the duplicate rows only if certain criteria is met, such as (Value of row, column a = Value of row-1, column) AND Value of row, column b > Value of row-1, column b) Then entirerow of the greater value in column b.font.color = vbRed.
Here is a section of the table I'm running...
Table Selection
Here is the code I am using...
Sub RemoveDuplicates()
Dim i As Long, R As Long
'Dim DeviceName As Range, SerialNumber As Range, LastContact As Range
Application.ScreenUpdating = False
R = Cells(Rows.Count, 1).End(xlUp).Row
'Set DeviceName = Columns(2)
'Set SerialNumber = Columns(3)
'Set LastContact = Columns(7)
For i = R To 2 Step -1
'If Cells(i, "F").Value > Cells(i - 1, "F").Value Then
'Code above doesn't work
If Cells(i, 3).Value = Cells(i - 1, 3).Value And Cells(i, 2).Value = Cells(i - 1, 2).Value Then
'If Cells(i, 3).Value = Cells(i - 1, 3).Value And Cells(i, 2).Value = Cells(i - 1, 2).Value And Cells(i, 5).Value > Cells(i - 1, 5).Value Then
'Code above doesn't work
Cells(i, 1).EntireRow.Font.Color = vbRed
End If
Next i
Application.ScreenUpdating = True
End Sub
I can get the duplicates to highlight, but when I try to introduce the greater than check, the system gets janky.
try a conditional formatting rule.
With worksheets("sheet1").usedrange.offset(1, 0).entirerow
.FormatConditions.Delete
With .FormatConditions.Add(Type:=xlExpression, Formula1:="=and($a2=$a1, $b2=$b1, $f2>$f1)")
.font.Color = vbRed
End With
End With

read Cellvalue through a range, and write to a different range

I am trying to make a VBA scrip that check all cells between B2 and B60 for the text "Ja" that's yes in Norwegian.
How can I make this a little bit simpler that making a "if" command for each cell?
I want it to, if the cell contains "ja"(yes) then write to colum D and the same number.
ie. B1,2,3,4,5 cotains "ja", I need it to take the previous cell value in D1.2,3,4,5 and add another digit to it +1.
If nothing is found in B(ie.false) it needs to write "NEI" in the current cell, and if "NEI" (no) is found in that cell it adds +1 to colum E
Sub Macro2()
Dim celltxt As String
Dim a As Variant
If IsEmpty(Range("B2").Value) = True Then
Cells(2, 2).Value = "NEI"
End If
celltxt = ActiveSheet.Range("B2").Text
If InStr(1, celltxt, "ja") Then
a = Cells(2, 1).Value
'write to cell
Cells(2, 4).Value = Cells(2, 4) + 1
Else
'antall Cw'er vedkommende IKKE har deltatt på
Cells(2, 5).Value = Cells(2, 5) + 1
End If
If IsEmpty(Range("B3").Value) = True Then
Cells(3, 2).Value = "NEI"
End If
celltxt = ActiveSheet.Range("B3").Text
If InStr(1, celltxt, "ja") Then
a = Cells(3, 1).Value
'write to cell
Cells(3, 4).Value = Cells(3, 4) + 1
Else
'antall Cw'er vedkommende IKKE har deltatt på
Cells(3, 5).Value = Cells(3, 5) + 1
End If
End Sub
Sub slettingALL()
Range("D2:E55").Select
Selection.ClearContents
End Sub
Sub slettingdeltakelse()
Range("B2:B60").Select
Selection.ClearContents
End Sub
The following code uses a For Each loop and an IF THEN ELSE statement to check for the value "JA" in the range B2:B60.
If it finds "JA", it looks two columns to the right from the current i location, and adds "+1" to the value above it. If it finds nothing, it writes "NEI" to the current i location, and then moves three columns to the right and adds +1 to the value above it.
Sub Macro2()
For Each i In Range(Cells(2, 2), Cells(60, 2))
If i.Value = "JA" Then
i.Offset(0, 2).Value = i.Offset(-1, 2).Value + 1
Else
i.Value = "NEI"
i.Offset(0, 3).Value = i.Offset(-1, 3).Value + 1
End If
Next i
End Sub
Please let me know if this code does not work for your purpose.

Need help, If statement not providing desired result. Values in the comparing cells are the same but the if argument works half the time

Need help, If statement not providing desired result. Values in the comparing cells are the same but the if argument works half the time. Code provided below
Sub autofilter1()
For b = 1 To 4
' Last row of unique values - Unique Tab
lr = Sheets("Unique").Cells(Rows.Count, b).End(xlUp).Row
'Tabs = c
ws_count = ActiveWorkbook.Worksheets.Count
For c = 2 To ws_count
'Last row of column A
lr1 = Sheets(c).Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lr
'Execution of auto filter program
Sheets(c).Range("A1:A" & lr1).autofilter Field:=1, Criteria1:=Sheets("Unique").Range("A" & i)
'Last row of Filtered visible cells
lr2 = Sheets(c).Cells(Rows.Count, 4).End(xlUp).Row
'Below line selects entire range of visible cells
'Sheets("Assets").Range("D2:D" & lr2).SpecialCells(xlCellTypeVisible).Select
'Selection of Cell to identify aggregate address 1) Range definition, 2) sub-class aggregate cell identifier
With Sheets(c).autofilter.Range
Range("D" & .Offset(1, 0).SpecialCells(xlCellTypeVisible)(1).Row).Select
End With
'Dynamic Sum function home
Selection.Offset(0, 1).Select
'First cell below primary (only in case of multiple sub accounts, else primary is account)
SS = Selection.Offset(1, -1).Address
'Final cell of dynamic autofilter range
SE = ("D" & lr2)
Rng = Sheets(c).Range(SS & " : " & SE).SpecialCells(xlCellTypeVisible)
ActiveCell = Application.WorksheetFunction.Sum(Rng)
If ActiveCell.Value = ActiveCell.Offset(0, -1).Value Then
ActiveCell.Offset(0, 1) = "True"
ActiveCell.Offset(0, 1).Font.Bold = True
ActiveCell.Offset(0, 1).Interior.Color = 5296274
Else
ActiveCell.Offset(0, 1) = "False"
ActiveCell.Offset(0, 1).Font.Bold = True
ActiveCell.Offset(0, 1).Interior.Color = 255
End If
Next i
Next c
Next b
End Sub

If a cell contains specific text, copy the whole row & the next two rows to next sheet in excel 2013

I am very much new to VBA and i need some help.
We have 100's of records as shown in the image
I would like to copy the row which has "Layer name" in it and the next two rows (Geometry & Feature Count) to the next sheet. I tried with the code mentioned here if cell contains specific text, copy whole row + next row Of-course,changed all the '1's to '2'. But it didn't work. Any sort of help will be appreciated
Try this:
Sub layer()
j = 1
For i = 1 To 100
If Left(Cells(i, 1), 10) = "Layer name" Then
For k = 0 To 2
Sheets(2).Cells(j + k, 1) = Cells(i + k, 1)
Next k
j = j + 3
End If
Next i
End Sub
Try this:
Sub Test()
For Each Cell In Sheets(1).Range("A:A")
If Left(Cell.Value, 11) = "Layer name:" Then
matchRow = Cell.Row
Rows(matchRow & ":" & matchRow + 2).Select
Selection.Copy
Sheets(2).Select
lastRow = ActiveSheet.UsedRange.Rows.Count
If lastRow > 1 Then lastRow = lastRow + 2
ActiveSheet.Range("A" & lastRow).Select
ActiveSheet.Paste
Sheets(1).Select
End If
Next
End Sub

Resources