简体   繁体   English

通过像元乘法打印像元输出-VBA Excel

[英]Printing Output of Cell By Cell multiplication - VBA Excel

Sub MVCalc()

On Error Resume Next

Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim rowcount As Integer
Dim result As Long

Verification1:
Set rng1 = Application.Selection
Set rng1 = Application.InputBox("Select Market Prices", "MV Calculator", Type:=8)
If rng1.Columns.Count > 1 Then
MsgBox ("Please select only one column")
Set rng1 = Nothing
GoTo Verification1
End If

Verification2:
Set rng2 = Application.Selection
Set rng2 = Application.InputBox("Select Position/Shares", "MV Calculator", Type:=8)
If rng1.Columns.Count > 1 Then
MsgBox ("Please select only one column")
Set rng2 = Nothing
GoTo Verification2
End If

Verification3:
Set rng3 = Application.Selection
Set rng3 = Application.InputBox("Select output area", "Mv Calculator", Type:=8)
If rng3.Columns.Count > 1 Then
MsgBox ("Please select only one column")
Set rng3 = Nothing
GoTo Verification3
End If


If rng1.Rows.Count < rng2.Rows.Count Then
MsgBox ("Too many Position/Shares entries, please check again")
Set rng1 = Nothing
Set rng2 = Nothing
Set rng3 = Nothing
GoTo Verification1

ElseIf rng1.Rows.Count > rng2.Rows.Count Then
MsgBox ("Not enough Position/Shares entries, please check again")
Set rng1 = Nothing
Set rng2 = Nothing
Set rng3 = Nothing
GoTo Verification1

ElseIf rng3.Rows.Count <> rng1.Rows.Count Then
MsgBox ("Output range doesn't match the number of Market Value or Price entries, please redo")
Set rng1 = Nothing
Set rng2 = Nothing
Set rng3 = Nothing
GoTo Verification1

End If

rowcount = rng1.Rows.Count
Dim table()
ReDim table(1 To rowcount, 0)

For i = 1 To rowcount
table(i, 0) = Val(rng1.Cells(i, 1)) * Val(rng2.Cells(i, 1))
Next i

Dim rng4 As Range
Set rng4 = rng3(1).Resize(1, rowcount)
rng4 = table


End Sub

This is what I have now, any suggestions? 这就是我现在有什么建议? I've changed to what you suggested and still get the first entry in a horizontal row, not columns with the various values multiplied. 我已更改为您的建议,但仍然获得水平行的第一项,而不是各种值相乘的列。 I don't know enough to add any suggestions to what the problem might be. 我还不了解如何对可能出现的问题添加任何建议。

result is a number variable. 结果是一个数字变量。 You go through the loop resetting the value of the same variable until you are done, and then assign this SINGLE value to the rng3 which would go into the first cell of the selected range. 您需要经过循环来重置相同变量的值,直到完成为止,然后将此SINGLE值分配给rng3 ,该值将进入所选范围的第一个单元格。

Before the loop, set up an array and fill it inside the loop: 在循环之前,建立一个数组并将其填充到循环内部:

Dim Array()
Redim Array(1 to rowcount,0) ' make it 2-dimentional so you do not need to rotate it later
For i = 1 To rowcount
    Array(i, 0) = Val(rng1.Cells(i, 1)) * Val(rng2.Cells(i, 1))
Next i
' I do not know what rng3 refers to or it's size. We know where it starts.
Dim rngOut as Range
Set rngOut = rng3(1).Resize(1, rowcount) ' a vertical column one-cell wide
rngOut = Array

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM