名前に特定の文字列が含まれている場合、フォルダ内のファイルを数えるコードがあります。ユーザーフォームの結果をvbaコード変数に渡す
例:ファイルの名前をクローズしてカウントしたい場合は(Close_26_03_2003.csv)。
現在、コードはシート内のセルの値を読み取り、(InStr関数を使用して)そのファイル名でその文字列を検索します。問題は、私はセルにファイルの種類を記述する必要があります。
私がしようとしているのは、3つのオプションボタン(開く、閉じる、キャンセル)でユーザーフォームを作成することです。 openの場合、文字列はopenと等しく設定され、その名前を持つファイルを検索します(closeと同じ)。キャンセルはサブを終了します。
問題私はこのためにユーザフォームでどのコードを使用しなければならないのか分かりませんし、ファイルを数えるコードに渡す方法もわかりません。
Sub CountFiles3()
Dim path As String, count As Integer, i As Long, var As Integer
Dim ws As Worksheet
Dim Filename As String
Dim FileTypeUserForm As UserForm1
Application.Calculation = xlCalculationManual
path = ThisWorkbook.path & "\*.*"
Filename = Dir(path)
'the problem is here:
'x = user form result***************
'if cancel = true, end sub
Set ws = ThisWorkbook.Sheets("FILES")
i = 0
Do While Filename <> ""
'var = InStr(Filename, ws.Cells(2, 7).Value) 'this is current code, it checks if the cell has open or close
var = InStr(Filename, x)
If var <> 0 Then
i = i + 1
ws.Cells(i + 1, 1) = Filename
Filename = Dir()
Else: Filename = Dir()
End If
Loop
Application.Calculation = xlCalculationAutomatic
ws.Cells(1, 2) = i
MsgBox i & " : files found in folder"
End Sub
そして、これは私の現在のユーザーフォームのコードです:
コードなどがある
Private Sub Cancel_Click()
Me.Tag = 3 ' EndProcess
Me.Hide
End Sub
Private Sub ClosingType_Click()
Me.Tag = 2 ' "CLOSING"
Me.Hide
End Sub
Private Sub OpeningType_Click()
Me.Tag = 1 ' "OPENING"
Me.Hide
End Sub
任意のアイデア?
[ユーザーフォームを呼び出して値を返す](http:// sta ckoverflow.com/questions/18966137/calling-a-userform-and-returning-a-value) – Carrosive