简体   繁体   English

如何将一个Excel工作表中的单元格与另一个Excel工作表中的多个单元格相乘

[英]How to multiply cells in one excel sheet with multiple cells in another excel sheet

I have a workbook with two excel sheets: sheet1 and sheet2. 我有一本带有两个Excel工作表的工作簿:sheet1和sheet2。 In sheet1 I have a large amount of data from column B to CE. 在工作表1中,我从B列到CE都有大量数据。

Using VBA I am currently copying each row from Sheet1 in the range "B3:AP3" to Sheet2 duplicating each line so it will be represented twice. 目前,我使用VBA将Sheet1中“ B3:AP3”范围中的每一行复制到Sheet2中,从而复制了每一行,因此它将代表两次。 This is working fine! 一切正常!

The problem i have as that in Sheet2 I now need to make a calculation. 我在Sheet2中遇到的问题现在需要进行计算。 In Sheet1 I have a number in cell CE and CF for each row. 在Sheet1中,每行CE和CF单元格中都有一个数字。 As these rows have been duplicated I need to multiply the number in cell AP3 of Sheet2 with that of CE3 in Sheet1 and that of AP4 in Sheet2 with that of CF3 in Sheet1. 由于这些行已重复,因此我需要将Sheet2的AP3单元中的数字与Sheet1中的CE3的数目相乘,并将Sheet2中的AP4的数目与Sheet1中的CF3的相乘。

The macro needs to keep on doing this with the subsequent cells. 宏需要继续对后续单元执行此操作。 In total cell AP the first duplicated line needs to be multiplied with the number CE3 from Sheet1 and cell AP of the second duplicated line needs to be multiplied with the number CF3 from sheet1. 在总单元AP中,第一条重复行需要与Sheet1中的数字CE3相乘,而第二条重复行的单元AP需要与Sheet1中的CF3相乘。 The result should be posted in Sheet2 column AQ. 结果应发布在Sheet2列AQ中。

Below I have posted the code for duplicating and moving the lines but I am really struggling with the multiplications, any help is greatly appreciated! 在下面,我发布了用于复制和移动行的代码,但是我真的为乘法而苦苦挣扎,任何帮助都将不胜感激!

Sub CopyRows()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim i As Long, k As Long
Dim ws1LR As Long, ws2LR As Long

Set ws1 = Sheets("Bearbejdning")
Set ws2 = Sheets("Sheet8")

ws1LR = ws1.Range("B" & Rows.Count).End(xlUp).Row + 1
ws2LR = ws2.Range("B" & Rows.Count).End(xlUp).Row + 1

i = 2
k = ws2LR
Do Until i = ws1LR
    With ws1
        .Range(.Cells(i, 1), .Cells(i, 43)).Copy
    End With

    With ws2
        .Cells(k, 1).PasteSpecial
        .Cells(k, 1).Offset(1, 0).PasteSpecial
    End With

    Range("k,AR" & ws2LR).Formula = "=ws1.range(.Cells(i, 73)) * ws2.range(.Cells(k, 36))"

    k = k + 2
    i = i + 1
Loop

End Sub

You can't pass an object into an excel formula that way. 您不能以这种方式将对象传递给excel公式。

Try this: 尝试这个:

Range(k,"AR" & ws2LR).Formula = "=" & ws1.range(.Cells(i, 73)).address &" * " & ws2.range(.Cells(k, 36)).address

Alternatively, you could place the pre-calculated value in the cell. 或者,您可以将预先计算的值放在单元格中。

Range(k,"AR" & ws2LR).Value = ws1.range(.Cells(i, 73)) * ws2.range(.Cells(k, 36))

Also, this Range call looks strange to me: Range("k,AR") . 另外,这个Range调用对我来说看起来很奇怪: Range("k,AR") I think it should be something like Cells(k,"AR") . 我认为应该类似于Cells(k,"AR")

Variables shouldn't be inside quotes and Range doesn't accept Row/Column parameters. 变量不应该在引号内,并且Range不接受行/列参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA用于隐藏一个工作表中的单元格(如果它们匹配另一个工作表中的单元格) - Excel VBA for hiding cells in one sheet if they match cells in another sheet 如何在Excel中将2个单元格与另一个工作表中的另外2个单元格进行比较? - How to compare 2 cells to another 2 cells in a different sheet in excel? Excel VBA,如何使用一个工作表上的下拉列表中的选择格式(颜色)另一工作表上的一系列单元格 - Excel VBA, How to use the selection in a dropdown list on one sheet format(color) a range of cells on another sheet excel片的着色细胞 - coloring cells of excel sheet VBA根据一个excel工作表中的单元格重命名多个工作表,反之,根据excel工作表重命名excel单元格 - VBA rename multiple worksheets based on cells in one excel sheet and reciprocally, rename the excel cells based on excel worksheets Excel宏-根据值将单元格从一张纸复制到另一张纸 - Excel Macro - copy cells from one sheet into another based on value Excel-将单元格+宏+ VBA从一张纸复制到另一张纸? - Excel - copy cells+macro+VBA from one sheet to another? Excel可以将某些单元格从一张纸复制/粘贴到另一张纸上,但要稍加改动 - Excel to copy / paste some cells from one sheet to another but with a twist 将单元格中的单元格复制到多张Excel - VBA中 - copy cells from one sheet into multiple sheets Excel - VBA 使用Python(和DataNitro)将单元格从一个Excel工作簿中的特定工作表复制到另一个Excel工作簿中的特定工作表 - Using Python (and DataNitro) to copy cells from a particular sheet in one Excel workbook, to a particular sheet in another Excel workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM