2017-05-31 12 views
0

vbaスクリプトを初めて使用しています。私が必要とするのは、LZFmaxデータを得ることです。このようなデータ形式のために働くExcel VBA:特定の行をtxtファイルから選択した列の範囲に読み込みます。

Public koef_k As Double 

Private Sub Open_Click() 

Dim myFile As Variant, koef_k As Integer 
myFile = Application.GetOpenFilename("Text Files (*.txt),*.txt", , _ 
      "Select file") 
If myFile = False Then Exit Sub 

Open myFile For Input As #1 
row_number = 0 

Do Until EOF(1) 
Line Input #1, LineFromFile 

LineItems = Split(LineFromFile, vbTab) 

Range("C9").Offset(row_number, 0).Value = LineItems(1) 

row_number = row_number + 1 

Loop 

Close #1 

End Sub 


測定は

Band [Hz] 6.3   8.0   10.0  12.5  16.0  20.0  25.0  31.5  40.0  50.0  63.0  80.0  100.0  125.0  160.0  200.0  250.0  315.0  400.0  500.0  630.0  800.0  1000.0  1250.0  1600.0  2000.0  2500.0  3150.0  4000.0  5000.0  6300.0  8000.0  10000.0  12500.0  16000.0  20000.0 

      [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  [dB]  

LZFmax  88.5  81.9  72.8  71.5  70.3  71.0  75.0  69.9  76.5  86.9  93.4  97.9  93.4  86.7  88.8  99.4  98.0  100.8  103.4  97.6  101.4  96.5  93.3  90.2  88.5  91.2  85.2  86.7  80.9  78.4  79.8  80.3  75.8  68.9  66.9  63.9  

LZFmin  20.0  21.4  22.8  20.1  24.6  24.6  28.7  30.5  32.8  35.0  29.0  35.6  34.5  38.0  39.4  39.0  42.5  40.1  41.9  41.7  43.0  39.2  38.5  37.5  36.7  35.4  34.7  34.8  34.6  34.2  34.7  35.7  36.6  37.5  38.9  40.9  

LZeq  61.8  56.8  46.8  46.7  49.1  55.5  49.4  47.5  56.2  69.0  75.3  79.4  75.2  70.7  72.7  76.7  78.8  79.0  79.2  78.6  81.3  78.5  75.2  70.5  70.9  70.0  67.2  68.2  63.6  62.7  57.5  57.4  53.7  51.8  47.8  53.9 

は、私はすでにコードを持っている結果、それが読まれます。このコードを変更する方法

Band[Hz] LZFmax     
50  51     
63  58     
80  60     
100  61     
125  63     
160  65     
200  66     
250  69     
315  73     
400  67     
500  65     
630  62     
800  60     
1000 58     
1250 55     
1600 51     
2000 48     
2500 42     
3150 39     
4000 36 
5000 32 

LZFmax線のみで50〜5000HzのBand [Hz]列を通過しますか?私が好きな

は現在、私は部分的な解決策に

Private Sub Open_Click() 
    Dim fn As Variant, myLine As Long, txt As String, i As Integer, x 
    fn = Application.GetOpenFilename("Text Files (*.txt),*.txt", , _ 
      "Open File") 
    If fn = False Then Exit Sub 
    myLine = 111 '<- change to suite 
    txt = CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll 
    x = Split(txt, vbTab) 
    'MsgBox x(myLine + 1) 
    row_number = 0 
    Range("C9").Offset(row_number, 0).Value = x(myLine - 10) 
    i = 10 
    Do While i < 31 
    Cells(i, "C").Value = x(myLine) 
    i = i + 1 
    myLine = myLine + 1 
    Loop 
Close #1 
End Sub 

を発見したこのコードは動作します、私は手動でそれを見つけるために持っていたので、唯一のマイライン値は、非常に便利ではありません。 このコードを最適化するにはより良い方法がありますか?

答えて

0

私はあなたの生データを入力として取り込み、それを動作させるために少し修正しなければなりませんでした。私はいくつかのことを固定し、このコードになってしまった:

Private Sub Open_Click() 
    Dim fn As Variant, myLine As Long, txt As String 
    Dim i As Integer, x As Variant, y As Variant, z As Variant, c As Variant 
    Dim sht As Worksheet 
    Set sht = Worksheets("Tabelle1") 'EDIT 
    fn = Application.GetOpenFilename("Text Files (*.txt),*.txt", , _ 
      "Open File") 
    If fn = False Then Exit Sub 
    txt = CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll 
    x = Split(txt, vbNewLine) 
    For i = 0 To UBound(x) 
     If Left(x(i), 4) = "Band" Then 
      y = x(i) 
     ElseIf Left(x(i), 6) = "LZFmax" Then 
      z = x(i) 
      Exit For 
     End If 
    Next i 
    y = Replace(y, " ", " ") 
    y = Split(y, " ") 
    z = Replace(z, " ", " ") 
    z = Split(z, " ") 
    c = 2 
    For i = 0 To UBound(y) 
     If y(i) <> "" And y(i) <> "[Hz]" And y(i) <> "Band" Then 
      sht.Cells(c, 3).Value = y(i) 
      c = c + 1 
     End If 
    Next i 
    c = 1 
    For i = 0 To UBound(z) 
     If z(i) <> "" Then 
      sht.Cells(c, 4).Value = z(i) 
      c = c + 1 
     End If 
    Next i 
    sht.Range("C1").Value = "Band [Hz]" 
Close #1 
End Sub 

イムわからない、Excelでデータを配置する計画か、しかし、私はちょうど2列に規模やデータを印刷しました。テキストファイル内で右の行を検索し(各行の最初の文字を切り取って比較)、データを2つの配列に整列します(書式化のために1つではなく、それぞれの長さがあります)。最初の行のヘッダーは2つの要素に分かれていたので、後でそれを無視してヘッダを追加しました。出力は次のとおりです。

enter image description here

+0

ありがとうございました。私には完全にはっきりしないコード部分があります。 'x =分割(txt、vbNewLine) i = 0の場合UBound(x) Left(x(i)、4)=" Band "Then y = x(i) ElseIf Left(x エンドIf' の場合)、6)= "LZFmax" その後 のz = xの(I) 出口は、だから、スプリットでvbLineオプションは、行にファイル全体を分割し、 番号4と6は、バンドとLZFかどうかをチェックするためのものですそれらの行にありますか? – user3434943

