【问题标题】:VBA issue - storing data in array from inputboxVBA问题 - 从输入框将数据存储在数组中
【发布时间】:2021-06-27 15:01:30
【问题描述】:

我是 VBA 新手,所以我想将输入框中的值存储到数组中,我编写了下面的代码,然后我尝试定义一个数组来存储它们,我的 VBA 代码有问题,请问帮忙查一下?任何帮助将不胜感激

Sub inpubox()
    Dim sn As String
    Dim em_ID As String
    Dim name As String
    Dim dept As String
    Dim hostname As String
    Dim loc As String
    sn = InputBox("Enter Laptop's Serial")
    em_ID = InputBox("Enter Colleague ID")
    name = InputBox("Enter Colleague Name")
    dept = InputBox("Enter Colleague department")
    loc = InputBox("Enter office location")
    'Xu li hostname
    hostname = "VN" & loc & sn
    sodong = Sheet1.Range("A" & Rows.Count).End(xlUp).Offset(1).row
    Dim arr6 As String
    arr6 =(sn,em_id,name,dept,hostname,loc)
           
End Sub

错误也附上了

【问题讨论】:

  • 尝试 .. arr6 = split(sn&","&em_ID&","&name&","&dept&","&hostname&","&loc, ",") .. 这将为您提供基于 0 的 arr6
  • 您将 arr6 定义为字符串。您需要将其定义为数组
  • Yes.. Dim arr6() As String 会给你字符串类型的数组。

标签: vba


【解决方案1】:
Sub ShortCode()
    arr = Array(InputBox("Enter Laptop's Serial"), InputBox("Enter Colleague ID"), _
          InputBox("Enter Colleague Name"), InputBox("Enter Colleague department"), _
          InputBox("Enter office location"))
    Sheet1.Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(, UBound(arr) + 1) = arr
End Sub

【讨论】:

  • 不错!我喜欢紧凑的代码......投票赞成。
  • .......................超级聪明!
【解决方案2】:

这个直接通过输入框输入到数组中

Option Explicit
Sub inpubox()

Dim arr(1 To 6) As String, sodong as long
    arr(1) = InputBox("Enter Laptop's Serial")
    arr(2) = InputBox("Enter Colleague ID")
    arr(3) = InputBox("Enter Colleague Name")
    arr(4) = InputBox("Enter Colleague department")
    arr(6) = InputBox("Enter office location")
    
    arr(5) = "VN" & arr(6) & arr(1)

sodong = Sheet1.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
Sheet1.Range("A" & sodong).Resize(1, 6).Value = arr

End Sub

只是为了补充来自下面 Алексей Р 的精彩回答 - - - 我们可以使用如下代码来克服 arr(4) 位置(基于 0)连接的限制。

Sub ShortCode()
arr = Array(InputBox("Serial"), InputBox("ID"), InputBox("Name"), _
      InputBox("Department"), "HostName", InputBox("location"))
arr(4) = "VN" & arr(5) & arr(0)
Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Offset(1).Resize(, UBound(arr) + 1) = arr
End Sub

【讨论】:

    【解决方案3】:

    我们需要:

    1. 定义数组
    2. 填充数组
    3. 将数组存储在一行单元格中

    例如:

    Sub inpubox()
        Dim sn As String
        Dim em_ID As String
        Dim name As String
        Dim dept As String
        Dim hostname As String
        Dim loc As String
        sn = InputBox("Enter Laptop's Serial")
        em_ID = InputBox("Enter Colleague ID")
        name = InputBox("Enter Colleague Name")
        dept = InputBox("Enter Colleague department")
        loc = InputBox("Enter office location")
        'Xu li hostname
        hostname = "VN" & loc & sn
        sodong = Sheet1.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
        
        Dim arr(1 To 6) As String
        arr(1) = sn
        arr(2) = em_ID
        arr(3) = name
        arr(4) = dept
        arr(5) = hostname
        arr(6) = loc
        
        Sheet1.Range(Cells(sodong, 1), Cells(sodong, 6)) = arr
               
    End Sub
    

    【讨论】:

    • 甚至不需要创建变量..我们可以直接在数组中输入值arr(1) = InputBox("Enter Laptop's Serial")arr(6) = InputBox("Enter office location")arr(5) = "VN" & arr(6) & arr(1)
    • @Naresh 好评你完全正确!
    • 谢谢大家的帮助 :) 。这里是我学习编程技巧的最佳场所。
    猜你喜欢
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    相关资源
    最近更新 更多