VBAスクリプトを使用して、別のワークシートのインスタンス値の順序でテキストセクションを書きたいと考えています。私は、ワークシートAにテキストのチャンクを書き込むコードの部分を持っているVBAはセルの値に応じて行を反復する
Dim selectedTest As String
Dim activeCell As Range
Dim outputCell As Range
Dim currentValue As String
Dim activePage As String
Dim row As String
Dim instancecol As String
selectedTest = template.Range("I6, I6").value
Set activeCell = template.Cells.Find(selectedTest + " Data")
Set activeCell = activeCell.Offset(0, 1)
instancecol = Split(activeCell(1).Address(1, 0), "$")(0)
Set activeCell = activeCell.Offset(2, -1)
currentValue = activeCell.value
row = activeCell.row
activePage = template.Range("B" + row)
Set outputCell = json.Range("D26")
outputCell.Activate
outputCell.value = Chr(34) + "name" + Chr(34) + ": " + Chr(34) + activePage + Chr(34) + ","
Set outputCell = outputCell.Offset(1, 0)
outputCell.value = Chr(34) + "instance" + Chr(34) + ": " + Chr(34) + "1" + Chr(34) + ","
Set outputCell = outputCell.Offset(1, 0)
outputCell.value = Chr(34) + "Input" + Chr(34) + ": ["
Set outputCell = outputCell.Offset(0, 1)
While Not currentValue = "ENDPARSE"
If Not (activeCell.Offset(0, 1).value = "") Then
Dim currentPage As String
Dim referenceType As String
Dim reference As String
Dim action As String
Dim wait As String
Dim screenshot As String
Dim instance As String
Set outputCell = outputCell.Offset(1, 0)
outputCell.value = "{"
Set outputCell = outputCell.Offset(1, 1)
row = activeCell.row
currentPage = template.Range("B" + row)
If Not (activePage = currentPage) Then
activePage = currentPage
Set outputCell = outputCell.Offset(-1, -1)
outputCell.value = ""
Set outputCell = outputCell.Offset(-1, 0)
outputCell.value = "}"
Set outputCell = outputCell.Offset(1, -1)
outputCell.value = "]"
Set outputCell = outputCell.Offset(1, -1)
outputCell.value = "},"
Set outputCell = outputCell.Offset(1, 0)
outputCell.value = "{"
Set outputCell = outputCell.Offset(1, 1)
outputCell.value = Chr(34) + "name" + Chr(34) + ": " + Chr(34) + activePage + Chr(34) + ","
Set outputCell = outputCell.Offset(1, 0)
outputCell.value = Chr(34) + "instance" + Chr(34) + ": " + Chr(34) + "1" + Chr(34) + ","
Set outputCell = outputCell.Offset(1, 0)
outputCell.value = Chr(34) + "Input" + Chr(34) + ": ["
Set outputCell = outputCell.Offset(1, 1)
outputCell.value = "{"
Set outputCell = outputCell.Offset(1, 1)
End If
referenceType = template.Range("C" + row)
reference = template.Range("A" + row)
action = template.Range("F" + row)
wait = template.Range("H" + row)
screenshot = template.Range("I" + row)
instance = template.Range(instancecol + row)
currentValue = activeCell.value
outputCell.value = Chr(34) + "type" + Chr(34) + ": " + Chr(34) + referenceType + Chr(34) + ","
Set outputCell = outputCell.Offset(1, 0)
outputCell.value = Chr(34) + "reference" + Chr(34) + ": " + Chr(34) + reference + Chr(34) + ","
Set outputCell = outputCell.Offset(1, 0)
outputCell.value = Chr(34) + "action" + Chr(34) + ": " + Chr(34) + action + Chr(34) + ","
Set outputCell = outputCell.Offset(1, 0)
outputCell.value = Chr(34) + "instance" + Chr(34) + ": " + Chr(34) + instance + Chr(34) + ","
Set outputCell = outputCell.Offset(1, 0)
outputCell.value = Chr(34) + "wait" + Chr(34) + ": " + Chr(34) + wait + Chr(34) + ","
If Not (currentValue = "") Then
Set outputCell = outputCell.Offset(1, 0)
outputCell.value = Chr(34) + "value" + Chr(34) + ": " + Chr(34) + currentValue + Chr(34) + ","
End If
Set outputCell = outputCell.Offset(1, 0)
outputCell.value = Chr(34) + "screenshot" + Chr(34) + ": " + Chr(34) + screenshot + Chr(34)
Set outputCell = outputCell.Offset(1, -1)
outputCell.value = "},"
End If
Set activeCell = activeCell.Offset(1, 0)
currentValue = activeCell.value
Wend
outputCell.value = "}"
Set outputCell = outputCell.Offset(1, -1)
outputCell.value = "]"
Set outputCell = outputCell.Offset(1, -1)
outputCell.value = "}"
Set outputCell = outputCell.Offset(1, -1)
outputCell.value = "]"
Set outputCell = outputCell.Offset(1, -1)
outputCell.value = "}"
Dim dataRange As Range
Set dataRange = json.UsedRange
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(template.Range("I2,I2") + "\\" + template.Range("I3,I3") + ".json", True)
For Each C In dataRange
a.WriteLine (C.value)
Next C
a.Close
template.Activate
End Sub
これは現時点でのワークシートAの情報を書き込むために、ワークシートBの値をとり、私の出力は次のようになります。
{
"name": "Search",
"instance": "1",
"Input": [
{
"type": "button",
"reference": "Search",
"action": "Click",
"instance": "1",
"wait": "10",
"screenshot": "true"
},
{
"type": "dropdown",
"reference": "PostcodeLogic",
"action": "SelectByText",
"instance": "2",
"wait": "10",
"value": "Is",
"screenshot": "true"
},
{
"type": "text",
"reference": "PostCodeInput",
"action": "SendKeys",
"instance": "3",
"wait": "10",
"value": "AL2 4ED",
"screenshot": "true"
},
{
"type": "button",
"reference": "Search",
"action": "Click",
"instance": "5",
"wait": "10",
"screenshot": "true"
},
{
"type": "link",
"reference": "ClientName ",
"action": "Click",
"instance": "4",
"wait": "10",
"screenshot": "true"
}
]
}
私が欲しい情報がすべて表示されているので、正しく動作しています。私は今、テストのチャンクをinstance
の順に書き出すようにする必要があります。私はsequence
というフィールドを持っていて、数字を読んでそれをinstance
フィールドに入れます。
これがうまく説明されていない場合にはお詫び申し上げますが、VBAではまだ非常に新しいです。
Sequence列に先にソートを適用できますか? –
シーケンス番号を持つ列があります。これは、テキストの各チャンクから 'instance'値を取得する場所です。しかし、現時点では、上から下までの行が読み込まれます。その列の値に応じて読みたいと思います。たとえば、シーケンス値1、2などの行。 – Ross
したがって、コードの前に列をソートし、並べ替えを削除します。 –