Anthony Norton
New Member
Hi Guys,
Got a bugging problem. I have attached an excel file to illustrate this issue. In Sheet1 is the current state of data I get, Sheet2 is what I want to achieve using VBA. However, all changes are to be done in the same sheet. Below is my code:
This code works fine in terms of inserting rows and copying values from the above columns. But if you refer to my excel, I would like the column A Number to be tagged to the Description. For eg, if the Description is "RECL FOR ABC", the digit before "-" in column A should reflect 1. If the Description is "RECL FOR XYZ, the digit before "-" should be 2. All digits after "-" should be the same as the cells above the inserted rows. Pls help thanks. I feel this should be more simple than it seems.
Got a bugging problem. I have attached an excel file to illustrate this issue. In Sheet1 is the current state of data I get, Sheet2 is what I want to achieve using VBA. However, all changes are to be done in the same sheet. Below is my code:
Code:
Option Explicit
Sub DuplicateRowsInGroups()
Dim arrOLD As Variant, arrNEW As Variant
Dim Rw As Long, Col As Long, NewRw As Long, LR As Long, i As Long
Dim FR As Long, oldNUM As String, newNUM As String
LR = Range("A" & Rows.Count).End(xlUp).Row
arrOLD = Range("A2:C" & LR).Value
ReDim arrNEW(1 To LR * 2, 1 To 3)
NewRw = 1
For Rw = 1 To UBound(arrOLD)
If FR = 0 Then
FR = Rw
oldNUM = arrOLD(Rw, 1)
newNUM = Mid(oldNUM, InStr(oldNUM, "-"), 100)
End If
For Col = 1 To 3
arrNEW(NewRw, Col) = arrOLD(Rw, Col)
Next Col
NewRw = NewRw + 1
If Rw = UBound(arrOLD) Then
For i = FR To Rw
arrNEW(NewRw, 1) = newNUM
arrNEW(NewRw, 2) = -arrOLD(i, 2)
arrNEW(NewRw, 3) = arrOLD(i, 3)
NewRw = NewRw + 1
Next i
Exit For
ElseIf arrOLD(Rw, 1) <> arrOLD(Rw + 1, 1) Then
For i = FR To Rw
arrNEW(NewRw, 1) = newNUM
arrNEW(NewRw, 2) = -arrOLD(i, 2)
arrNEW(NewRw, 3) = arrOLD(i, 3)
NewRw = NewRw + 1
Next i
FR = 0
End If
Next Rw
Range("A:A").NumberFormat = "@"
Range("A2:C2").Resize(UBound(arrNEW)).Value = arrNEW
End Sub
This code works fine in terms of inserting rows and copying values from the above columns. But if you refer to my excel, I would like the column A Number to be tagged to the Description. For eg, if the Description is "RECL FOR ABC", the digit before "-" in column A should reflect 1. If the Description is "RECL FOR XYZ, the digit before "-" should be 2. All digits after "-" should be the same as the cells above the inserted rows. Pls help thanks. I feel this should be more simple than it seems.
Attachments
Last edited by a moderator: