[英]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.