Insert row when Cell Value is Greater - excel

Here is my Sheet in which i wrote this code
Sub L()
Dim rng As Range, cell As Range
Set rng = Range("P12:P1322")
For Each cell In rng
If cell.Value > 12 Then
cell.Interior.ColorIndex = 3
ActiveCell.Range(rng).Activate
Copy.selection
Range(rng).Offset(1, 0).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrAbove
'Range(rng).Offset(1).EntireRow.PasteSpecial xlPasteFormats
End If
Next cell
End Sub
I tried another code too but it was not working.
You see the red cells These cells value is greater than 12 I want to add the row with same formatting below the active row.

I think this should do what you want
Sub L()
Dim i As Long
With Range("P12:P1322")
For i = .Rows.Count To 12 Step -1
If .Cells(i, 1).Value > 12 Then
.Cells(i, 1).Interior.ColorIndex = 3
.Cells(i + 1, 1).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If
Next i
End With
End Sub
The following is significantly faster
Sub LImproved()
Dim dStart As Double
dStart = Timer
Dim i As Long
Dim lCol As Long: lCol = 16 ' column P
Dim lLastRow As Long
Dim sRows As String
With ThisWorkbook.Sheets("Sheet1")
lLastRow = .Cells(.Rows.Count, lCol).End(xlUp).Row
'lLastRow = 1322
For i = lLastRow To 12 Step -1
If .Cells(i, lCol).Value > 12 Then
sRows = sRows & i + 1 & ":" & i + 1 & ","
If Len(sRows) > 235 Then
UpdateFormats Left(sRows, Len(sRows) - 1), lCol
sRows = ""
End If
End If
Next i
If sRows <> "" Then UpdateFormats Left(sRows, Len(sRows) - 1), lCol
End With
MsgBox "Time taken: " & Format(Timer - dStart, "0.00s")
End Sub
Sub UpdateFormats(sRows As String, lCol As Long)
With ThisWorkbook.Sheets("Sheet1")
With Intersect(.Range(sRows), .Columns(lCol)).Offset(-1, 0)
.Interior.ColorIndex = 3
End With
With .Range(sRows)
.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End With
End With
End Sub

Related

If two consecutive cells are blank then second cell equals value

I was able to get this much working but not able to get it to start from Range("B12:B500").
Sub findTwoEmptyCells()
Dim lastRow As Long, i As Long
Dim firstEmptyCell As Range
lastRow = Cells(Rows.Count, 2).End(xlUp).Row
For i = 1 To lastRow
If Cells(i, 1).Value = "" And Cells(i, 1).Offset(1, 0).Value = "" Then
Set firstEmptyCell = Cells(i, 1)
Exit For
End If
Next i
End If
firstEmptyCell.Value = "x"
End Sub
Thanks to #Dominique & #FaneDuru, i was able to get the result as per the below by combining both their ideas :
Sub findTwoEmptyCells()
Dim lastRow As Long, i As Long
Dim firstEmptyCell As Range
lastRow = Cells(Rows.Count, 12).End(xlUp).Row
For i = 12 To lastRow
If Cells(i, 2).Value = "" And Cells(i + 1, 1).Value = "" Then
Set firstEmptyCell = Cells(i, 1)
Exit For
End If
Next i
If firstEmptyCell Is Nothing Then
MsgBox ("There are no two empty cells in a row")
Exit Sub
End If
firstEmptyCell.Value = "x"
End Sub

Adding value of range("B6") everytime it counts from Range("B1 : B5") cell that has Green Background and value = 0

I want code that adds a value of range("B6") every time it counts from Range("B1 : B5") cell that has Green Background and value = 0 while in a worksheet_Change event.
Here's my code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ActiveSheet
lastRow = 5
For i = 2 To lastRow
With ws.Cells(i, 2)
If .Interior.ColorIndex = 4 And .Value = 0 Then
MsgBox "Test" ' <----- supposedly range("B6").value .add
End If
End With
Next
End Sub
Please, try this code:
It works only for cell changed in the range "B1:B" & lastRow. Would you like to work for any cell changed in all the sheet? I think, it could be annoying...
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet, rng As Range, lastRow As Long
Dim i As Long, iCount As Long, iCountC As Long
Set ws = ActiveSheet
lastRow = 5
Set rng = ws.Range("B1:C" & lastRow)
If Not Intersect(Target, rng) Is Nothing Then
For i = 1 To lastRow
With ws.Cells(i, 2)
If .Interior.ColorIndex = 4 And .Value = 0 Then
iCount = iCount + 1
End If
If .Offset(0, 1).Interior.ColorIndex = 4 And _
.Offset(0, 1).Value = 0 Then
iCountC = iCountC + 1
End If
End With
Next
ws.Range("B" & lastRow + 1).Value = iCount
ws.Range("C" & lastRow + 1).Value = iCountC
End If
End Sub
It returns the number of green cells, immediately after the lastRow. So, if you modify this number (from 5 to 15), it will return in "B16"...
For your second request I used .Offset(0, 1) which means the neighbor cell to the right...

