Excel VBA insert row plus copy last row formula - excel

Formula below just inserts the row and changing color for certain offset. I need to copy formula from previuos cells H, M, N. Any ideas?
Sub button()
LastRow = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row
Range("D" & LastRow + 1).EntireRow.Insert
With Range("D" & Rows.Count).End(xlUp).Offset(1)
.Value = .Offset(-1).Value + 1
.Offset(, -1).Interior.ColorIndex = 0
.Offset(, -2).Interior.ColorIndex = 0
.Offset(, -3).Interior.ColorIndex = 0
End With
End Sub

So now it's working
Sub Prideti_produkta()
LastRow = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row
Range("D" & LastRow + 1).EntireRow.Insert
Range("H" & LastRow + 1).FillDown
Range("K" & LastRow + 1).FillDown
Range("M" & LastRow + 1).FillDown
Range("N" & LastRow + 1).FillDown
With Range("D" & Rows.Count).End(xlUp).Offset(1)
.Value = .Offset(-1).Value + 1
.Offset(, -1).Interior.ColorIndex = 0
.Offset(, -2).Interior.ColorIndex = 0
.Offset(, -3).Interior.ColorIndex = 0
End With
End Sub

Related

How to read all the data cells and only highlight the cells which have a more than 10 characters

I need to read all the data cell which have the entries but I only need to highlight the cells which have a character more than 10 in that data cell.
For example:
In the A column I need to read all the data but my condition is that I need to highlight the cell which contains more than 10 char.
Likewise In the B column I need to do the same thing but here I need to highlight the cell which contains more than 12 char.
Likewise I want to implement one solution for all the columns which contains the data.
Please help me to resolve it.
The code I tried:
Sub Dendrinos2()
Dim i As Long
Dim lr As Long
lr = Cells(Rows.Count, 5).End(xlUp).Row
For i = lr To 2 Step -1
If Range("C" & i).Value > 6 Then Range("C" & i).Interior.ColorIndex = 3
If Range("G" & i).Value > 3 Then Range("G" & i).Interior.ColorIndex = 3
If Range("I" & i).Value > 3 Then Range("I" & i).Interior.ColorIndex = 3
If Range("C" & i).Value < -3 Then Range("C" & i).Interior.ColorIndex = 3
If Range("G" & i).Value < -3 Then Range("G" & i).Interior.ColorIndex = 3
If Range("I" & i).Value < -3 Then Range("I" & i).Interior.ColorIndex = 3
If Range("E" & i).Value = "--" Then Range("E" & i).Interior.ColorIndex = Range("A" & i).Interior.ColorIndex
If Range("G" & i).Value = "--" Then Range("G" & i).Interior.ColorIndex = Range("A" & i).Interior.ColorIndex
If Range("I" & i).Value = "--" Then Range("I" & i).Interior.ColorIndex = Range("A" & i).Interior.ColorIndex
Next i
End Sub
Use conditional formatting with a simple formula that covers columns A and B.
Sub highlightLength()
With Worksheets("sheet3")
With .Range("A:B")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=len(a1)>(column(a1)+4)*2"
With .FormatConditions(.FormatConditions.Count)
.Interior.Color = vbYellow
End With
End With
End With
End Sub
I would do something like this:
Sub Dendrinos2()
Dim i As Long
Dim lr As Long
Dim sht As Worksheet
Set sht = ActiveSheet
lr = sht.Cells(sht.Rows.Count, 5).End(xlUp).Row
For i = lr To 2 Step -1
Checklength sht.Range("A" & i), 10
Checklength sht.Range("B" & i), 12
CheckLimits sht.Range("C" & i), -3, 6
CheckLimits sht.Range("G" & i), -3, 3
CheckLimits sht.Range("I" & i), -3, 3
CheckDashes sht.Range("E" & i), sht.Range("A" & i)
CheckDashes sht.Range("G" & i), sht.Range("A" & i)
CheckDashes sht.Range("I" & i), sht.Range("A" & i)
Next i
End Sub
Sub CheckLimits(c As Range, ll, ul)
With c
If .Value < ll Or .Value > ul Then .Interior.ColorIndex = 3
End With
End Sub
Sub CheckDashes(c As Range, cA As Range)
With c
If .Value = "--" Then
.Interior.ColorIndex = cA.Interior.ColorIndex
End If
End With
End Sub
Sub Checklength(c As Range, l As Long)
With c
If Len(.Value) > l Then .Interior.ColorIndex = 3
End With
End Sub

