2016-04-15 43 views
0

大量のテキストを生成してテキストボックスに格納するユーザーフォームがあります。Excel VBA TextBoxへの追加が遅い

Sub AddLineToSQL(sLine As String) 

    frmSQL.txtSQL.Value = frmSQL.txtSQL.Value & sLine & vbCr 

End Sub 

それが処理に時間がかかり、テキストの数百行を追加する(最大20秒):

は、私は、テキストボックスにテキストの次の行を追加するには、以下の機能を持っています。

この問題は、1000行以上のテキストを追加する可能性があることです。

私たちは基本的に同じことをする古いフォームを持っていますが、よりクリーンなユーザーエクスペリエンスを作成しようとしています。古いフォームはテキストをワークシートに書き込んだので、テキストボックスに追加するよりもはるかに迅速に動作するようです。

上記のものよりもテキストボックスにテキストを追加する方が効率的ですか?

私は古いフォームの作業を行い、ワークシートに行を書き込む必要がありますか?

おかげで、

マーク

答えて

0

は、テキストボックスに行ずつ追加しないでください。その代わりに、Stringをすべての行と連結し、そのStringをTextBox値として設定します。

Sub test() 
Dim sTxtSQL As String 

For i = 1 To 5000 
    sTxtSQL = sTxtSQL & "This is row " & i & vbCrLf 
Next 

frmSQL.txtSQL.Value = sTxtSQL 
frmSQL.Show 
End Sub 
+0

ありがとうございます。 –

0

テキストのあなたの量がveeery大きくなければならない、あなたは、このクラスを使用することができます。

' Class: StringBuilder 
' from http://stackoverflow.com/questions/1070863/hidden-features-of-vba 
Option Explicit 

Private Const initialLength As Long = 32 

Private totalLength As Long ' Length of the buffer 
Private curLength As Long ' Length of the string value within the buffer 
Private buffer As String  ' The buffer 

Private Sub Class_Initialize() 
    ' We set the buffer up to it's initial size and the string value "" 
    totalLength = initialLength 
    buffer = Space(totalLength) 
    curLength = 0 
End Sub 

Public Sub Append(Text As String) 

    Dim incLen As Long ' The length that the value will be increased by 
    Dim newLen As Long ' The length of the value after being appended 
    incLen = Len(Text) 
    newLen = curLength + incLen 

    ' Will the new value fit in the remaining free space within the current buffer 
    If newLen <= totalLength Then 
    ' Buffer has room so just insert the new value 
    Mid(buffer, curLength + 1, incLen) = Text 
    Else 
    ' Buffer does not have enough room so 
    ' first calculate the new buffer size by doubling until its big enough 
    ' then build the new buffer 
    While totalLength < newLen 
     totalLength = totalLength + totalLength 
    Wend 
    buffer = Left(buffer, curLength) & Text & Space(totalLength - newLen) 
    End If 
    curLength = newLen 
End Sub 

Public Property Get Length() As Integer 
    Length = curLength 
End Property 

Public Property Get Text() As String 
    Text = Left(buffer, curLength) 
End Property 

Public Sub Clear() 
    totalLength = initialLength 
    buffer = Space(totalLength) 
    curLength = 0 
End Sub 

だけのことができます。その後、任意のクラスモジュールに配置し、「StringBuilderの」後

をそれに名前を付けますAxelの答えと同様にそれをテストしてください:

Sub test() 

Dim i As Long 
Dim sb As StringBuilder 
Dim sTxtSQL As String 

Dim timeCount As Long 

timeCount = Timer 

Set sb = New StringBuilder 
For i = 1 To 50000 
    sb.Append "This is row " & CStr(i) & vbCrLf 
Next i 
sTxtSQL = sb.Text 

MsgBox Timer - timeCount 

frmSQL.txtSQL.Value = sTxtSQL 
frmSQL.Show 

End Sub 

私のテストでは、 r "i"が50kを超えるループ

+0

ありがとう、私はテキスト文字列が "それ"大きな得るとは思わないが、私はいくつかのより多くのテストを行い、必要に応じてあなたの優雅なソリューションを実装します。 –

関連する問題