Copy value from Row and column corresponding to all X'es in sheet

I have a task where i have "Function" in column A, and tags in rows with "X" in middle showing which tag and function is connected together (Please see attachment)
I have been trying to make a script that can go to the "Function (Column A)", check if it will find Value "X" in the same row, if it finds it will go up and get the tag posting the information in new sheet.
Sheet2 will then be showing:
Function -> and thich Tag is in the same function, if there is few tags like in the example below it will show like this.
802AB Tag1
802AB Tag2
802AB Tag3
802AB Tag4
802AB Tag5
804AB Tag4
805AB Tag2
I have few hundrets of those files, which are very big so this is simplified example. Thank you for your help.
https://imgur.com/a/xo0TEZs
Sub test()
Dim rng As Range
Dim cel As Range
Dim lastRow As Long
Dim writeRow As Long
Dim rCell As Range
Dim lColor, ColorRow As Long
Dim rColored As Range
Dim i, j As Integer
Dim temprow As Long
Dim lnRow As Long, lnCol As Long
lColor = RGB(255, 153, 204)
Set rColored = Nothing
lastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
writeRow = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row + 1
Set rng = Sheets("Sheet1").Range("A6:A" & lastRow)
For Each cel In rng
If cel.Interior.Color = lColor Then
ColorRow = cel.Row + 1
For j = ColorRow + 1 To lastRow
For i = ColorRow + 1 To lastRow
lnCol = Sheet1.Cells(i, 1).EntireRow.Find(What:="X",
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlBycolumn,
SearchDirection:=xlNext, MatchCase:=False).Column
' Sheets("Sheet2").Range("A" & writeRow).Value = cel.Offset(0, 0).Value
' writeRow = writeRow + 1
Next i
Next j
'End If
If rColored Is Nothing Then
Else
Sheets("Sheet2").Range("A" & writeRow).Value = cel.Offset(-1, 0).Value
writeRow = writeRow + 1
End If
End If
Next cel
End Sub
This is basically what i have, not yet functional, it searches for the first row with the right format color, then it starts a loop going through rows, searches for X in row, and it stops, I need Copy the tag where it found row, and go to the next X in same row, after all rows is done it shall go to next row do the same.
Sub test()
Dim rng As Range
Dim cel As Range
Dim lastRow As Long
Dim writeRow As Long
Dim rCell As Range
Dim lColor, ColorRow As Long
Dim rColored As Range
Dim i, j As Integer
Dim temprow As Long
Dim lnRow As Long, cellvalueTemp As String
Dim WS As Workbook
lColor = RGB(255, 153, 204)
Set rColored = Nothing
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Sheet2"
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Sheet3"
Sheets("Sheet2").Cells(1, 1).Value = "Tag"
Sheets("Sheet2").Cells(1, 2).Value = "Terminal"
Sheets("Sheet2").Cells(1, 3).Value = "CollectiveGroupName"
Sheets("Sheet2").Cells(1, 4).Value = "LogicalGroupName"
lastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
writeRow = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row + 1
Set rng = Sheets("Sheet1").Range("A6:A" & lastRow)
For Each cel In rng
If cel.Interior.Color = lColor Then
ColorRow = cel.Row + 1
For i = ColorRow To lastRow
For j = 20 To 100 'Needs to be adjusted, possibily find the last colum and first
If Sheet1.Cells(i, j).Value = "X" Then
Sheets("Sheet2").Range("A" & writeRow).Value = Sheet1.Cells(i, 1).Value
Sheets("Sheet2").Range("B" & writeRow).Value = Sheet1.Cells(i - 7 - (i - ColorRow), j).Value
Sheets("Sheet2").Range("D" & writeRow).Value = Sheet1.Cells(i - 6 - (i - ColorRow), j).Value
writeRow = writeRow + 1
Columns("A:D").EntireColumn.AutoFit
End If
'Ikke gjør noe
Next j
Next i
If rColored Is Nothing Then
Else
'Ikke gjør noe
End If
End If
Next cel
End Sub