+0

はい 'vbNewLine'はtxtファイルを9行に分割し、配列' x'は9要素を含んでいます。今、各行を通って最初の4(6)の文字をカットして、「Band」か「LZFmax」かを調べます。しかし、それはあなたのように言った。他の質問がある場合は、ただ質問してください:) – UGP

0

後はあなたに望ましい結果を与える必要があります。

Sub GetDataFromFile() 
    Dim colIndex As Long 
    Dim LineText As String 
    Dim bandArr, LZFMaxArr, arr 
    Dim fn As Variant 
    'Open "C:\Users\Shiva\Desktop\t1.txt" For Input As #24 
    fn = Application.GetOpenFilename("Text Files (*.txt),*.txt", , _ 
      "Open File") 
    If fn = False Then Exit Sub 
    Open fn For Input As #24 
    colIndex = 1 
    While Not EOF(24) 
     Line Input #24, LineText 
      If colIndex = 1 Then 'condition for Band Column 
       arr = Split(CStr(LineText), " ") 
       bandArr = GetArray(arr) 
      ElseIf colIndex = 5 Then 'condition for LZFMax Column 
       arr = Split(CStr(LineText), " ") 
       LZFMaxArr = GetArray(arr) 
      End If 
      colIndex = colIndex + 1 
    Wend 
    Close #24 

    Dim rIndex As Long 
    rIndex = 2 
    'display headers 
    ActiveSheet.Cells(1, 1).Value = bandArr(1) 
    ActiveSheet.Cells(1, 2).Value = LZFMaxArr(1) 
    'display column value where 50<=Band<=5000 
    For j = 2 To UBound(bandArr) 
     If bandArr(j) >= 50 And bandArr(j) <= 5000 Then 
      ActiveSheet.Cells(rIndex, 1).Value = bandArr(j) 
      ActiveSheet.Cells(rIndex, 2).Value = LZFMaxArr(j) 
      rIndex = rIndex + 1 
     End If 
    Next j 
End Sub 

Private Function GetArray(arr As Variant) 
    Dim destArr(), tempArr() As String 
    Dim rowIndex, index As Long 
    Dim temp As String 
    temp = "" 
    rowIndex = 1 
    For j = 1 To UBound(arr) 
     If Not arr(j - 1) = vbNullString Then 
      'add column values in a atring 
      temp = temp & "," & arr(j - 1) 
     End If 
    Next j 
    tempArr = Split(temp, ",") 

    ReDim destArr(LBound(tempArr) To UBound(tempArr)) 
    For index = LBound(tempArr) To UBound(tempArr) 
     'assign comma separated values to array 
     destArr(index) = tempArr(index) 
    Next index 

    GetArray = destArr 
End Function 

これは上のコードから得られる出力です。

enter image description here

何かが明確でない場合は、私に教えてください。

+0

ありがとうございます。私はこのコードをテストして動作します。しかし、測定装置は測定結果の前にセットアップを記述する余分な行を追加することがあります。 コードのこの部分は、私の必要なデータを調べる責任がありますか? LZFMax COLUMN' – user3434943

+0

ため 'colIndex = 1次に場合は「バンドの列の条件 ARR =スプリット(CSTR(LINETEXT)、"「) bandArr =のgetArray(ARR) のElseIf colIndex = 5次に「条件時にはデータは、次のようになりますこれは: '#ハードウェア構成 \tデバイス情報:\t XL2、SNo。 A2A-09994-E0、FW3.10 \tマイクタイプ:\t NTi Audio M4260、SNo。 5396、工場 \tマイク感度調整:\t 32.0 MV/Paで \tタイムゾーン:\t UTC + 01:00(ヨーロッパ/ベルリン) \tバンド[Hz]の\t 6.3 \t 8.0 \t 10.0 \t 12.5 \t 16.0 \t 20.0 \t 25.0 \t 31.5 \t 40.0 \t 50.0 \t 63.0 \t 80.0 \t 100.0 \t 125.0 \t 160.0 \t 200.0 \t 250.0 \t 315.0 \t 400.0 \t 500.0 \t 630.0 \t 800.0 \t 1000.0 \t 1250.0 \t 1600.0 \t 2000.0 \t 2500.0 \t 3150.0 \t 4000.0 \t 5000.0 \t 6300.0 \t 8000.0 \t 10000.0 \t 12500.0' – user3434943

+0

だから私は何をする必要があるか、セットアップを正しくcolIndex値が正しいですか? – user3434943

関連する問題