How to do multiple select with ActiveCell

I'm trying to make multiple selections from Sheet2. The value is from the same column but different rows (thinking if using ActiveCell.Offset(1,0) will be feasible).
My code takes the value from an ActiveCell select and runs a macro compares it to another sheet (Sheet10) with some information to copy and paste in a target sheet (Sheet5).
The following is the code that I have right now.
a = Sheet10.Cells(Rows.Count, 1).End(xlUp).Row
c = Sheet2.Cells(Rows.Count, 5).End(xlUp).Row
For Each cell In Range(ActiveCell, ActiveCell.Offset(1, 0))
For i = 2 To a 'from Row 1 to the last row of "DMP"
Debug.Print ("i = " & i)
If cell.Value = Sheet10.Cells(i, 1).Value Then 'if selected cell matches (i,1) of "Sheet10 (DMP)"
Debug.Print ("ActiveCell =" & ActiveCell.Value)
For k = 1 To 20 'from Column 1 to Column 20
Debug.Print ("k = " & k)
For r = 1 To c 'from Row 1 to the last row of "Sheet 2(LightOn SKU)"
Debug.Print ("r = " & r)
If Sheet10.Cells(i, k).Value = Sheet2.Cells(r, 4).Value Then 'if value of (i,k) of "Sheet10 (DMP)" = (r,4) of "Sheet2 (LightOn SKU)"
Sheet2.Range("A" & r & ":G" & r).Copy
Sheet5.Activate
b = Sheet5.Cells(Rows.Count, 1).End(xlUp).Row
Sheet5.Cells(b + 1, 1).Select
ActiveSheet.Paste
Range("A" & r & ":L" & r).Borders.Color = vbBlack
End If
Next
Next
End If
Next
Next
Right now, it's running on an endless loop.
Still Unclear
Sub ACCopy2()
Dim a As Long
Dim c As Long
Dim r As Long
Dim i As Long
Dim k As Integer
Dim b As Long
a = Sheet10.Cells(Rows.Count, 1).End(xlUp).Row
c = Sheet2.Cells(Rows.Count, 5).End(xlUp).Row
For r = 1 To c ' from Row 1 to the last row of "Sheet 2(LightOn SKU)"
Debug.Print ("r = " & r)
For i = 2 To a ' from Row 1 to the last row of "DMP"
Debug.Print ("i = " & i)
' if selected cell matches (i,1) of "Sheet10 (DMP)"
If Sheet2.Cells(r, 1).Value = Sheet10.Cells(i, 1).Value Then
Debug.Print ("Sheet2 =" & Sheet2.Cells(r, 1).Value)
For k = 1 To 20 ' from Column 1 to Column 20
Debug.Print ("k = " & k)
' if value of (i,k) of "Sheet10 (DMP)" = (r,4) of
' "Sheet2 (LightOn SKU)"
If Sheet10.Cells(i, k).Value = Sheet2.Cells(r, 4).Value Then
With Sheet5
b = Sheet5.Cells(Rows.Count, 1).End(xlUp).Row
Sheet2.Range("A" & r & ":G" & r).Copy .Cells(b + 1, 1)
.Range("A" & r & ":L" & r).Borders.Color = vbBlack
End With
End If
Next
End If
Next
Next
End Sub

How to select row before and after row with specific text in Excel?

