如果我对您的理解正确,并且您不介意更改工作表显示中的“标题”...那么首先您需要复制原始工作簿并在复制的工作簿上测试以下子项。
首先,使工作表的标题显示如下:
每个名称由一列分隔。所以 Mark H 将在 L 列中,依此类推。
分步运行子程序,请不要点击“播放”来运行子程序-因为子程序在选择情况下不完整--->它只定义了DB和PR的oFill。
我没有写完整的子程序,但我希望这个示例子程序可以帮助您入门。
Sub test()
Dim sh1 As Worksheet: Dim sh2 As Worksheet
Dim arr1: Dim arr2: Dim arr3
Dim rg As Range: Dim cell As Range: Dim oFill As Range
Dim x As String: Dim y As String
Dim j As Long: Dim i As Long
'set the worksheet as sh1 and sh2 variable, and set the range of sh1 column A as rg variable
Set sh1 = Sheets("Weekly Input")
Set sh2 = Sheets("Display")
Set rg = sh1.Range("A2", sh1.Range("A" & Rows.Count).End(xlUp))
'this is the loop for 4 week in sheet Weekly Input
'where rg at the first iteration is column A
'2nd iteration is column L, and so on
For j = 1 To 4
'this is the loop to each row of data value in rg (the N/U column)
For Each cell In rg
'join the name, model, reg and date with comma separated into variable x
x = cell.Offset(0, 2).Value & "," & cell.Offset(0, 3).Value & "," & _
cell.Offset(0, 1).Value & "," & cell.Offset(0, 9).Value
'make x value into into array as arr1 variable
arr1 = Split(x, ",")
'join the prds, fin, px and discount with comma separated into variable y
y = cell.Offset(0, 5).Value & "," & cell.Offset(0, 6).Value & "," & _
cell.Offset(0, 7).Value & "," & cell.Offset(0, 8).Value
'make y value into array as arr2 variable
arr2 = Split(y, ",")
'create arr3 variable by joining arr1 and arr2
ReDim arr3(0 To 1, 0 To UBound(arr1))
For i = 0 To UBound(arr3, 2)
arr3(0, i) = arr1(i)
arr3(1, i) = arr2(i)
Next
'check what is the value of the looped row,column S/C
Select Case UCase(cell.Offset(0, 4).Value)
'if the value is DB
Case "DB"
'check, if the looped cell value is u, set the range in sh2 to a blank cell of column B as oFill variable
'other then "u" (meaning "n"), set the range in sh2 to a blank cell of column D as oFill variable
If cell.Value = "u" Then Set oFill = sh2.Range("B" & Rows.Count).End(xlUp).Offset(1, 0) _
Else Set oFill = sh2.Range("D" & Rows.Count).End(xlUp).Offset(1, 0)
'same thing with PR
'add a similar code for MH and MD pointing to the needed range
Case "PR"
If cell.Value = "u" Then Set oFill = sh2.Range("G" & Rows.Count).End(xlUp).Offset(1, 0) _
Else Set oFill = sh2.Range("i" & Rows.Count).End(xlUp).Offset(1, 0)
End Select
'put the arr3 value into oFill
oFill.Resize(4, 2).Value = Application.Transpose(arr3)
'looped to the next row of column N/U in sh2
Next cell
'set the rg for the next iteration of the week
Set rg = rg.Offset(0, 11)
Next j
End Sub