Excel VBA Paste error when applying style to cell - excel

I'm seeking some assistance. I have a code that does what I need and works pretty fine, but I want to make it do some more, and thats when it breaks.
Here is the code, a bit messy I know:
Sub AgainstAbstain()
Application.ScreenUpdating = False
'Stating variables
Dim Abstain As String
Abstain = "Abstain"
Dim Against As String
Against = "Against"
Dim C11 As Variant
'Enter amount of votable items
Dim e As Byte 'number of agenda items
e = InputBox("Number of votable items in Agenda?")
'Create Necessary sheets
On Error Resume Next
Sheets("Abstain").Delete
'Sheets("Against").Delete
On Error GoTo 0
ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
'ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
ActiveWorkbook.Sheets(2).Name = "Abstain"
'ActiveWorkbook.Sheets(3).Name = "Against"
'Change zoom level of sheets
Sheets(2).Activate
ActiveWindow.Zoom = 85
'Sheets(3).Activate
'ActiveWindow.Zoom = 85
Sheets(1).Activate
'For better copying of cells
Cells.WrapText = False
'To count spaces
Dim j As Integer
j = 1
Dim k As Integer
k = 1
Dim c As Integer
c = 3 '
'Main filter and copy
For i = 1 To e
Worksheets(1).Cells(11, c).Select
C11 = ActiveCell.Value
'Range("A11:C11").Select
Range(Cells(11, 1), Cells(11, c)).Select
Selection.AutoFilter
ActiveSheet.Range("C:C").AutoFilter Field:=c, Criteria1:="ABSTAIN"
'Amount of items visible after filter
Dim x As Integer
x = Application.Subtotal(3, Columns("A")) - 19
'MsgBox x
If x > 0 Then
ActiveSheet.AutoFilter.Range.Offset(1).Copy
Sheets("ABSTAIN").Activate
' Range("A" & j).Select
' Range("A" & j).Font.Bold = True
' Range("A" & j).Font.Underline = True
Range("A" & j).Value = C11 & ") " & Abstain
j = j + 2
' Range("A" & j).Select
Range("A" & j).Value = "Beneficial owner:"
'Range("A" & j).Font.Bold = True
Range("B" & j).Value = "Number of shares:"
'Range("A" & j).Font.Bold = True
j = j + 1
Sheets(2).Range("A" & j).PasteSpecial
' Range("A" & j).Select
' ActiveSheet.Paste
j = j + x
Range("A" & j).Value = "Sum"
Range("A" & j).Font.Bold = True
Range("A" & j).Interior.Color = RGB(255, 204, 153)
Range("B" & j).Font.Bold = True
Range("B" & j).Interior.Color = RGB(255, 204, 153)
j = j + 3
Columns(3).EntireColumn.Delete
Err.Clear
Sheets(1).Activate
Worksheets(1).Columns(c).Hidden = True
c = c + 1
Cells.AutoFilter
Else: Cells.AutoFilter
Worksheets(1).Columns(c).Hidden = True
c = c + 1
End If
Next i
Cells.EntireColumn.Hidden = False
c = 3
For i = 1 To e
Worksheets(1).Cells(11, c).Select
C11 = ActiveCell.Value
'Range("A11:C11").Select
Range(Cells(11, 1), Cells(11, c)).Select
Selection.AutoFilter
ActiveSheet.Range("C:C").AutoFilter Field:=c, Criteria1:="AGAINST"
'Amount of items visible after filter
Dim y As Integer
y = Application.Subtotal(3, Columns("A")) - 19
'MsgBox y
If y > 0 Then
ActiveSheet.AutoFilter.Range.Offset(1).Copy
Sheets("Abstain").Activate
' Range("A" & j).Select
Range("A" & j).Value = C11 & ") " & Abstain
j = j + 2
' Range("A" & j).Select
Range("A" & j).Value = "Beneficial owner:"
Range("B" & j).Value = "Number of shares:"
j = j + 1
Sheets(2).Range("A" & j).PasteSpecial
' Range("A" & j).Select
' ActiveSheet.Paste
j = j + y
Range("A" & j).Value = "Sum"
Range("A" & j).Font.Bold = True
Range("A" & j).Interior.Color = RGB(255, 153, 204)
Range("B" & j).Font.Bold = True
Range("B" & j).Interior.Color = RGB(255, 153, 204)
j = j + 3
Columns(3).EntireColumn.Delete
Err.Clear
Sheets(1).Activate
Worksheets(1).Columns(c).Hidden = True
c = c + 1
Cells.AutoFilter
Else: Cells.AutoFilter
Worksheets(1).Columns(c).Hidden = True
c = c + 1
End If
'If y > 0 Then
'ActiveSheet.AutoFilter.Range.Offset(1).Copy
' Sheets("AGAINST").Activate
' Range("A" & k).Select
' Range("A" & k).Value = C11 & ") " & Against
' k = k + 2
' Range("A" & k).Select
' Range("A" & k).Value = "Beneficial owner:"
' k = k + 1
' Range("A" & k).Select
' ActiveSheet.Paste
' k = k + y
' Range("A" & k).Value = "Sum"
' k = k + 3
' Columns(3).EntireColumn.Delete
' Err.Clear
' Sheets(1).Activate
' Cells.AutoFilter
' 'Columns(3).EntireColumn.Delete
' Worksheets(1).Columns(c).Hidden = True
' c = c + 1
'Else: Cells.AutoFilter
' 'Columns(3).EntireColumn.Delete
' Worksheets(1).Columns(c).Hidden = True
' c = c + 1
'End If
Next i
Sheets(2).Activate
For Each NumRange In Columns("B").SpecialCells(xlConstants, xlNumbers).Areas
SumAddr = NumRange.Address(False, False)
NumRange.Offset(NumRange.Count, 0).Resize(1, 1).Formula = "=SUM(" & SumAddr & ")"
c = NumRange.Count
Next NumRange
NoData:
'Sheets(2).Select
Columns("A:B").AutoFit
Sheets(1).Activate
Cells.EntireColumn.Hidden = False
Application.ScreenUpdating = True
End Sub
It filters and moves data just fine. But when i try to activate this part
' Range("A" & j).Font.Bold = True
' Range("A" & j).Font.Underline = True
It gives me this error
Run-time error '1004':
PasteSpecial method of Range class failed. In fact, if I try to activate any style change before the paste i get this error.
And highlights this area
Sheets(2).Range("A" & j).PasteSpecial
I just don't get.

