私はExcel VBAマクロコードを多数作成しており、多くの成果を上げています。私はABC12345のような開いているExcelブックの1つで利用可能なキーワードを検索したいという問題にぶつかりました。そして、 "ABC"がセルB2に見つかったら条件を満たしたい。開いているすべてのExcelブックでキーワード検索を実行するVBA
私のコードは、これまで:
Sub ABC_Upload()
Sheets("Add File Here").Select
If IsEmpty(Range("A1")) Then
Worksheets("Master Mapper").Activate
Dim answerABC As Integer
answerABC = MsgBox("Please check the Data Sheet. No value found in first row! Do you wish to find XYZ file in open workbooks and start process?", vbYesNo + vbQuestion, "Review & Proceed")
If answerABC = vbYes Then
'Starts here
Dim wSheet As Worksheet
Dim wBook As Workbook
Dim XYZFound As Range
Dim xFound As Boolean
Dim lngLastRow2 As Long
On Error Resume Next
For Each wBook In Application.Workbooks
For Each wSheet In wBook.Worksheets
Set XYZFound = Nothing
Set XYZFound = wSheet.Cells.Find(What:="ABC", After:=wSheet.Cells(1, 1), _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=True)
'Set XYZFound = wSheet.Cells.Find(What:="BIC", After:=wSheet.Cells(1, 1), _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
'XYZFound.Cells.Select
If Not XYZFound Is Nothing Then
xFound = True
Application.Goto XYZFound, True
'Rows(1, 2).EntireRow.Hidden = True
lngLastRow2 = Cells(Cells.Rows.Count, "B").End(xlUp).Row
Range("A1:E" & lngLastRow2).Copy
ThisWorkbook.Worksheets("Add File Here").Activate
Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End If
Next wSheet
If xFound Then Exit For
Next wBook
If XYZFound Is Nothing Then
MsgBox "No open file for XYZ Meetings Found. Make sure the most recent XYZ Excel WB is open!", vbCritical + vbOKOnly
Exit Sub
End If
'Ends Here
Sheets("Add File Here").Select
Columns("A").Replace _
What:=";", Replacement:=""
Columns("A").Replace _
What:=":", Replacement:=""
Columns("A").Replace _
What:=",", Replacement:=""
Columns("A").Replace _
What:="(", Replacement:=""
Columns("A").Replace _
What:=")", Replacement:=""
Columns("A").Replace _
What:="{", Replacement:=""
Columns("A").Replace _
What:="}", Replacement:=""
Columns("A").Replace _
What:="[", Replacement:=""
Columns("A").Replace _
What:="]", Replacement:=""
Columns("A").Replace _
What:="~+", Replacement:=""
Columns("A").Replace _
What:="~*", Replacement:=""
Columns("A").Replace _
What:="~?", Replacement:=""
Columns("A").Replace _
What:="_", Replacement:=""
Columns("A").Replace _
What:=".", Replacement:=""
Columns("A").Replace _
What:="'", Replacement:=""
Columns("A").Replace _
What:="\", Replacement:=""
Columns("A").Replace _
What:="/", Replacement:=""
Columns("A").Replace _
What:=".", Replacement:=""
Columns("A").Replace _
What:="@", Replacement:=""
Columns("A").Replace _
What:=Chr(34), Replacement:=""
Columns("C:C").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Value = "Client ID"
Columns("D:D").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("D1").Value = "Client Name"
Columns("E:E").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("E1").Value = "Planner Name"
Columns("I:I").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("I1").Value = "External System Name"
Dim rng As Range
Dim i As Long
'Set the range in column A you want to loop through
Set rng = Range("B2:B100")
For Each cell In rng
'test if cell is empty
If cell.Value <> "" Then
'write to adjacent cell
cell.Offset(0, 1).Value = "Company ID"
End If
Next
Dim rngC As Range
Dim Ci As Long
'Set the range in column A you want to loop through
Set rngC = Range("C2:C100")
For Each cell In rngC
'test if cell is empty
If cell.Value <> "" Then
'write to adjacent cell
cell.Offset(0, 1).Value = "Company"
End If
Next
Dim rngP As Range
Dim Pi As Long
'Set the range in column A you want to loop through
Set rngP = Range("D2:D100")
For Each cell In rngP
'test if cell is empty
If cell.Value <> "" Then
'write to adjacent cell
cell.Offset(0, 1).Value = "NA"
End If
Next
Dim rnEP As Range
Dim Ei As Long
'Set the range in column A you want to loop through
Set rngE = Range("H2:H100")
For Each cell In rngE
'test if cell is empty
If cell.Value <> "" Then
'write to adjacent cell
cell.Offset(0, 1).Value = "Company"
End If
Next
'MsgBox "File has been formatted for XYZ and is ready for MMS upload.", vbOKOnly
Dim answer As Integer
answer = MsgBox("Temporary File Prepared for XYZ. Do you wish to proceed with MMS_NewMtgs file creation?", vbYesNo + vbQuestion, "Review & Proceed")
If answer = vbYes Then
Call Prepare_OutputFile
Else
MsgBox "Output file not created!! Please select - Click to create MMS Formatted File from Master Mapper.", vbOKOnly
End If
End If
End If
ThisWorkbook.Saved = True
End Sub
任意の提案をいただければ幸いです。
ありがとうございます!
正確には動作しません。また、エラーの再開を削除すると、実際のエラーの原因を隠すため、デバッグに役立ちます。 – nwhaught
MsgBoxからの回答を 'answerabc'に設定しますが、' If'文では 'answerBICL'をチェックします。それは "タイプミス"ですか? –
期待どおりのワークブック/ワークシートですべての範囲を修飾する必要があります。あなたが 'Range(Cells()、Cells()) 'を持っているときは、** ActiveWorkbook/Worksheet'の範囲とセルを使用します。あなたは 'wBook.wSheet.Cells(Rows.Count、" B ")End(xlUp).Row'のようにする必要があります。そうしないと、アクティブブック/シートから最後の行を取得します。あなたがそれを望むなら、明示的にしてください。 ( 'Rows.Count'はシート/ブックの参照が必ずしも必要ではないことに注意してください。これは、すべてのワークシートで同じ(仮定している)ためです。) – BruceWayne