2016-03-31 12 views
1

私のサブルーチンは個々のシートで実行するとうまく動作しますが、個別のシートごとに実行するには多くの問題がありました。サブルーチンはオンラインのCSVデータベースの簡単なクエリですが、最初のシートでは25回しか実行されません。なぜこれが私の人生のために把握することはできません。Excel VBAのトラブルシューティングシートと呼び出しサブルーチン

私はこの同じループで計算を実行できましたが、各シート上でサブルーチンを実行することができませんでした。

Sub Datacollection() 

    Dim ws As Worksheet 
    For Each ws In Worksheets 

    ws.Application.Run "Gethistory" 

    Next ws 
End Sub 


Sub Gethistory() 
Dim Target As Variant 
Dim Name As Variant 
' 
Set Target = Range("B1") 
Set Name = Range("B2") 

    With ActiveSheet.QueryTables.Add(Connection:= _ 
    "Text;" & Target, _ 
    Destination:=Range("$A$3")) 
    .Name = Name 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    .PreserveFormatting = True 
    .RefreshOnFileOpen = False 
    .RefreshStyle = xlOverwriteCells 
    .SavePassword = False 
    .SaveData = True 
    .AdjustColumnWidth = True 
    .RefreshPeriod = 0 
    .TextFilePromptOnRefresh = False 
    .TextFilePlatform = 437 
    .TextFileStartRow = 1 
    .TextFileParseType = xlDelimited 
    .TextFileTextQualifier = xlTextQualifierDoubleQuote 
    .TextFileConsecutiveDelimiter = True 
    .TextFileTabDelimiter = True 
    .TextFileSemicolonDelimiter = False 
    .TextFileCommaDelimiter = True 
    .TextFileSpaceDelimiter = True 
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1) 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=False 
End With 

End Sub 

答えて

2

は、ワークシートがあなたの主なループで処理し、パラメータとしてgetHistoryサブにそれを渡すことを収集します。

Option Explicit 

Sub dataCollection() 
    Dim w As Long 
    For w = 1 To Worksheets.Count 
     getHistory Worksheets(w) 
    Next w 
End Sub 


Sub getHistory(ws As Worksheet) 
    Dim trgt As Range, nm As Range 

    With ws 
     Set trgt = .Range("B1") 
     Set nm = .Range("B2") 

     With .QueryTables.Add(Connection:= _ 
      "Text;" & trgt.Value, _ 
      Destination:=.Range("$A$3")) 
      .Name = nm.Value 
      .FieldNames = True 
      .RowNumbers = False 
      .FillAdjacentFormulas = False 
      .PreserveFormatting = True 
      .RefreshOnFileOpen = False 
      .RefreshStyle = xlOverwriteCells 
      .SavePassword = False 
      .SaveData = True 
      .AdjustColumnWidth = True 
      .RefreshPeriod = 0 
      .TextFilePromptOnRefresh = False 
      .TextFilePlatform = 437 
      .TextFileStartRow = 1 
      .TextFileParseType = xlDelimited 
      .TextFileTextQualifier = xlTextQualifierDoubleQuote 
      .TextFileConsecutiveDelimiter = True 
      .TextFileTabDelimiter = True 
      .TextFileSemicolonDelimiter = False 
      .TextFileCommaDelimiter = True 
      .TextFileSpaceDelimiter = True 
      .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1) 
      .TextFileTrailingMinusNumbers = True 
      .Refresh BackgroundQuery:=False 
     End With 
    End With 

End Sub 

あなたが繰り返しこの操作を行う場合は、一般的なワークブック効率だけでなく、将来getHistory実行に干渉する可能性が接続の多くで終わるだろう。接続を作成するときに接続を削除するか、またはリフレッシュ方法のみを使用してデータを維持することができます。

関連する問題