VBA copy row multiple times based on cell value and change cell value in copied rows

secretsqrl

New Member
Joined
Aug 15, 2008
Messages
7
Hello,
I'm a VBA novice and normally can cobble something together based upon some of the fantastic work on this forum, but I have a complex issue that has multiple elements that I'm having a difficult time getting my head around.

I have a master worksheet that has 200 rows and looking for a script that can do the following:
1. Evaluate each row and copy the row x number of times into a new sheet based upon the information in Location column
a. If the Location is a single city then only copy the row once
b. If the Location is Global then the row needs to be copied 11 times representing all locations changing the location from Global to each of the 11 cities
2. When the location is Global, once all 11 rows have been copied and changed to each of the locations I also need to split the total cost(Column C) based on a percentage for each location. it will total 100% but will be different for each location and need the ability to change the values in the script as it could change over time.

Simple example: Master Spreadsheet
1644213307679.png


New worksheet with copied rows and changed values.
1644213837881.png


Any assitance would be greaty appreciated.
 

Attachments

  • 1644212758935.png
    1644212758935.png
    6.9 KB · Views: 56

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try this:

VBA Code:
Sub CopyRows()
  Dim shR As Worksheet
  Dim i As Long
  Dim c As Range
  Dim x As Double
  
  Set shR = Sheets("Result")
  With Sheets("Master")
    For i = 2 To .Range("A" & Rows.Count).End(3).Row
      If .Range("D" & i).Value = "Global" Then
        For Each c In .Range("G2:G12")
          x = .Range("C" & i).Value * c.Offset(, 1).Value
          shR.Range("A" & Rows.Count).End(3)(2).Resize(1, 4).Value = Array(.Range("A" & i), .Range("B" & i), x, c)
        Next
      Else
        shR.Range("A" & Rows.Count).End(3)(2).Resize(1, 4).Value = .Range("A" & i).Resize(1, 4).Value
      End If
    Next
  End With
End Sub
 
Upvote 0
Solution
Try this:

VBA Code:
Sub CopyRows()
  Dim shR As Worksheet
  Dim i As Long
  Dim c As Range
  Dim x As Double
 
  Set shR = Sheets("Result")
  With Sheets("Master")
    For i = 2 To .Range("A" & Rows.Count).End(3).Row
      If .Range("D" & i).Value = "Global" Then
        For Each c In .Range("G2:G12")
          x = .Range("C" & i).Value * c.Offset(, 1).Value
          shR.Range("A" & Rows.Count).End(3)(2).Resize(1, 4).Value = Array(.Range("A" & i), .Range("B" & i), x, c)
        Next
      Else
        shR.Range("A" & Rows.Count).End(3)(2).Resize(1, 4).Value = .Range("A" & i).Resize(1, 4).Value
      End If
    Next
  End With
End Sub

WOW! WOW! WOW!
Many thanks @DanteAmor ...this is exactly what I was after. I've scaled it to the actual size of my worksheet and works like a charm

Can't thank you enough as you've saved me lots of work!!!!!
 
Upvote 0
Im glad to help you. Thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,643
Members
449,093
Latest member
Ahmad123098

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