我正在尝试构建一个循环,该循环将从两个单独的工作表中提取数据并将它们放入一个表中。我试图让它将跨行的数据放置到最后,然后向下移动一行并重复直到它到达表的末尾。和列 IChanges
和WTD
将在事后计算。我只是想将数据放入其中。这是我拥有的编码 This Year
。Last Year
Sub MakeTable()
Application.ScreenUpdating = False
Dim gRange As Range
Dim i As Integer
Dim j As Integer
Dim baseSheet As Worksheet
Dim from1Sheet As Worksheet
Dim from2Sheet As Worksheet
Dim baseBaseCell As Range
Dim baseFrom1Cell As Range
Dim baseFrom2Cell As Range
Set baseSheet = Sheets("Chart")
Set from1Sheet = Sheets("Week")
Set from2Sheet = Sheets("WeekMinusYear")
Set baseBaseCell = Sheets("Chart").Range("B3")
Set baseFrom1Cell = Sheets("Week").Range("C2")
Set baseFrom2Cell = Sheets("WeekMinusYear").Range("C2")
For i = 0 To 4
For j = 0 To 12
If i = ((4 Mod 2) <> 0) Then
setRawData1
Else
setRawData2
End If
Next j
Next i
End Sub
Function setRawData1()
Dim baseSheet As Worksheet
Dim from1Sheet As Worksheet
Dim from2Sheet As Worksheet
Dim baseBaseCell As Range
Dim baseFrom1Cell As Range
Dim baseFrom2Cell As Range
Set baseSheet = Sheets("Chart")
Set from1Sheet = Sheets("Week")
Set from2Sheet = Sheets("WeekMinusYear")
Set baseBaseCell = Sheets("Chart").Range("B3")
Set baseFrom1Cell = Sheets("Week").Range("C2")
Set baseFrom2Cell = Sheets("WeekMinusYear").Range("C2")
'Sales
baseBaseCell.Offset(0, 0) = baseFrom1Cell.Value 'This Year
'Cost
baseBaseCell.Offset(0, 3) = baseFrom1Cell.Offset(0, 1) 'This Year
'Margin
baseBaseCell.Offset(0, 6) = baseFrom1Cell.Offset(0, 2) 'This Year
'Basis Points
baseBaseCell.Offset(0, 9) = baseFrom1Cell.Offset(0, 3) 'This Year
End Function
Function setRawData2()
Dim baseSheet As Worksheet
Dim from1Sheet As Worksheet
Dim from2Sheet As Worksheet
Dim baseBaseCell As Range
Dim baseFrom1Cell As Range
Dim baseFrom2Cell As Range
Set baseSheet = Sheets("Chart")
Set from1Sheet = Sheets("Week")
Set from2Sheet = Sheets("WeekMinusYear")
Set baseBaseCell = Sheets("Chart").Range("B3")
Set baseFrom1Cell = Sheets("Week").Range("C2")
Set baseFrom2Cell = Sheets("WeekMinusYear").Range("C2")
'Sales
baseBaseCell.Offset(0, 1) = baseFrom2Cell.Value 'Last Year
'Cost
baseBaseCell.Offset(0, 4) = baseFrom2Cell.Offset(0, 1) 'Last Year
'Margin
baseBaseCell.Offset(0, 7) = baseFrom2Cell.Offset(0, 2) 'Last Year
'Basis Points
baseBaseCell.Offset(0, 10) = baseFrom2Cell.Offset(0, 3) 'Last Year
End Function
这是我拥有的表格的示例。
对不起,图像质量差。所有列都按应有的方式分解。我已经尝试了几种不同的方法来实现这一点。就像现在一样,它将通过并将去年的信息放入正确的列中,但仅限于第一行。我能得到的任何帮助将不胜感激!