我仍然是VBA的新手,并且我试图将我的最后一列(每月更改一次)乘以该列之前的列中的值。这对我来说很困难的是,一旦执行了这个,我需要添加另一列并乘以前一个列乘以的相同列。例如:我的代码在下一个可用列中执行vlookup,假设是列R.然后我需要将列R中的值乘以列Q中的值(直到列R中的最后一行)。然后我需要在S列中执行一个新的vlookup,但仍然将它乘以Q中的值。再次,列每月更改一次。到目前为止,这是我的代码,我已经设法找出所有的vlookup和所有内容,只是在将我的列与前一列相乘时遇到了麻烦:
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
答案 0 :(得分:0)
使用它来乘以行的值:
outputSheet.Cells(4,1).Activate
Application.ScreenUpdating = False
While ActiveCell.Value <> ""
ActiveCell.Offset(1, 0).Activate
Wend
While ActiveCell.Offset(0, -1).Value <> ""
ActiveCell.Value = ActiveCell.Offset(0, -2).Value * ActiveCell.Offset(0,
-1).Value
ActiveCell.Offset(1, 0).Activate
Wend
Application.ScreenUpdating = True