VBA - Help with Copy/Paste from Source Workbook to Create Multiple Rows in Active Workbook

Luannilow

New Member
Joined
Jan 28, 2018
Messages
2
I'm new to VBA and need some help. I'm looking for most efficient way to create an import file based off of an Excel Spreadsheet.

Source Workbook contains a variable number of rows with 7 columns of data.
In my active workbook, I need to create 5 separate rows for EACH line of data in the Source Workbook.
In the active workbook, the letter or number in column A will always be hard-coded for all 5 rows (see example below).
In the active workbook, the values in the 2nd column & 3-5 rows will also be hard-coded (see example below).

I need to loop through this routine for as many rows that are on the Source data sheet, which varies from month-to-month.

Source Workbook
1A10BB40.0020.0020.00
2A20ED55.0035.0020.00
3C50AA100.0075.0025.00

<tbody>
</tbody>

Active Workbook
01BB
1A
U2940.00
U8120.00
U8220.00
02ED
1A
U2955.00
U8135.00
U8220.00
03AA
1C
U29100.00
U8150.00
U8250.00

<tbody>
</tbody>

Thanks in advance for any help you can throw my way. I tried recording this as a macro but I can't get the macro to keep looping through for each line of data.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Code:
Sub ConverData()


    Dim Arr As Variant
    Dim SrcWb As Workbook
    Dim MyActiveWb As Workbook
    Dim x, y, i As Integer
    
    Set MyActiveWb = ActiveWorkbook
        
    'Copy the path of the source workbook in the area marked with color red
    Set SrcWb = Workbooks.Open("[COLOR=#ff0000]C:\Users\Nishant\Desktop\Source Workbook.xlsm[/COLOR]")
    
    'Check the sheet name in the source workbook and the range. Adjust it according to your need
    Arr = SrcWb.Sheets("[COLOR=#ff0000]Source[/COLOR]").Range("[COLOR=#ff0000]A1:G3[/COLOR]").Value
    
    For i = 1 To UBound(Arr, 1)
        
        For x = 1 To UBound(Arr, 2)
            
            y = y + 1
    'Adjust the name of sheet in the activeworkbook according to your need
            With MyActiveWb.Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")
            
                If x = 1 Then
                    .Cells(y, 1) = 0
                    .Cells(y, 2) = Arr(i, 1)
                    .Cells(y, 3) = Arr(i, 4)
                ElseIf x = 2 Then
                    .Cells(y, 1) = 1
                    .Cells(y, 2) = Arr(i, x)
                ElseIf x = 3 Then
                    .Cells(y, 1) = "U"
                    .Cells(y, 2) = 29
                    .Cells(y, 3) = Arr(i, 5)
                ElseIf x = 4 Then
                    .Cells(y, 1) = "U"
                    .Cells(y, 2) = 81
                    .Cells(y, 3) = Arr(i, 6)
                ElseIf x = 5 Then
                    .Cells(y, 1) = "U"
                    .Cells(y, 2) = 82
                    .Cells(y, 3) = Arr(i, 7)
                End If
                
            End With
            
        Next x
        
        y = y - 2
        
    Next i
    
End Sub
 
Upvote 0
Nishant, WOW! This is awesome. I'd seen the array commands but just couldn't put it all together in my head. Thanks for giving me a great, working example of exactly what I was trying to pull off. Just awesome!!! :)
 
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,608
Members
449,038
Latest member
apwr

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