After the .Copy method you need to immediately paste the results. Doing anything else will empty the copy buffer, so this will work:
ActiveSheet.Range("A1").Copy
ActiveSheet.Range("A2").PasteSpecial
ActiveSheet.Range("A1").Font.Size = 10
But this won't
ActiveSheet.Range("A1").Copy
ActiveSheet.Range("A1").Font.Size = 10
ActiveSheet.Range("A2").PasteSpecial

Related

How do you fix VBA code that counts 1 too many?

I have written a program that counts bins that are empty (verified), empty (unverified), and not accessible (bins locked).
I am trying to count the bins that are locked from my Bin Conversions sheet that if they are TRUE (there are 20 that are true), then they are locked and will be counted on my Bin Report sheet.
My Bin Reports sheet counts 1 too many for each group (all groups total 23 instead of 20). A group example would be 4-Pallet, 2.5ft, 2 bins locked (instead of 1).
Bin Report
Bin Conversions
Sub getBinStatusArray()
calc (False)
Dim dSH As Worksheet
Dim brSH As Worksheet
Dim bcSH As Worksheet
Set dSH = ThisWorkbook.Sheets("data")
Set brSH = ThisWorkbook.Sheets("Bin Report")
Set bcSH = ThisWorkbook.Sheets("Bin Conversions")
Dim binLockCell As Byte, binType As String, binSize As Variant, binLocked As Boolean, b As Long, i As Long
Dim dataArray() As Variant
Dim binIDArray As Variant
'Create empty array cells
ReDim Preserve dataArray(1 To dSH.Range("A" & Rows.Count).End(xlUp).Row, 1 To 3)
'Navigates cells
With dSH
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
dataArray = .Range(.Cells(lastrow, 1), .Cells(1,
.Columns.Count).End(xlToLeft)).Value
End With
'Count Bin Conversion Cells
With bcSH
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
.Range("E" & i).Value2 = Application.WorksheetFunction.CountIf(dSH.Range("A:A"), .Range("A" & i).Value2)
Next i
End With
'Generate Bin Report
With brSH
.Cells.ClearContents
.Range("H1").Value = "Filter Input"
.Range("B1").Value = "Bin Type"
.Range("I1").Value = "Bin Type"
.Range("C1").Value = "Bin Height"
.Range("J1").Value = "Bin Height"
.Range("D1").Value = "Verified"
.Range("K1").Value = "Verified"
.Range("E1").Value = "Unverified"
.Range("L1").Value = "Unverified"
.Range("F1").Value = "Bins Locked"
.Range("M1").Value = "Bins Locked"
For i = 2 To lastrow
If bcSH.Range("E" & i).Value = 1 Or Application.WorksheetFunction.CountIfs(bcSH.Range("G" & i), "true") Then
binType = bcSH.Range("B" & i).Value
binSize = bcSH.Range("C" & i).Value
binLocked = bcSH.Range("H" & i).Value
If .Range("b2") = "" Then
.Range("b2").Value = bcSH.Range("B" & i).Value
.Range("c2").Value = bcSH.Range("C" & i).Value
.Range("F2").Value2 = Application.WorksheetFunction.CountIfs(bcSH.Range("G" & i), "true")
ElseIf .Range("b2") <> "" Then
lastrow = brSH.Cells(Rows.Count, 2).End(xlUp).Row
For b = 2 To lastrow + 1
If brSH.Range("B" & b) = binType And brSH.Range("C" & b) = binSize Then
brSH.Range("D" & b) = brSH.Range("D" & b) + bcSH.Range("E" & i)
binLockCell = Application.WorksheetFunction.CountIfs(bcSH.Range("G" & i), "true")
brSH.Range("F" & b) = binLockCell + brSH.Range("F" & b)
Exit For
ElseIf b = lastrow Then
.Range("b" & b + 1).Value = bcSH.Range("B" & i).Value
.Range("c" & b + 1).Value = bcSH.Range("c" & i).Value
.Range("D" & b + 1).Value = bcSH.Range("E" & i).Value
binLockCell = Application.WorksheetFunction.CountIfs(bcSH.Range("G" & i), "true")
.Range("F" & b + 1) = binLockCell + .Range("F" & b + 1)
End If
Next b
End If
End If
Next i
Range("b1").CurrentRegion.sort key1:=Range("b1"), order1:=xlAscending, _
key2:=Range("C1"), order2:=xlAscending, Header:=xlYes
End With
calc (True)
End Sub
You are looping For b = 2 To lastrow + 1 but adding a new line when b = lastrow i.e. before the loop has ended. So on the last iteration when b = lastrow + 1 it summates the record again. One fix would be use a flag.
ElseIf .Range("b2") <> "" Then
Dim bExists: bExists = False
lastrow = brSH.Cells(Rows.Count, 2).End(xlUp).Row
' increment existing
For b = 2 To lastrow
If brSH.Range("B" & b) = binType And brSH.Range("C" & b) = binSize Then
brSH.Range("D" & b) = brSH.Range("D" & b) + bcSH.Range("E" & i)
binLockCell = Application.WorksheetFunction.CountIfs(bcSH.Range("G" & i), "true")
brSH.Range("F" & b) = binLockCell + brSH.Range("F" & b)
bExists = True
Exit For
Next b
' or add new line
If Not bExists Then
.Range("b" & b + 1).Value = bcSH.Range("B" & i).Value
.Range("c" & b + 1).Value = bcSH.Range("c" & i).Value
.Range("D" & b + 1).Value = bcSH.Range("E" & i).Value
binLockCell = Application.WorksheetFunction.CountIfs(bcSH.Range("G" & i), "true")
.Range("F" & b + 1) = binLockCell + .Range("F" & b + 1)
End If
End If