What could be going wrong in this particular code? I am trying to count the cells whose values are <=7

Private Sub last_column()
Dim Cell As Range
With ThisWorkbook.Sheets("Sheet1")
lcol = .Cells(1, Columns.Count).End(xlToLeft).Column
MsgBox ("Last column is " & lcol)
For i = 4 To 7
'below line is raising an error
Cell(i, lcol).Value = Application.Countif(Range(.Cells("i", 3), .Cells("i", "lcol")), "<= 7")
Next i
End With
End Sub
Edit:
I have changed the code as follows, but although there is no error now there is no output on the cells as well.
Private Sub last_column()
Dim Cell As Range
Dim i As Double
With ThisWorkbook.Sheets("Sheet1")
lcol = .Cells(1, Columns.Count).End(xlToLeft).Column
MsgBox ("Last column is " & lcol)
For i = 4 To 7
.Cells(i, lcol).Value = Application.Countif(.Range(.Cells(i, 3), .Cells(i, lcol)), "<= 7")
Next i
End With
End Sub

Copy entire row from current sheet to another sheet on color basis

My current sheet is having data in which few cells having Green color, i need to move or copy those rows in which cell having green colour (only few cells coloured with green)to another sheet. i have written code for that but the loop runs on first column for each row wise but wont checks for every cell in that row. i need to check for every row each cell if any cell in green colour then it should copy and paste the entire row in another sheet on next row
Sub Copy()
lastRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
sheet2Counter = 1
For i = 1 To lastRow
ConditionalColor = Worksheets("Sheet1").Cells(i, 1).Interior.ColorIndex
Worksheets("Sheet1").Activate
Worksheets("Sheet1").Range("A" & i & " ").Select
If ConditionalColor = 35 Then
ActiveCell.EntireRow.copy
Worksheets("Sheet2").Activate
lastrow1 = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
If Not Worksheets("Sheet2").Range("A" & lastrow1 & " ") = "" And Not i = 1 Then
lastrow1 = lastrow1 + 1
Worksheets("Sheet2").Range("A" & lastrow1 & " ").Select
With Selection
.PasteSpecial Paste:=xlPasteAll
End With
Else
Worksheets("Sheet2").Range("A1").Select
With Selection
.PasteSpecial Paste:=xlPasteAll
End With
End If
Worksheets("Sheet1").Cells(i, 1).Value
End If
Next
End Sub
You can do something like this:
Option Explicit
Sub CopyByColor()
Dim shtSrc As Worksheet, shtDest As Worksheet
Dim lastRowSrc As Long, nextRowDest As Long, i As Long
Set shtSrc = Worksheets("Sheet1")
Set shtDest = Worksheets("Sheet2")
lastRowSrc = shtSrc.Cells(Rows.Count, 1).End(xlUp).Row
nextRowDest = shtDest.Cells(Rows.Count, 1).End(xlUp).Row + 1
For i = 1 To lastRowSrc
'only check used cells in the row...
If IsColorMatch(Application.Intersect(shtSrc.Rows(i), shtSrc.UsedRange)) Then
shtSrc.Rows(i).Copy shtDest.Cells(nextRowDest, 1)
nextRowDest = nextRowDest + 1
End If
Next i
End Sub
Function IsColorMatch(rng As Range)
Const INDEX_COLOR As Long = 35
Const INDEX_COLOR_BAD As Long = 3 'or whatever...
Dim c As Range, indx
IsColorMatch = False '<< default
For Each c In rng.Cells
indx = c.Interior.ColorIndex
If indx = INDEX_COLOR Then
IsColorMatch = True
Elseif indx = INDEX_COLOR_BAD Then
IsColorMatch = False
Exit Function '<< got a "bad" color match, so exit
End If
Next c
End Function
EDIT: a different implementation of IsColorMatch using the "find formatting" approach:
Function IsColorMatch(rng As Range) As Boolean
If RangeHasColorIndex(Selection.EntireRow, 6) Then
IsColorMatch = Not RangeHasColorIndex(Selection.EntireRow, 3)
Else
IsColorMatch = False
End If
End Function
Function RangeHasColorIndex(rng As Range, indx As Long)
With Application.FindFormat
.Clear
.Interior.ColorIndex = indx
End With
RangeHasColorIndex = Not rng.Find("", , , , , , , , True) Is Nothing
End Function

Resources