Inserting rows and adding line numbers

CorrieLynn

New Member
Joined
Aug 7, 2023
Messages
1
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. 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
 

Attachments

  • Current_Results.jpg
    Current_Results.jpg
    155.9 KB · Views: 32
  • Desired_Results.jpg
    Desired_Results.jpg
    165.6 KB · Views: 39

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top