VBA How to do a recurring cycle

I have code that generates stickers, I wrote it this way below, so for 15 stickers. Can it be done with a loop for easier code writing because I have to do it up to 100 stickers? I have currently written it for 15 stickers and it works by changing the cell names manually because they are different.
Thank you
Code is:
Sheets("Helper").Range("A1:Z50").Clear
Sheets("DUT Sticker").Range("A8:C100").Clear
If Sheets("Helper").Range("A1").Value <> "" Then
Sheets("DUT Sticker").Range("A3:C7").Copy
Sheets("DUT Sticker").Range("A8").PasteSpecial Paste:=xlPasteAll
Sheets("DUT Sticker").Range("A8").Value = Sheets("DUT Sticker").Range("A3") + 1
Sheets("DUT Sticker").Select
ActiveSheet.Shapes.Range(Array("Picture 3")).Select
Selection.Copy
Sheets("DUT Sticker").Range("B8").Select
ActiveSheet.Paste
Selection.ShapeRange.IncrementLeft 2
Selection.ShapeRange.IncrementTop 4
Sheets("Helper").Range("A1").Copy
Sheets("DUT Sticker").Range("B8").PasteSpecial Paste:=xlPasteAll
Sheets("DUT Sticker").Range("B9") = Sheets("Helper").Range("B2")
Sheets("DUT Sticker").Range("B10") = Sheets("Helper").Range("J2")
Sheets("DUT Sticker").Range("B11") = Sheets("Helper").Range("K2")
Sheets("DUT Sticker").Range("C9").Formula = "=IF(Helper!R[-7]="""",""SPARE"",""SS ""&Helper!R[-7]&""#""&Helper!R[-7]C[1])"
Sheets("DUT Sticker").Range("C10") = Sheets("Sample Recording Form").Range("F5")
Sheets("DUT Sticker").Range("C12").Formula = "S/N: " & Sheets("Helper").Range("I2")
Sheets("DUT Sticker").PageSetup.PrintArea = "$B$3:$C$12"
End If
If Sheets("Helper").Range("A2").Value <> "" Then
Sheets("DUT Sticker").Range("A3:C7").Copy
Sheets("DUT Sticker").Range("A13").PasteSpecial Paste:=xlPasteAll
Sheets("DUT Sticker").Range("A13").Value = Sheets("DUT Sticker").Range("A8") + 1
Sheets("DUT Sticker").Select
ActiveSheet.Shapes.Range(Array("Picture 3")).Select
Selection.Copy
Sheets("DUT Sticker").Range("B13").Select
ActiveSheet.Paste
Selection.ShapeRange.IncrementLeft 2
Selection.ShapeRange.IncrementTop 4
Sheets("Helper").Range("A2").Copy
Sheets("DUT Sticker").Range("B13").PasteSpecial Paste:=xlPasteAll
Sheets("DUT Sticker").Range("B14") = Sheets("Helper").Range("B3")
Sheets("DUT Sticker").Range("B15") = Sheets("Helper").Range("J3")
Sheets("DUT Sticker").Range("B16") = Sheets("Helper").Range("K3")
Sheets("DUT Sticker").Range("C14").Formula = "=IF(Helper!R[-11]="""",""SPARE"",""SS ""&Helper!R[-11]&""#""&Helper!R[-11]C[1])"
Sheets("DUT Sticker").Range("C15") = Sheets("Sample Recording Form").Range("F5")
Sheets("DUT Sticker").Range("C17").Formula = "S/N: " & Sheets("Helper").Range("I3")
Sheets("DUT Sticker").PageSetup.PrintArea = "$B$3:$C$17"
End If
end sub
There are different ways to access another cell:
i = 15
Cell_A15_Value = Range("A" & i).Value
Cell_A15_Value = Range("A1").OffSet(14,0).Value
(Obviously, as you start by row 1, you need to add 14 to get to cell A15.
Please, try the next code. It is not tested, of course. I only tried following your code logic, adapting it to avoid selections and working faster. I would suggest you to try it line by line (pressing F8) and see what it does. I shouldn't be impossible to miss something in terms of variables incrementation:
Sub MakeStickers()
Dim shH As Worksheet, shD As Worksheet, lastRH As Long, i As Long, k As Long, J As Long, M As Long
Dim sh As Shape, constVal
constVal = Sheets("Sample Recording Form").Range("F5").value
k = 8: J = 3: M = 2
Set shH = Sheets("Helper")
lastRH = shH.Range("A" & shH.rows.count).End(xlUp).row
Set shD = Sheets("DUT Sticker")
shD.Range("A8:C100").Clear
'Sheets("Helper").Range("A1:Z50").Clear 'clearing this sheet, the iteration will never find a range in A:A column <> ""
For i = 1 To lastRH
If shH.Range("A" & i).value <> "" Then
shD.Range("A3:C7").Copy shD.Range("A" & k)
shD.Range("A" & k).value = shD.Range("A" & J) + 1
shD.Shapes("Picture 3").Copy: shD.Paste
Set sh = shD.Shapes(shD.Shapes.count)
sh.top = shD.Range("B" & k).top
sh.left = shD.Range("B" & k).left
shD.Range("B" & k).value = shH.Range("A" & i).value
shD.Range("B" & k + 1).value = Sheets("Helper").Range("B" & M).value
shD.Range("B" & k + 2).value = Sheets("Helper").Range("J" & M).value
shD.Range("B" & k + 3).value = Sheets("Helper").Range("K" & M)
shD.Range("C" & k + 1).Formula = "=IF(Helper!R[-" & k - 1 & "]="""",""SPARE"",""SS ""&Helper!R[-" & k - 1 & "]&""#""&Helper!R[-" & k - 1 & "]C[1])"
shD.Range("C" & k + 2).value = constVal
shD.Range("C" & k + 4).value = "S/N: " & Sheets("Helper").Range("I" & M).value
Sheets("DUT Sticker").PageSetup.PrintArea = "$B$3:$C$" & k + 4
'reset variables:
k = k + 5: M = M + 1: J = J + 5
End If
Next i
End Sub
If not confidential, can you share the workbook in discussion? I will try adapting the above code according to its behavior on the real workbook...

How to merge several cells using VBA

I have some problems with excel and VBA, in that don't know have much knowledge. I copied text from pdf and it's awful.
I have cells which contain some text.
The problem is that the text from one paragraph is broken down over several cells. At the beginning of each paragraph is a word in bold (e.g. CLR.) which describes the rest of the text. As such, it defines where each paragraph should start. How I can merge these cells into one?
I see
I want
Sub MergeText()
Dim strMerged$, r&, j&, i&
r = 1
Do While True
If Cells(r, 1).Characters(1, 1).Font.Bold Then
strMerged = "": strMerged = Cells(r, 1)
r = r + 1
While (Not Cells(r, 1).Characters(1).Font.Bold) And Len(Cells(r, 1)) > 0
strMerged = strMerged & Cells(r, 1)
r = r + 1
Wend
i = i + 1: Cells(i, 2) = strMerged
Cells(i, 2).Characters(1, InStr(1, strMerged, ".", vbTextCompare)).Font.Bold = True
Else
Exit Do
End If
Loop
End Sub
Modify (if needed) and try:
Option Explicit
Sub test()
Dim LastRow As Long, i As Long, j As Long, Count As Long
Dim str As String
With ThisWorkbook.Worksheets("Sheet1") 'Change sheet name if needed
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = LastRow To 2 Step -1
If (UCase(Left(.Range("A" & i), 1)) <> Left(.Range("A" & i), 1)) And UCase(Left(.Range("A" & i - 1), 1)) = Left(.Range("A" & i - 1), 1) Then
Count = 0
For j = 1 To Len(.Range("A" & i - 1))
If .Range("A1").Characters(j, 1).Font.FontStyle = "Bold" Then
Count = Count + 1
Else
Exit For
End If
Next j
str = .Range("A" & i - 1).Value & " " & .Range("A" & i).Value
With .Range("A" & i - 1)
.Value = str
.Font.Bold = False
With .Characters(Start:=1, Length:=Count).Font
.FontStyle = "Bold"
End With
End With
.Rows(i).EntireRow.Delete
ElseIf (UCase(Left(.Range("A" & i), 1)) <> Left(.Range("A" & i), 1)) And UCase(Left(.Range("A" & i - 1), 1)) <> Left(.Range("A" & i - 1), 1) Then
str = .Range("A" & i - 1).Value & " " & .Range("A" & i).Value
With .Range("A" & i - 1)
.Value = str
.Font.Bold = False
End With
.Rows(i).EntireRow.Delete
End If
Next i
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 prevent the move of cells in excel when using Worksheet_Change Event

I have this code and it works just fine.
The only problem is that after i press enter ,in cell "A2" for example, instead of moving down to cell "A3", like it normally would - it moves to cell "E3", so it makes hard on the user to type.
Any suggestions?
Private Sub Worksheet_change(ByVal Target As Range)
Application.EnableEvents = False
Range("A2:M2").Interior.ColorIndex = 19
Dim LASTROW As Long
TheLastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
Dim intx As Variant
For i = 2 To TheLastRow
If Range("a" & i) = Range("a" & i + 1) Then
Range("A" & i + 1 & ":n" & i + 1).Interior.Color = Range("a" & i).Interior.Color
intx = intx + 0
Else
Range("A" & i + 1 & ":n" & i + 1).Interior.ColorIndex = 46 - intx
intx = intx + 1
End If
Next i
For i = 2 To TheLastRow
Range("e" & i).Select
ActiveCell.FormulaR1C1 = _
"=IF(ISBLANK(RC[-1]),"""",HYPERLINK(""PCDOCS://PCDOCS_JLM/""&RC[-1]&""/R"",""link""))"
Next i
Application.EnableEvents = True
End Sub
You should avoid using SELECT or ACTIVATE in VBA, so:
Private Sub Worksheet_change(ByVal Target As Range)
Application.EnableEvents = False
Range("A2:M2").Interior.ColorIndex = 19
Dim LASTROW As Long
TheLastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
Dim intx As Variant
For i = 2 To TheLastRow
If Range("a" & i) = Range("a" & i + 1) Then
Range("A" & i + 1 & ":n" & i + 1).Interior.Color = Range("a" & i).Interior.Color
intx = intx + 0
Else
Range("A" & i + 1 & ":n" & i + 1).Interior.ColorIndex = 46 - intx
intx = intx + 1
End If
Next i
For i = 2 To TheLastRow
Range("e" & i).FormulaR1C1 = _
"=IF(ISBLANK(RC[-1]),"""",HYPERLINK(""PCDOCS://PCDOCS_JLM/""&RC[-1]&""/R"",""link""))"
Next i
Application.EnableEvents = True
End Sub
I made some modifications to your code, and when I press {enter} on cell "A2" it performs the code and "jumps" to cell "A3".
Code
Option Explicit
Private Sub Worksheet_change(ByVal Target As Range)
Dim C As Range
Dim intx As Long
Application.EnableEvents = False
Range("A2:M2").Interior.ColorIndex = 19
' loop through all cells with data in column "A"
For Each C In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
If C.Value = C.Offset(1, 0).Value Then
C.Offset(1, 0).Resize(1, 14).Interior.Color = C.Interior.Color
Else
C.Offset(1, 0).Resize(1, 14).Interior.Color = 46 - intx
intx = intx + 1
End If
Next C
' loop through all cells with data in column "E"
For Each C In Range("E2:E" & Cells(Rows.Count, "E").End(xlUp).Row)
C.FormulaR1C1 = "=IF(ISBLANK(RC[-1]),"""",HYPERLINK(""PCDOCS://PCDOCS_JLM/""&RC[-1]&""/R"",""link""))"
Next C
Application.EnableEvents = True
End Sub
You can Read out the Address from the Cell that Trigert the Event and save them.
After your code is done, you can select the Cell, 1 Row below.
Hope this Helps.
Private Sub Worksheet_change(ByVal Target As Range)
Application.EnableEvents = False
Dim rngAddress As String
rngAddress = Target.Address
Range("A2:M2").Interior.ColorIndex = 19
Dim LASTROW As Long
TheLastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
Dim intx As Variant
For i = 2 To TheLastRow
If Range("a" & i) = Range("a" & i + 1) Then
Range("A" & i + 1 & ":n" & i + 1).Interior.Color = Range("a" & i).Interior.Color
intx = intx + 0
Else
Range("A" & i + 1 & ":n" & i + 1).Interior.ColorIndex = 46 - intx
intx = intx + 1
End If
Next i
For i = 2 To TheLastRow
Range("e" & i).Select
ActiveCell.FormulaR1C1 = _
"=IF(ISBLANK(RC[-1]),"""",HYPERLINK(""PCDOCS://PCDOCS_JLM/""&RC[-1]&""/R"",""link""))"
Next i
Range(rngAddress).offset(1,0).select
Application.EnableEvents = True
End Sub

Resources