VBA - adding row if column has value

mcc1323

New Member
Joined
May 30, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Currently copying from an external workbook to my working workbook & worksheet.

This is the external worksheet:

1653897753266.png


My code below splits my column with multiple contract numbers as planned, however I want to be able to go from this:

1653897946145.png


To this below, where there is data past column D, there is another row added and the data from A:C is replicated and the contract number moves to column D. In addition, all columns past Column D are to be cleared.

1653898147337.png


Basically, for Contract 40007304, a line gets added (B7 as per below), and the details for Columns A-C for line B6 has been replicated for it. Same goes for all multiple contract #s.
Another complication is that there can be up to 10 unique contract numbers for a customer account, so the goal is for it to be the same as the above.



Current copy-paste VBA is as per below:

VBA Code:
Sub get_copyt()

Application.ScreenUpdating = False 

Dim App As New Excel.Application 
Dim wb As Workbook
Dim copy As Range
Dim filter_date As Date

Set wb = ThisWorkbook
Set ws = Sheets("SheetA")

ws.Range("B3:E10000").ClearContents

Set copy_wb = App.Workbooks.Open(Filename:="ABC.csv", UpdateLinks:=True, ReadOnly:=True)

Set sht = wb.Worksheets(1)
Set StartCell = Range("A1")

lastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
lastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column

copy_wb.Worksheets("Sheet1").Range("A2:D" & lastRow).Copy
                    
Sheets("SheetA").Range("B3").PasteSpecial Paste:=xlPasteValues
copy_wb.Application.CutCopyMode = False
copy_wb.Close SaveChanges:=False

With ws
    .Range("C3:C500").Cut Destination:=ws.Range("E3:E500")
    .Range("C3:C500").Delete Shift:=xlToLeft
    

Set myRng = ws.Range("D3:D" & lastRow)
    myRng.TextToColumns _
        Destination:=ws.Range("E3:K3"), _
        DataType:=xlDelimited, _
        Tab:=False, _
        Semicolon:=False, _
        Comma:=True, _
        Space:=False, _
        Other:=False

End Sub



Help would be much appreciated on how to append the new code line to the existing one as I'm a bit lost.

Thank you!
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try to attach a mini sheet via XL2BB. In order to test the output, helpers have to input data manually, but most of them (include me :) )do not want to do that
 
Upvote 0
Try to attach a mini sheet via XL2BB. In order to test the output, helpers have to input data manually, but most of them (include me :) )do not want to do that
Sorry about that!

Book2
ABCD
1Account NameAccount NumberContract Number(s)Product
2Customer A123440007304A1
3Customer B567840007397A1
4Customer C912340007781A1
5Customer D456740007325A1
6Customer E891240007650, 40007304A1
7Customer F345640007781A1
8Customer G789140007994A1
9Customer H234540007821, 40007806A1
10Customer I678940007687A2
11Customer J112340006576, 40007386A1
12Customer K445640007723A1
13Customer L778940007154A3
14CustomerM122340006632A4
Sheet1


Data here. Thank you!
 
Upvote 0
I am testing with separate sub. If it worked, try to combine it into your main code:
VBA Code:
Sub splitContract()
Dim lr&, i&, k&, cell As Range, s, arr(1 To 65000, 1 To 4)
lr = Cells(Rows.Count, "C").End(xlUp).Row ' last row of column C
For Each cell In Range("C2:C" & lr)
    s = Split(cell, ",") ' split contract numbers, base on ","
    For i = 0 To UBound(s) ' loop through each contract number of each row
        k = k + 1
        arr(k, 1) = cell.Offset(0, -2).Value
        arr(k, 2) = cell.Offset(0, -1).Value
        arr(k, 3) = s(i)
        arr(k, 4) = cell.Offset(0, 1).Value
    Next
Next
Range("A2").Resize(k, 4).Value = arr
End Sub
 
Upvote 0
Solution
I am testing with separate sub. If it worked, try to combine it into your main code:
VBA Code:
Sub splitContract()
Dim lr&, i&, k&, cell As Range, s, arr(1 To 65000, 1 To 4)
lr = Cells(Rows.Count, "C").End(xlUp).Row ' last row of column C
For Each cell In Range("C2:C" & lr)
    s = Split(cell, ",") ' split contract numbers, base on ","
    For i = 0 To UBound(s) ' loop through each contract number of each row
        k = k + 1
        arr(k, 1) = cell.Offset(0, -2).Value
        arr(k, 2) = cell.Offset(0, -1).Value
        arr(k, 3) = s(i)
        arr(k, 4) = cell.Offset(0, 1).Value
    Next
Next
Range("A2").Resize(k, 4).Value = arr
End Sub
Worked it in with existing code and it ran as expected - thank you!
 
Upvote 0

Forum statistics

Threads
1,223,891
Messages
6,175,229
Members
452,621
Latest member
Laura_PinksBTHFT

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