Hello! I don't know what happened, but my macro has stopped working! It just isn't putting data in the right spots, so I will again explain what I am hoping you can help identify the issue for.
I have a large table of data with a lot of data. It is in horizontal format today and it needs to be changed to somewhat vertical formatting. Column A includes a stored figure that needs to be pasted 8 times down (output file column L), as well as the figures from volumes N, P, R, T, V, X, Z, AB (Output file column R) down and values from O, Q, S, U, W, Y, AA, AC (output file column N) beside the value from column A. I also need the value from Column C stored and placed 8 times down in the output file (column Q) , and the value from column D stored and placed 8 times down in the output file (column E). Lastly, I need the value in column L stored and placed 8 times down in the output file (column U). I am also wondering if I could just get a word pasted in every row of column K ("Goods") and M ("Each").
I have gotten quite rusty and I am not sure what happened here.
A "Nice to have" would be that the figure in column O-AC and output into column N could be rounded to only 3 decimal points. Not as a formatting thing, but using a 'round' function so when they are later pasted those numbers don't come back, just stuck at 3 decimal points.
Thanks in advance!
I have a large table of data with a lot of data. It is in horizontal format today and it needs to be changed to somewhat vertical formatting. Column A includes a stored figure that needs to be pasted 8 times down (output file column L), as well as the figures from volumes N, P, R, T, V, X, Z, AB (Output file column R) down and values from O, Q, S, U, W, Y, AA, AC (output file column N) beside the value from column A. I also need the value from Column C stored and placed 8 times down in the output file (column Q) , and the value from column D stored and placed 8 times down in the output file (column E). Lastly, I need the value in column L stored and placed 8 times down in the output file (column U). I am also wondering if I could just get a word pasted in every row of column K ("Goods") and M ("Each").
I have gotten quite rusty and I am not sure what happened here.
A "Nice to have" would be that the figure in column O-AC and output into column N could be rounded to only 3 decimal points. Not as a formatting thing, but using a 'round' function so when they are later pasted those numbers don't come back, just stuck at 3 decimal points.
Thanks in advance!
VBA Code:
Sub Corrgulator_to_BPA()
Dim r As Long, q As Integer, lr As Long
Dim lrData As Long, lcData As Long
Dim NoOfQtyCols As Long
Dim wsData As Worksheet
Dim wsOutput As Worksheet
Dim rngData As Range, rngOut As Range
Dim arrData As Variant, arrOut
Dim colQty1st As Long, colOut1st As Long
Set wsData = ThisWorkbook.Worksheets("Corrgulator")
Set wsOutput = ThisWorkbook.Worksheets("New BPA")
With wsData
lcData = .Cells(1, .Columns.Count).End(xlToLeft).Column
lrData = .Cells(.Rows.Count, "A").End(xlUp).Row
Set rngData = .Range(.Cells(1, 1), .Cells(lrData, lcData))
arrData = rngData.Value2
NoOfQtyCols = 8 ' <--- No of Qty columns (each Qty has an associated Price column)
' J,L,N,P,R,T,V,X ---> Qty
' K,M,O,Q,S,U,W,Y ---> Price
colQty1st = .Columns("J").Column ' <--- Start of Qty, Price is offset 1 to the right
End With
With wsOutput
lr = .Columns("E:R").Find("*", , xlValues, , xlByRows, xlPrevious).Row
If lr = 1 Then lr = 2
.Range("E2:R" & lr).ClearContents
colOut1st = .Columns("E").Column ' <--- Starting column to be used by array
Set rngOut = .Cells(2, colOut1st)
End With
' Column references based on 1 being Column E - colOut1st is the offset for the starting column in the array
Dim outSupp As Long, outItem As Long, outPrice As Long, outStore As Long, outQty As Long, outColU As Long, outGoods As Long
outSupp = Columns("E").Column - colOut1st + 1
outGoods = Columns("K").Column - colOut1st + 1
outItem = Columns("L").Column - colOut1st + 1
outPrice = Columns("N").Column - colOut1st + 1
outStore = Columns("Q").Column - colOut1st + 1
outQty = Columns("R").Column - colOut1st + 1
outColU = Columns("U").Column - colOut1st + 1 ' <--- Ideally use an identifiable variable name for this
ReDim arrOut(1 To lrData * NoOfQtyCols, 1 To outColU)
lr = 0
For r = 2 To UBound(arrData) ' Loop through all items in data sheet
For q = 1 To NoOfQtyCols ' Loop through all quantities
lr = lr + 1 ' Increment row in output sheet
arrOut(lr, outStore) = arrData(r, 1) ' Store - Column A in Source
arrOut(lr, outSupp) = arrData(r, 2) ' Supplier - Column B in Source
arrOut(lr, outItem) = arrData(r, 3) ' Item - Column C in Source
arrOut(lr, outGoods) = arrData(r, 4) ' Unknown - Column D in Source
arrOut(lr, outColU) = arrData(r, 7) ' Unknown - Column G in Source
arrOut(lr, outQty) = arrData(r, colQty1st + (q - 1) * 2) ' Quantity
arrOut(lr, outPrice) = arrData(r, colQty1st + 1 + (q - 1) * 2) ' Price - Column 1 to the right of Qty - in Source
Next q
Next r
rngOut.Resize(lr, UBound(arrOut, 2)).Value2 = arrOut
End Sub