VBA Code - Create a list on one sheet based on another spreadsheet, multiplying cells based on Values.

BAQI

New Member
Joined
Dec 2, 2022
Messages
40
Office Version
  1. 2016
Platform
  1. Windows
I am looking for VBA code that does the following:

On sheet 1, I input information in columns A-H. In column H, I put the quantity.

On sheet 2, it creates a list, filling out the information in the appropriate columns. A new row of the same information for the quantity in Column H on sheet 1. I would also like it to delete all the information from row 2 on (only leaving the headers) before it does this task, so it only has the latest information from sheet 1.

I attached images of what I mean. Thank you!
 

Attachments

  • 1.png
    1.png
    6.4 KB · Views: 8
  • 2.png
    2.png
    15.2 KB · Views: 7

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try:

VBA Code:
Sub CreateList()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, n As Long, lr As Long
  
  Set sh1 = Sheets("Sheet1")
  Set sh2 = Sheets("Sheet2")
  sh2.Rows("2:" & Rows.Count).ClearContents
  
  lr = sh1.Range("C" & Rows.Count).End(3).Row
  a = sh1.Range("A2:H" & lr).Value
  n = WorksheetFunction.Sum(sh1.Range("H2:H" & lr))
  ReDim b(1 To n, 1 To 7)
  
  For i = 1 To UBound(a, 1)
    For j = 1 To a(i, 8)
      k = k + 1
      b(k, 1) = a(i, 3)
      b(k, 2) = a(i, 2)
      b(k, 3) = a(i, 1)
      b(k, 4) = a(i, 4)
      b(k, 5) = a(i, 6)
      b(k, 6) = a(i, 7)
      b(k, 7) = a(i, 5)
    Next
  Next
  
  sh2.Range("A2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  MsgBox "Finished"
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------
 
Upvote 0
Solution
Try:

VBA Code:
Sub CreateList()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, n As Long, lr As Long
 
  Set sh1 = Sheets("Sheet1")
  Set sh2 = Sheets("Sheet2")
  sh2.Rows("2:" & Rows.Count).ClearContents
 
  lr = sh1.Range("C" & Rows.Count).End(3).Row
  a = sh1.Range("A2:H" & lr).Value
  n = WorksheetFunction.Sum(sh1.Range("H2:H" & lr))
  ReDim b(1 To n, 1 To 7)
 
  For i = 1 To UBound(a, 1)
    For j = 1 To a(i, 8)
      k = k + 1
      b(k, 1) = a(i, 3)
      b(k, 2) = a(i, 2)
      b(k, 3) = a(i, 1)
      b(k, 4) = a(i, 4)
      b(k, 5) = a(i, 6)
      b(k, 6) = a(i, 7)
      b(k, 7) = a(i, 5)
    Next
  Next
 
  sh2.Range("A2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  MsgBox "Finished"
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------
It appears I'm getting an "Subscript out of range" error when I step into this line: ReDim b(1 To n, 1 To 7)
 
Upvote 0
You can put here the data with which you are testing. Use the XL2BB tool, it is assumed that the Qtys are in column H, if they are in another column or column H is empty, then the macro will not work.


1692292868623.png


:cool:
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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