VBA to insert row and data on multiple sheets

grays

Board Regular
Joined
Nov 5, 2013
Messages
71
worksheet1 has 3 columns of data which are duplicated in multiple worksheets. I am looking to add code that would allow a user to insert a row and add data on worksheet1 while simultaneously doing the same thing on the other worksheets.

There are 3 things to note.
1- the data from worksheet1 is repeated 4 times on the other worksheets (segregated by Total & 3 locations). The data on theses worksheets also has a Total Row and and blank row between the 4 sections
2 - every worksheet will have worksheet1 column 1 data, but some will have column 2 data and the others will have column 3 data.

at a minimum what code would help with inserting the rows on each section of the worksheets assuming they each had one section that matched worksheet1?

also does anyone have suggestions on simplifying what I'm trying to do?

Thanks
 
you have I defined;)

Code:
    For Each myCell In Rng
        ReDim Preserve arr(i)
        arr(i) = myCell
        i = i + 1
    Next myCell

so after loop will finish you will know what is your Array length.

try this code:

Code:
Sub rangearray()
    Dim arr As Variant
    Dim i As Long
    Dim LastRow As Long

    LastRow = labor_ODClist.Rows(.Rows.Count).Row
    
    arr = labor_ODClist.Range(Cells(4, 1), Cells(LastRow, 1))
    
    i = LastRow - 4 + 1
    
    LaborDetail.Range(Cells(4, 1), Cells(LastRow, 1)).Value = WorksheetFunction.Transpose(arr)
    
    LaborDetail.Range(Cells(LastRow + 4, 1), Cells(LastRow + 4, 1 + i - 1)).Value = WorksheetFunction.Transpose(arr)

End Sub
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Thanks so much for all of your help! I figured out that was having issues becuase it was going to the end of the sheet instead of the last row with data (i was too large). I was close to getting there when you responded. below is my final code.

Code:
Sub rangearray()
    Dim arr() As Variant
    Dim Rng As Range
    Dim myCell As Range
    Dim i As Long
    Dim LastRow As Long
    
    With labor_ODClist
        LastRow = .Range("A4").End(xlDown).Row
    End With
    Set Rng = labor_ODClist.Range("A4:A" & LastRow)
       
    For Each myCell In Rng
        ReDim Preserve arr(i)
        arr(i) = myCell
        i = i + 1
    Next myCell
    LaborDetail.Range(Cells(4, 3), Cells(LastRow, 3)).Value = WorksheetFunction.Transpose(arr)
    LaborDetail.Range(Cells(LastRow + 4, 3), Cells(LastRow + LastRow, 3)).Value = WorksheetFunction.Transpose(arr)
End Sub
very excited about this...
 
Upvote 0
Please check this code. It should do same think but without loop. And it looks like you dont need Transpose here

Code:
Sub rangearray()

    Dim arr As Variant
    Dim LastRow As Long

     LastRow = labor_ODClist.Cells(4, 1).End(xlDown).Row
    
    arr = labor_ODClist.Range(Cells(4, 1), Cells(LastRow, 1))
    
    LaborDetail.Range(Cells(4, 3), Cells(LastRow, 3)).Value = arr
    LaborDetail.Range(Cells(LastRow + 4, 3), Cells(LastRow + LastRow+4-1, 3)).Value = arr

End Sub
 
Last edited:
Upvote 0
Code:
Rng = labor_ODClist.Range("A4:A" & LastRow)
the range you have is Vertical and same is an Array (Arr)

so if you would Transpose it as in your code you will have first cell value in whole range
Code:
LaborDetail.Range(Cells(4, 3), Cells(LastRow, 3)).Value

if you want to transpose it then
Code:
 LaborDetail.Range(Cells(LastRow + 4, 3), Cells(LastRow + 4, 3 + LastRow - 1)).Value = WorksheetFunction.Transpose(arr)
 
Last edited:
Upvote 0
thanks if I wanted to add a loop where the array is used the same way on more than just the LaborDetail worksheet, what do you think is the best approach? A for each Loop with a Select Case scenario?
 
Upvote 0
Thanks. Instead of looping, could I create another array that contained all of the worksheets where I wanted the data to "paste"? It would be a 1 dimensional static array. However, I'm not sure how to fit that into the code we've already discussed.
 
Upvote 0
I tried this code. I can see where it highlights all of the right sheets, but the data only shows up on the first sheet.
Code:
 With Sheets(Array(LaborDetail.Name, Sheet3.Name, Sheet5.Name, Sheet6.Name, Sheet8.Name, Sheet9.Name)).Select
        Range(Cells(4, 3), Cells(LastRow, 3)).Value = WorksheetFunction.Transpose(arr)
        ...

suggestions?
 
Upvote 0

Forum statistics

Threads
1,216,332
Messages
6,130,082
Members
449,556
Latest member
Sergiusz

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