
下記のマクロは先日教えて頂いたマクロで、マクロを実行すると指定セル値に該当するサブフォルダ名がマクロ設定ブックと同じフォルダ内に移動できます。
このマクロを下記の内容に変更出来る方法を教えてください。
指定セル値「R18」に表示されている数字とサブフォルダ名の数字とが一致し、
メッセージボックスで「該当フォルダがありました、フォルダを移動しますか」と表示され、「OK」をクリックすると「ダイアログが開き、自身で指定したフォルダ内に移動できるように変更出来る方法を教えてください。
現状のマクロ
Sub Macro1()
Dim i As Long
Dim strKeyword As String
Dim strFolderPath As String, strFolderName As String
Dim arrMoveFolders As Variant
Dim strOriginPath As String, strDestPath As String
strKeyword = CStr(Sheets("青紙表").Range("R18").Value)
If strKeyword = "" Then MsgBox ("キーワードが空白です"): Exit Sub
If MsgBox("フォルダを検索しますか", vbOKCancel) <> vbOK Then Exit Sub
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
strOriginPath = "\\nas-sp01\share\確認部\■01_敷地照会回答書"
strDestPath = ThisWorkbook.Path
If Right(strDestPath, 1) = "\" Then strDestPath = Left(strDestPath, Len(strDestPath) - Len("\"))
ReDim arrMoveFolders(0 To 1, 0 To 0)
For i = 0 To 9
strFolderPath = strOriginPath & "\" & i
On Error Resume Next
strFolderName = Dir(strFolderPath & "\*" & strKeyword & "*", vbDirectory)
If Err.Number <> 0 Then
MsgBox "エラーが発生しました" & vbCrLf _
& strFolderPath & "\*" & strKeyword & vbCrLf _
& Err.Description, vbExclamation
Exit Sub
End If
Do Until strFolderName = ""
If Replace(strFolderName, ".", "") <> "" Then
If arrMoveFolders(0, 0) <> Empty Then ReDim Preserve arrMoveFolders(UBound(arrMoveFolders, 1), UBound(arrMoveFolders, 2) + 1)
arrMoveFolders(0, UBound(arrMoveFolders, 2)) = strFolderPath & "\" & strFolderName
arrMoveFolders(1, UBound(arrMoveFolders, 2)) = strFolderName
strFolderName = Dir
End If
Loop
Next
If arrMoveFolders(0, 0) = Empty Then
MsgBox "該当フォルダがありません"
Exit Sub
Else
If MsgBox("該当フォルダがありました、フォルダを移動しますか", vbOKCancel) <> vbOK Then Exit Sub
End If
Dim objWSH As Object
Set objWSH = CreateObject("WScript.Shell")
Dim psCmd As String
For i = 0 To UBound(arrMoveFolders, 2)
psCmd = "Move-Item " & arrMoveFolders(0, i) & " " & strDestPath & "\" & arrMoveFolders(1, i)
objWSH.Run "powershell -NoLogo -ExecutionPolicy RemoteSigned -Command " & psCmd, 0, True
Next
End Sub
以上となります。よろしくお願いいたします。
No.2ベストアンサー
- 回答日時:
書くのが面倒でしたのでFunctionにしましたが、しない方が良かったかな?
>をどのように設定してよいかが、わかりません。
メインプロシージャの外に書けばよいです
Sub Macro1()
Dim i As Long
Dim strKeyword As String
Dim strFolderPath As String, strFolderName As String
Dim arrMoveFolders As Variant
Dim strOriginPath As String, strDestPath As String
strKeyword = CStr(Sheets("青紙表").Range("R18").Value)
If strKeyword = "" Then MsgBox ("キーワードが空白です"): Exit Sub
If MsgBox("フォルダを検索しますか", vbOKCancel) <> vbOK Then Exit Sub
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
strOriginPath = "\\nas-sp01\share\確認部\■01_敷地照会回答書"
If Right(strDestPath, 1) = "\" Then strDestPath = Left(strDestPath, Len(strDestPath) - Len("\"))
ReDim arrMoveFolders(0 To 1, 0 To 0)
For i = 0 To 9
strFolderPath = strOriginPath & "\" & i
On Error Resume Next
strFolderName = Dir(strFolderPath & "\*" & strKeyword & "*", vbDirectory)
If Err.Number <> 0 Then
MsgBox "エラーが発生しました" & vbCrLf _
& strFolderPath & "\*" & strKeyword & vbCrLf _
& Err.Description, vbExclamation
Exit Sub
End If
Do Until strFolderName = ""
If Replace(strFolderName, ".", "") <> "" Then
If arrMoveFolders(0, 0) <> Empty Then ReDim Preserve arrMoveFolders(UBound(arrMoveFolders, 1), UBound(arrMoveFolders, 2) + 1)
arrMoveFolders(0, UBound(arrMoveFolders, 2)) = strFolderPath & "\" & strFolderName
arrMoveFolders(1, UBound(arrMoveFolders, 2)) = strFolderName
strFolderName = Dir
End If
Loop
Next
If arrMoveFolders(0, 0) = Empty Then
MsgBox "該当フォルダがありません"
Exit Sub
Else
If MsgBox("該当フォルダがありました、フォルダを移動しますか", vbOKCancel) <> vbOK Then Exit Sub
strDestPath = FolderPicker '当該部分
If strDestPath = Empty Then Exit Sub '当該部分
End If
Dim objWSH As Object
Set objWSH = CreateObject("WScript.Shell")
Dim psCmd As String
For i = 0 To UBound(arrMoveFolders, 2)
psCmd = "Move-Item " & arrMoveFolders(0, i) & " " & strDestPath & "\" & arrMoveFolders(1, i)
objWSH.Run "powershell -NoLogo -ExecutionPolicy RemoteSigned -Command " & psCmd, 0, True
Next
End Sub
'当該部分Function
Private Function FolderPicker() As String
Dim folderPath As Variant
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = 0 Then
MsgBox "キャンセルしました"
Exit Function
End If
folderPath = .SelectedItems(1)
End With
FolderPicker = folderPath
End Function
No.1
- 回答日時:
strDestPath = ThisWorkbook.Path 不要
該当Ifブロック書き換え
If arrMoveFolders(0, 0) = Empty Then
MsgBox "該当フォルダがありません"
Exit Sub
Else
If MsgBox("該当フォルダがありました、フォルダを移動しますか", vbOKCancel) <> vbOK Then Exit Sub
strDestPath = FolderPicker
If strDestPath = Empty Then Exit Sub
End If
フォルダピッカーFunctionを追加(同じモジュール)
Private Function FolderPicker() As String
Dim folderPath As Variant
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = 0 Then
MsgBox "キャンセルしました"
Exit Function
End If
folderPath = .SelectedItems(1)
End With
FolderPicker = folderPath
End Function
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
一つのTeratermのマクロで複数...
-
ExcelVBAでPDFを閉じるソース
-
エクセルで特定の列が0表示の場...
-
Excel_マクロ_現在開いているシ...
-
Excel マクロ VBA プロシー...
-
Excel マクロでShearePoint先の...
-
Libre office マクロ
-
エクセルのマクロで「実行時エ...
-
ExcelVBA 図形をクリックした...
-
UWLSの記録でマクロを作成し使...
-
エクセルのマクロとシートの保...
-
段落の先頭に文字列を挿入する...
-
印刷枚数を指定して印刷(印刷...
-
テニス(ダブルス)乱数表 Exc...
-
メッセージボックスのOKボタ...
-
マクロの登録を使って、オート...
-
エクセルのマクロについて教え...
-
TERA TERMを隠す方法
-
エクセルのマクロが正常に動か...
-
右クリックによるイベントマク...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
エクセルで特定の列が0表示の場...
-
特定のPCだけ動作しないVBAマク...
-
メッセージボックスのOKボタ...
-
Excel_マクロ_現在開いているシ...
-
一つのTeratermのマクロで複数...
-
マクロの連続印刷が突然不可能...
-
ExcelのVBA。public変数の値が...
-
Excel マクロ VBA プロシー...
-
Excel・Word リサーチ機能を無...
-
エクセルに張り付けた写真のフ...
-
Excelのセル値に基づいて図形の...
-
TERA TERMを隠す方法
-
マクロ実行時エラー
-
ExcelVBAでPDFを閉じるソース
-
wordを起動した際に特定のペー...
-
特定文字のある行の前に空白行...
-
Excel マクロでShearePoint先の...
-
エクセルで縦に並んだデータを...
-
マクロ実行時、ユーザーフォー...
-
ソース内の行末に\\
おすすめ情報
回答ありがとうございました。
何時もご連絡ありがとうございます。
大変申し訳ありません、
フォルダピッカーFunctionを追加(同じモジュール)
Private Function FolderPicker() As String
Dim folderPath As Variant
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = 0 Then
MsgBox "キャンセルしました"
Exit Function
End If
folderPath = .SelectedItems(1)
End With
FolderPicker = folderPath
End Function
をどのように設定してよいかが、わかりません。
お手数をお掛け致しますが、
教えていただけますでしょうか。
よろしくお願いいたします。
回答ありがとうございます。
上手くできました。
別件で
昨日教えて頂きましたマクロで、少々解決したい問題が発生いたしました。
別質問をさせて頂きます、
何卒、お助けください。
よろしくお願いいたします。