VBA to insert a duplicate row based on criteria

Chaozfate

Board Regular
Joined
Mar 15, 2017
Messages
71
Hi,

I wonder if this could be solve by VBA, would be very much appreciate if anyone could help to write a VBA for me if the request is even posssible.

First I have a Sheet 1 as following:
NumberAdditionalDividend
A00010500
B0002100600

I would like the VBA to detect if there the value on Additional column is more than 0, it will generate a report on Sheet 2 as following:
NumberParticularAmount
A0001Dividend500
B0002Dividend600
B0002Additional100

Really hope something can help to resolve this cause i am dealing with 2000 lines or more on Sheet 1.

Thanks!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hello Chaozfate,
I don't know details of the request, but think this code shuld be helpful.
VBA Code:
Option Explicit

Sub NewReport()

    Dim varWS As Worksheet, varWS2 As Worksheet
    Dim varNRows As Long, varNRows2 As Long
    Dim varRange1 As Range, varRange2 As Range
   
    Application.ScreenUpdating = False
    Set varWS = Worksheets("Sheet1")
    Set varWS2 = Worksheets("Sheet2")
    varNRows = varWS.UsedRange.Rows.Count
    Set varRange2 = varWS.Range("B2:B" & varNRows)
    varWS2.Range("A1") = "Number"
    varWS2.Range("B1") = "Particular"
    varWS2.Range("C1") = "Amount"
    
    For Each varRange1 In varRange2
        varNRows2 = varWS2.UsedRange.Rows.Count + 2
        If varRange1.Value > 0 Then
           varWS2.Range("A" & varNRows2 - 1) = varRange1.Offset(-1, -1).Value
           varWS2.Range("B" & varNRows2 - 1) = "Dividend"
           varWS2.Range("C" & varNRows2 - 1) = varRange1.Offset(-1, 1).Value
           varWS2.Range("A" & varNRows2) = varRange1.Offset(0, -1).Value
           varWS2.Range("B" & varNRows2) = "Dividend"
           varWS2.Range("C" & varNRows2) = varRange1.Offset(0, 1).Value
           varWS2.Range("A" & varNRows2 + 1) = varRange1.Offset(0, -1).Value
           varWS2.Range("B" & varNRows2 + 1) = "Additional"
           varWS2.Range("C" & varNRows2 + 1) = varRange1.Value
        End If
    Next
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
The code below might be another solution. Paste it in a separate module (note the line with Option Base 1)

VBA Code:
Option Explicit
Option Base 1       ' <<<  Be sure to have this line on top of the code module

Public Sub Chaozfate()

    Dim rng     As Range
    Dim arrSrc  As Variant
    Dim arrDest As Variant
    Dim j       As Long
    Dim n       As Long

    With ThisWorkbook.Sheets("Sheet1")
        Set rng = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row).Resize(, 3)
    End With
    
    arrSrc = Application.Transpose(rng)
    ReDim arrDest(UBound(arrSrc), 1)
    For j = LBound(arrSrc, 2) To UBound(arrSrc, 2)
        n = n + 1
        ReDim Preserve arrDest(UBound(arrSrc), n)
        arrDest(1, n) = arrSrc(1, j)
        arrDest(2, n) = "Dividend"
        arrDest(3, n) = arrSrc(3, j)
        If arrSrc(2, j) > 0 Then
            n = n + 1
            ReDim Preserve arrDest(UBound(arrSrc), n)
            arrDest(1, n) = arrSrc(1, j)
            arrDest(2, n) = "Additional"
            arrDest(3, n) = arrSrc(2, j)
        End If
    Next j
    arrDest = Application.Transpose(arrDest)

    With ThisWorkbook.Sheets("Sheet2")
        Set rng = .Range("A2").Resize(UBound(arrDest), UBound(arrDest, 2))
    End With
    rng = arrDest

End Sub
 
Upvote 0
Thanks guys, this VBA is beyond my understanding but it works perfectly.

Both VBA function the same, with the difference of code from GWteB overwriting the previous generated result. This save me from creating another clearing code for the previous result.

Appreciate the time from both of you to draft the code, these code definitely helps with my 2000 lines.

Thanks again!
 
Upvote 0
An alternative solution is to employ Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Number"}, "Attribute", "Value"),
    #"Filtered Rows" = Table.SelectRows(#"Unpivoted Other Columns", each ([Value] <> 0))
in
    #"Filtered Rows"

Book5
ABCDEFG
1NumberAdditionalDividendNumberAttributeValue
2A00010500A0001Dividend500
3B0002100600B0002Additional100
4B0002Dividend600
Sheet1
 
Upvote 0
An alternative solution is to employ Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Number"}, "Attribute", "Value"),
    #"Filtered Rows" = Table.SelectRows(#"Unpivoted Other Columns", each ([Value] <> 0))
in
    #"Filtered Rows"

Book5
ABCDEFG
1NumberAdditionalDividendNumberAttributeValue
2A00010500A0001Dividend500
3B0002100600B0002Additional100
4B0002Dividend600
Sheet1
Thanks Alan, unfortunately, this is for office works and we cant install any add on without the IT permission.

Appreciate your respond.
 
Upvote 0
Thanks guys, this VBA is beyond my understanding but it works perfectly.
Both VBA function the same, with the difference of code from GWteB overwriting the previous generated result. This save me from creating another clearing code for the previous result.
Appreciate the time from both of you to draft the code, these code definitely helps with my 2000 lines.
Thanks again!

Glad we could help and thanks for letting us know.

GWteB,
excellent advanced VBA technique.

Thanks. In this case, using this technique is unlikely to cause any problems and typically performs faster than copying individual worksheet ranges.
However, an important side note must be made here. The outcome should never exceed 65,536 rows because that's the amount of rows the used Transpose function can handle without unusual results. With a greater number of rows the returned arrays will be truncated without any warning or run-time error (at least in Excel 2013).
 
Upvote 0
@Chaozfate
What version of Excel are you running. Power Query is part of 2016, 2019 and 365. It is not an addin for those versions. It is called Get and Transform in those versions and found on the Data Tab
 
Upvote 0
I see, my bad.

I am never exposed to power query, did some google and found out it was add on to excel and thought it was accordingly.

Thanks for your feedback, I try do some study into it see what it can offer.
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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