My raw data looks something like this;
std1
std1
deviant
std2
std1
std2
std2
deviant
The "deviants" are presented randomly and thus do not occur every nth row...
I wish to select 1 row before and 1 row after each "deviant" row so I can copy it in another spread sheet.
See code below.
We loop through each row in the column (I have assumed your data is in column A) and when the given value is found, we add the following and prior rows to our selection array. When the loop is complete, we select the rows in the array
Public Sub DeviantSelect()
Dim myRange As Range
Set myRange = Nothing
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To lastRow
If Cells(i, 1) = "deviant" Then
If myRange Is Nothing Then
Set myRange = Union(Range(i - 1 & ":" & i - 1), Range(i + 1 & ":" & i + 1))
Else
Set myRange = Union(myRange, Range(i - 1 & ":" & i - 1), Range(i + 1 & ":" & i + 1))
End If
myRange.Select
End If
Next
End Sub
The below code copies the cells before and after deviant to another sheet.
Sub check()
Sheet1.Activate
Range("A1").Select
LastRow = Sheets("Sheet1").UsedRange.Rows(Sheets("Sheet1").UsedRange.Rows.Count).Row
For i = 1 To LastRow
Sheet1.Activate
If Range("A" & i).Value = "deviant" Then
Range("A" & i - 1).Select
Selection.Copy
Sheet2.Activate
LastRow2 = Sheets("Sheet2").UsedRange.Rows(Sheets("Sheet2").UsedRange.Rows.Count).Row
If LastRow2 = 1 Then
Range("A" & LastRow2).Activate
Else
Range("A" & LastRow2 + 1).Activate
End If
ActiveSheet.Paste
Sheet1.Activate
Range("A" & i + 1).Select
Selection.Copy
Sheet2.Activate
LastRow2 = Sheets("Sheet2").UsedRange.Rows(Sheets("Sheet2").UsedRange.Rows.Count).Row
Range("A" & LastRow2 + 1).Activate
ActiveSheet.Paste
End If
Next
End Sub

Troubles stopping my loop

Do While Cells(i, 1).Value <> ""
....
End If
i = i + 1
Loop
End Sub
Right. It works fine with numbers and stop perfectly. But With Text. It does not stop.
Ideally I want to stop at the last row of my content rather than my last row in Excel. I manage to make it work fine with numbers, but I cannot fix it with Text.
Any help would be great as I am a beginner in VBA.
Sub checkRoutine()
Dim i As Integer
Dim LastRow As Long
i = 1
Do While Cells(i, 1).Value <> ""
If IsNumeric(Cells(i, 1).Value) Then Cells(i, 2).Value = Cells(i, 1).Value & " " & Cells(7, 5).Value
If Not IsNumeric(Cells(i, 1).Value) Then
LastRow = Range("A" & Rows.Count).End(xlUp).row + 1
ActiveSheet.Cells(LastRow, "A").Value = Cells(i, 1).Value & " " & Cells(7, 5).Value
End If
i = i + 1
Loop
End Sub
As suggested by so many people, you need to change to use a For loop:
Sub checkRoutine()
Dim i As Long
Dim LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).row
For i = 1 To LastRow
If IsNumeric(Cells(i, 1).Value) Then
Cells(i, 2).Value = Cells(i, 1).Value & " " & Cells(7, 5).Value
Else
LastRow = Range("A" & Rows.Count).End(xlUp).row + 1
Cells(LastRow, "A").Value = Cells(i, 1).Value & " " & Cells(7, 5).Value
End If
Next
End Sub

Copying columns from a worksheet to another

Something is wrong with my code I can't figure out.
I'd like to copy a column C from workbook 1 of worksheet wsCopyFrom_OFP to column D in workbook 2 of worksheet wsCopyTo so that only rows with values will be copied
LastRow_OFP = wsCopyFrom_OFP.Cells(wsCopyFrom_OFP.Rows.Count, "C").End(xlUp).Row
For i = 4 To LastRow_OFP
wsCopyFrom_OFP.Cells(i, 3).Copy
erow = wsCopyTo.Cells(wsCopyTo.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
wsCopyFrom_OFP.Paste Destination:=Worksheets(wsCopyTo).Cells(erow, 1)
Next i
Help very much appreciated
Try with below code
LastRow_OFP = wsCopyFrom_OFP.Cells(wsCopyFrom_OFP.Rows.Count, "C").End(xlUp).Row
For i = 4 To LastRow_OFP
wsCopyFrom_OFP.Cells(i, 3).Copy wsCopyTo.Range("A" & wsCopyTo.Range("A" & Rows.Count).End(xlUp).Row + 1)
Next i
EDIT #1
Try with below code
LastRow_OFP = wsCopyFrom_OFP.Cells(wsCopyFrom_OFP.Rows.Count, "C").End(xlUp).Row
For i = 4 To LastRow_OFP
wsCopyFrom_OFP.Range("C" & i & ":C" & i + 3).Copy
wsCopyTo.Range("A" & wsCopyTo.Range("A" & Rows.Count).End(xlUp).Row + 1).PasteSpecial (xlPasteValues)
Next i

Resources