0
我对VBA仍然很陌生,我试图将最后一列(每月更改一次)乘以列之前的值。这对我来说很困难的是,一旦执行了这个操作,我需要添加另一列并乘以前一个乘以的列。例如:我的代码在下一个可用列中执行vlookup,让我们说这是列R.然后我需要将列R中的值乘以列Q中的值(直到列R中的最后一行)。然后,我需要在列S中执行新的查找,但仍然将它乘以Q中的值。同样,列每月都会更改。这里是我的代码,到目前为止,我已设法找出所有vlookups和一切,只是有麻烦我的专栏与前一列相乘:Excel VBA:如何将包含数据的最后一列与列之前相乘?
Sub vlookup5()
Dim SourceLastRow As Long
Dim OutputLastRow As Long
Dim sourceSheet As Worksheet
Dim outputSheet As Worksheet
Dim NextColumn As Long
Set sourceSheet = Worksheets("Data1")
Set outputSheet = Worksheets("Pivot")
With sourceSheet
SourceLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With outputSheet
NextColumn = .Cells(4, Columns.Count).End(xlToLeft).Column + 1
OutputLastRow = .Cells(.Rows.Count, "D").End(xlUp).Row - 1
.Range(Cells(4, NextColumn), Cells(OutputLastRow, NextColumn)).Formula = _
"=VLOOKUP(D4,'" & sourceSheet.Name & "'!$A$2:$H$" & SourceLastRow & ",6,0)*LastColumn"
.Cells(OutputLastRow + 1, NextColumn).Formula = "=SUM(" & Chr(64 + NextColumn) & "4:" & Chr(64 + NextColumn) & OutputLastRow & ")*1000"
End With
End Sub
Sub vlookup6()
Dim SourceLastRow As Long
Dim OutputLastRow As Long
Dim sourceSheet As Worksheet
Dim outputSheet As Worksheet
Dim NextColumn As Long
Set sourceSheet = Worksheets("Data1")
Set outputSheet = Worksheets("Pivot")
With sourceSheet
SourceLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With outputSheet
NextColumn = .Cells(4, Columns.Count).End(xlToLeft).Column + 1
OutputLastRow = .Cells(.Rows.Count, "D").End(xlUp).Row - 1
.Range(Cells(4, NextColumn), Cells(OutputLastRow, NextColumn)).Formula = _
"=VLOOKUP(D4,'" & sourceSheet.Name & "'!$A$2:$H$" & SourceLastRow & ",7,0)"
.Cells(OutputLastRow + 1, NextColumn).Formula = "=SUM(" & Chr(64 + NextColumn) & "4:" & Chr(64 + NextColumn) & OutputLastRow & ")*1000"
End With
End Sub