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
这就是我现在所拥有的,有什么建议吗?我已经改变了你的建议并且仍然在水平行中得到第一个条目,而不是各个值乘以的列。我不太了解可能会对问题添加任何建议。
答案 0 :(得分:0)
结果是一个数字变量。您完成循环重置同一变量的值,直到完成为止,然后将此SINGLE值分配给 rng3 ,这将进入所选范围的第一个单元格。
在循环之前,设置一个数组并将其填入循环内:
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