【发布时间】:2016-10-04 13:09:11
【问题描述】:
我需要一个电子表格来提示用户输入以下内容:
他们测试的次数
启动电压
电路中的电阻数
电阻之间的距离
每个电阻的阻值
我需要输出以下内容:
- 等效电阻
- 当前理论
- 每个停止字符串的电压
- 根据输入值响应
我已经设法让用户输入起作用。我相信我的问题是在循环中设置一个数组,这样我就可以将所有数据的总和用于等效电阻之类的东西。
我需要一种方法来执行功能,例如在记录某些输入值组后对其求和。我认为这是一个数组,但我仍在自学 VBA。任何帮助表示赞赏!
Private Sub CommandButton21_Click()
TrialRuns = InputBox("How many separate voltage sources have you tested?")
Dim x As Integer
For x = 1 To TrialRuns
TrialName = InputBox("Please input the name or some other type of identifying quality (ie. Level 1 load side)")
Rows("2").Insert Shift:=x1ShiftDown, CopyOrigin:=x1FormatFromRightorBelow
StartingVoltage = "What is the starting voltage?"
Msg = "How many resistors/loads do you have?"
VoltageInput = InputBox(StartingVoltage)
ActiveSheet.Range("A2").Value = TrialName
ActiveSheet.Range("B2").Value = VoltageInput
QtyEntry = InputBox(Msg)
ActiveSheet.Range("C2").Value = QtyEntry
Dim i As Integer
For i = 1 To Range("C2").Value
Rows("3").Insert Shift:=xlShiftDown, CopyOrigin:=xlFormatFromRightOrBelow
ResistanceValue = InputBox("Hey dude, what is the resistance value in ohms?")
RunLength = InputBox("Cool! How far is it (in feet) from the previous load? Use distance from voltage source if this is the first load.")
ActiveSheet.Range("G3").Value = RunLength
ActiveSheet.Range("D3").Value = ResistanceValue
ActiveSheet.Range("E3").Value = 1 / ResistanceValue
Next i
Next x
End Sub
【问题讨论】:
-
如果你正在学习 VBA,我强烈推荐使用
Option Explicit。您正在使用各种未声明的变量。另外,请尝试一致地缩进代码。您发布的内容很难阅读。 -
使用一系列输入框将值加载到电子表格中的循环对大多数用户来说很烦人(重复的“嘿伙计”不太可能减轻这种烦恼)。为什么不让用户直接将值输入电子表格,然后让 VBA 代码读取它们(可能在用户选择要处理的值之后)?
-
嗨,John,我将如何让 VBA 代码读取它们?
标签: arrays vba loops user-input