【问题标题】:How to add several columns to a variable in Visual Basic (Excel)如何在 Visual Basic (Excel) 中将多列添加到变量中
【发布时间】:2015-07-30 12:11:16
【问题描述】:

我在 Visual Basic 中为一个变量分配多个列时遇到了问题。这可能吗?

目前我的代码如下所示:

Do
  Dim data As String
  data = wsh.Cells(Row, 2) 'Data for barcodes is taken from this column

现在我的问题是:如何将更多列分配/存储到数据变量?我在下面尝试的方法是不允许的:

 data= wsh.Cells(Row,2), wshCells(Row, 3), wshCells(Row, 4)  

基本上,我想要做的是获取几个具有一些整数值的列。在这些值中,我想生成 QR 码并用生成的 QR 码填充一些特定的列。使用我拥有的当前方法,我只能选择一列并填充另一列。我无法选择多个列并分别填充多个列。

这是将二维码插入特定列的一种。

Set qrcode_cell = wsh.Cells(Row, 1) 'The cell where the QR Code will be placed

*注意我正在使用一个名为 StrokeScribe 的 Excel 插件。

感谢您的帮助和建议。

【问题讨论】:

  • 你为什么不简单地使用一个数组?
  • 您已将数据声明为字符串。字符串是一段文本。如果您想将每个字符串彼此相邻附加,您可以使用 data = wsh.Cells(Row,2) & wshCells(Row, 3) [但我怀疑这是您想要的]。相反,您需要将数据声明为 Range,它基本上是一组单元格。或者,您可以将数据声明为 Array,这就像手动创建具有 x 维的网格一样,以手动创建自己的数据表。你有这方面的经验吗?

标签: excel vba multiple-columns basic


【解决方案1】:

将您的数据变量转换为列表

dim datas() as string, pos as integer
pos = -1

' Add a column to datas
public sub addColumn(byval row as integer, byval col as integer)
    pos = pos + 1
    redim preserve datas(pos)
    datas(pos) = wsh.Cells(row, col)
end sub

' Convert your datas into the format of your qrcode
public function retrieveData() as string
   retrieveData = ""
   dim data as string
   for each data in datas
      ' data is the string you had in wsh.Cells(Row, col)
      ' do what you want with this column
   next

   ' affect the result to "retrieveData" to retrieve datas in the needed format
end function

【讨论】:

    【解决方案2】:

    只需使用& 将每个单元格值附加到现有字符串:

    Do
      Dim data As String
      data = wsh.Cells(Row,2) & wsh.Cells(Row, 3) & wsh.Cells(Row, 4) 
    

    如果您的意思是每次循环时都想添加一个值,那么类似:

    Do
      Dim data As String
      data = data & wsh.Cells(Row,2), wshCells(Row, 3), wshCells(Row, 4)  
    

    所以你只是将单元格附加到现有字符串上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      相关资源
      最近更新 更多