用于'写行的动态行和静态列数组'环

时间:2015-10-21 08:32:21

标签: arrays excel vba excel-vba

我需要为“写入行”生成一个数组'自定义功能。此函数在循环期间将单行单元格(数组)写入文本文件 这个数组的大小是静态的 要写入文本文件的行被“检测到”'通过下面的Rng变量:

Set Rng = Worksheets("Sheet1").Columns(5).Find(userform1.ComboBox1.Value)    

即。 Rng变量仅索引第5列中的单个单元格 - 与ComboBox1中的值匹配。 writeLine写入的数组将是Rng匹配的行的第6:49列。

'写行的参数'功能是

  1. 组合框的值,即userform1.combobox1.value
  2. 要写入'写入行的数组'功能
  3. 到目前为止,我有这个:

    Private Sub CommandButton18_Click()
    
    Dim Range As Range
    Dim Array As Range
    
    Set Range =      Worksheets("Sheet1").Columns(5).Find(userform1.ComboBox1.Value) ''anticipated to set the row index of the array???
    
    Array = ????
    
    Function.writeLine(userform1.ComboBox1.Value,????)
    
    End Sub
    

    我遇到的麻烦是创建一个包含更改行的数组,同时修复列数?

    以下是writeLine函数的一部分:

    Do Until objTF.AtEndOfStream
        readString = objTF.readline
        data = Split(readString, vbTab)
        foundID = data(0)
        If StrComp(**foundID, ID**, 1) <> 0 Then
            objTF2.writeLine (readString)
        ElseIf StrComp(**foundID, ID**, 1) = 0 Then
            'write initial value outside the loop
            strTmp = Split(readString, strDelim)
            'Modify the data array to include the data provided by writeArray
            For argPos = 5 To UBound(data)
                'check for index out of bounds, stop writing if it is!
                If (argPos - 5) > UBound(writeArray) Then Exit For 'need to check this will exit if the value is index out of bounds.
                data(argPos) = writeArray(dataPos)
                dataPos = dataPos + 1
            Next argPos
            'Take each entry from data and build a string delimited by strDelim
            Do Until counter > UBound(data)
                resultStr = IIf(counter <= UBound(data), resultStr & data(counter) & strDelim, resultStr & data(counter))
                counter = counter + 1
            Loop
            'output to temp file
            objTF2.writeLine (resultStr)...
    

    基本上,&#34; foundID&#34;和&#34; ID&#34;是要匹配的变量,以便将数组写入文本文件。 &#34; ID&#34;是ComboBox1.value。 &#34; foundID&#34;是电子表格第5列中的值。第6:49列(数组)将写入文本文件。

1 个答案:

答案 0 :(得分:0)

使用Ubound将范围作为数组获取。

Private Sub CommandButton18_Click()

Dim rRange As Range
Dim colno As Long
Dim rownumber As Long
Dim sString As String

    lastrow = 10

For colno = 6 To 43
   For rownumber = 1 To lastrow
       sString = Worksheets("sheet1").Cells(rownumber, colno)
       Next rownumber
    Next

End Sub