【问题标题】:Should be simple, array based on user input with a loop应该是简单的,基于用户输入的数组,带有循环
【发布时间】: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


【解决方案1】:

我不完全确定我了解您要查找的内容,但您应该能够在不使用数组的情况下获得每个组的总和。

在此代码块中添加以下内容:

Dim i As Integer
Dim runTotal As Double
Dim resistTotal As Double

runTotal = 0
resistTotal = 0

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

    runTotal = runTotal + RunLength
    resistTotal = resistTotal + ResistanceValue

    If i = Range("C2").Value Then
        ActiveSheet.Range("G4").Value = runTotal
        ActiveSheet.Range("D4").Value = runTotal
    End If

Next i

如果这不能解决您的问题并且您想使用数组,您可以添加类似于以下内容的内容:

Dim runArr() As Double
ReDim runArr(1 To QtyEntry)

For i = 1 To Range("C2").Value
    '...
    runArr(i) = RunLength
Next i

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    相关资源
    最近更新 更多