Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA:如何将包含数据的最后一列与之前的列相乘?_Vba_Excel_Vlookup - Fatal编程技术网

Excel VBA:如何将包含数据的最后一列与之前的列相乘?

Excel VBA:如何将包含数据的最后一列与之前的列相乘?,vba,excel,vlookup,Vba,Excel,Vlookup,我对VBA还是一个新手,正在尝试将我的最后一列(每月更改一次)乘以前一列中的值。(对我来说)困难的是,一旦执行此操作,我需要添加另一列并乘以前一列乘以的同一列。例如:我的代码在下一个可用列中执行vlookup,假设这是列R。然后我需要将列R中的值乘以列Q中的值(直到列R中的最后一行)。然后我需要在S列中执行一个新的vlookup,但仍然要将它乘以Q中的值。同样,这些列每月都会更改。这是到目前为止我的代码,我已经设法找出了所有的vlookups和所有东西,只是在将我的列与前一列相乘时遇到了麻烦:

我对VBA还是一个新手,正在尝试将我的最后一列(每月更改一次)乘以前一列中的值。(对我来说)困难的是,一旦执行此操作,我需要添加另一列并乘以前一列乘以的同一列。例如:我的代码在下一个可用列中执行vlookup,假设这是列R。然后我需要将列R中的值乘以列Q中的值(直到列R中的最后一行)。然后我需要在S列中执行一个新的vlookup,但仍然要将它乘以Q中的值。同样,这些列每月都会更改。这是到目前为止我的代码,我已经设法找出了所有的vlookups和所有东西,只是在将我的列与前一列相乘时遇到了麻烦:

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

使用此选项乘以行的值:

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
outputSheet.Cells(4,1).激活
Application.ScreenUpdating=False
而ActiveCell.Value为“”
ActiveCell.Offset(1,0).激活
温德
而ActiveCell.Offset(0,-1).Value“”
ActiveCell.Value=ActiveCell.Offset(0,-2).Value*ActiveCell.Offset(0,
-1) .价值
ActiveCell.Offset(1,0).激活
温德
Application.ScreenUpdating=True