表+错误9(下标超出范围)

时间:2017-03-17 10:48:37

标签: excel vba excel-vba excel-formula

我正在使用表格从文本框中获取数据: textbox的每一行= table_result_final的一行。

我想摆脱textebox中的空白行:

第1行:“”

第2行:“TEXT”

第3行:“TEXT”

第4行:“”

问题是,当我的所有行都不为空时,我会在“Subscript out of range”结尾处收到错误消息。虽然,我的所有数据都已收集在正确的表格中。我想摆脱它,但不明白这个问题:(

感谢您的帮助。

While Table_Result_Final(i) <> ""

Sheets("IDENREG_FORM_TEXTBOX").Cells(j, 1).Value = Table_Result_Final(i)
i = i + 1
j = j + 1

Wend

3 个答案:

答案 0 :(得分:2)

你必须循环遍历数组索引范围:

For i = LBound(Table_Result_Final) To UBound(Table_Result_Final)
    If Table_Result_Final(i) <> "" Then
        j = j + 1
        Sheets("IDENREG_FORM_TEXTBOX").Cells(j, 1).Value = Table_Result_Final(i)
    End If
Next

答案 1 :(得分:0)

我认为你的循环条件应该是:

While Table_Result_Final(i) <> "" And i <= UBound(Table_Result_Final)

答案 2 :(得分:0)

我们假设您有一个名为txtBox的文本框,如下所示:

enter image description here

然后,如果您运行此代码:

Option Explicit

Public Sub RemoveEmptyLines()

    Dim strTextBox          As String

    strTextBox = ActiveSheet.Shapes.Range(Array("txtBox")).TextFrame2.TextRange.Characters.Text

    Debug.Print "Input:"
    Debug.Print strTextBox

    '   We need to make it actually 2 times, just in case if the lines are
    '   odd number.

    strTextBox = Replace(strTextBox, Chr(10) & "" & Chr(10), Chr(10))
    strTextBox = Replace(strTextBox, Chr(10) & "" & Chr(10), Chr(10))
    strTextBox = Replace(strTextBox, Chr(10) & "" & Chr(10), Chr(10))

    Debug.Print "Output:"
    Debug.Print strTextBox


End Sub

您将在即时窗口中看到此内容:

Input:
My name is

tsaka tsaka



Slim Shaddy

....
Output:
My name is
tsaka tsaka
Slim Shaddy
....

这个想法是读取文本框中的文本并将其替换为Chr(10) & "" & Chr(10),它是字符串中空行的chr代码。

相关问题