VBAからCMD書き出して、チェックするコード
Sub testdmd()
'コマンドプロンプトを使うためのオブジェクト
Dim wsh As New IWshRuntimeLibrary.WshShell
Dim result As WshExec
Dim cmd As String
Dim filedata() As String
Dim i As Integer
'実行したいコマンド
With Worksheets("check")
cmd = .Range("J28")
End With
'コマンドを実行
Set result = wsh.Exec("%ComSpec% /c " & cmd)
'コマンドの実行が終わるまで待機
Do While result.Status = 0
DoEvents
Loop
'結果を改行区切りで配列へ格納
filedata = Split(result.StdOut.ReadAll, vbCrLf)
'A1から順番に結果を書き込む
With Worksheets("CMD1")
i = 2
For Each filenm In filedata
.Cells(i, 2).Value = filenm
i = i + 1
Next
End With
Set result = Nothing
Set wsh = Nothing
End Sub
Sub チェック()
With Worksheets("CMD1")
Dim name As String
name = .Range("B17").Value '変数nameにセルB12の値『test』を代入
Dim rowsCnt As Long
rowsCnt = .Range("B2").CurrentRegion.Rows.Count '表の行数を数えます。
Dim i As Long
For i = 0 To rowsCnt - 2 '表の行数分繰り返します。
If .Cells(2 + i, 2).Value Like "*" & name & "*" = True Then
Worksheets("check").Cells(2 + i, 11).Value = "一致"
Else
Worksheets("check").Cells(2 + i, 11).Value = "不一致"
Worksheets("check").Cells(2 + i, 11).Interior.ColorIndex = 3
MsgBox "チェック完了"
End If
Next i
End With
End Sub
'コマンドプロンプトを使うためのオブジェクト
Dim wsh As New IWshRuntimeLibrary.WshShell
Dim result As WshExec
Dim cmd As String
Dim filedata() As String
Dim i As Integer
'実行したいコマンド
With Worksheets("check")
cmd = .Range("J28")
End With
'コマンドを実行
Set result = wsh.Exec("%ComSpec% /c " & cmd)
'コマンドの実行が終わるまで待機
Do While result.Status = 0
DoEvents
Loop
'結果を改行区切りで配列へ格納
filedata = Split(result.StdOut.ReadAll, vbCrLf)
'A1から順番に結果を書き込む
With Worksheets("CMD1")
i = 2
For Each filenm In filedata
.Cells(i, 2).Value = filenm
i = i + 1
Next
End With
Set result = Nothing
Set wsh = Nothing
End Sub
Sub チェック()
With Worksheets("CMD1")
Dim name As String
name = .Range("B17").Value '変数nameにセルB12の値『test』を代入
Dim rowsCnt As Long
rowsCnt = .Range("B2").CurrentRegion.Rows.Count '表の行数を数えます。
Dim i As Long
For i = 0 To rowsCnt - 2 '表の行数分繰り返します。
If .Cells(2 + i, 2).Value Like "*" & name & "*" = True Then
Worksheets("check").Cells(2 + i, 11).Value = "一致"
Else
Worksheets("check").Cells(2 + i, 11).Value = "不一致"
Worksheets("check").Cells(2 + i, 11).Interior.ColorIndex = 3
MsgBox "チェック完了"
End If
Next i
End With
End Sub