CorrieLynn
New Member
- Joined
- Aug 7, 2023
- Messages
- 1
- Office Version
- 365
- 2016
- Platform
- Windows
- Web
I am creating an import sheet that needs to have a row inserted above each change in value in column B and then number each row starting at 0 for the col B value in col I. The results from the code below are not inserting a row above the last value in col B row 2 and is not adding line number 1 in col I. Any and all help is appreciated.
' Initialize lineNumber and currentGroup variables
lineNumber = 0
Dim currentGroup As String
currentGroup = wsImport.Cells(2, "B").Value
' Insert row above each change in value in col B copy values from A:H from row below then number the lines in col I
For i = lastRowImport To 3 Step -1
If wsImport.Cells(i, "B").Value <> wsImport.Cells(i - 1, "B").Value Then
wsImport.Rows(i).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
wsImport.Range("A" & i & ":H" & i).Value = wsImport.Range("A" & i + 1 & ":H" & i + 1).Value
currentGroup = wsImport.Cells(i, "B").Value
lineNumber = 0 ' Reset lineNumber for each group
End If
wsImport.Cells(i, "I").Value = lineNumber
lineNumber = lineNumber + 1 ' Increment lineNumber for each row within the group
Next i
' Initialize lineNumber and currentGroup variables
lineNumber = 0
Dim currentGroup As String
currentGroup = wsImport.Cells(2, "B").Value
' Insert row above each change in value in col B copy values from A:H from row below then number the lines in col I
For i = lastRowImport To 3 Step -1
If wsImport.Cells(i, "B").Value <> wsImport.Cells(i - 1, "B").Value Then
wsImport.Rows(i).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
wsImport.Range("A" & i & ":H" & i).Value = wsImport.Range("A" & i + 1 & ":H" & i + 1).Value
currentGroup = wsImport.Cells(i, "B").Value
lineNumber = 0 ' Reset lineNumber for each group
End If
wsImport.Cells(i, "I").Value = lineNumber
lineNumber = lineNumber + 1 ' Increment lineNumber for each row within the group
Next i