掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
VBからエクセルファイルを読込み、エクセル内の文字を検索したい (ID:123795)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
>サーバー内のあるフォルダ に関しては、自部署のサーバー内を検索する予定であったことを思い出したので・・・改めて追記しました。 >しかし、あるサーバー内のあるフォルダの中のファイルを検索したいのですが、パスを指定する箇所がわかりません。 に関しては、パスを指定する箇所がわかりませんでした。 >利用したいフォルダ内にはいくつかフォルダがあり階層にもなっているので >それも検索できるようにしたいのです。 リンクして頂いたサンプルコードを利用して、検索可能になりました。またパスの指定箇所もわかりました。 >検索結果としてB列やC列もMsgBoxに表示できるようにしたいのですが この件なのですが、特攻隊長まるるうさんにサンプルコードを提示して頂いた内容丸々そのまま入れさせてもらい、そこから色々いじって理解してきました。(下記のようになりました。) どうしてもMsgBoxに表示させる内容が、うまくいきません。Rangeの使用方法を調べては見たのですが下記点がうまくできません。 ●B列を検索し、抽出できた内容がどの位置にあるかAdrress表示していることがわかりましたが、そのときのA列やC列も同時にMsgBoxに表示しようとヘルプでRangeの使い方を調べながらやっているのですがうまくいきません。 (Findの検索結果をRangeの引数に代入したらよいのか困ってます。) 例えばAdrressで$B$3と表示されるとき、A3,B3.C3も表示したいのですが・・・ 教えていただきたいのですがよろしくお願いします。 Dim lIndex As Long Dim nPrompt As String Dim nFilePath As String Dim nFilePathes() As String Dim strFile As String Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet ' InputBoxで検索したいファイル名を入力し、エクセルファイルを最下層まで検索し取得する strFile = "NTL302M" 'strFile = InputBox("機種") nFilePathes() = GetFilesMostDeep("D:\VB\", "" & strFile & ".xls") ' 取得したファイル名を列挙する (※ 添字が 1 からであることに注意) For lIndex = 1 To UBound(nFilePathes()) nPrompt = nPrompt & nFilePathes(lIndex) & vbNewLine Next lIndex ' 取得したすべてのファイルパス(機種ファイル)を表示する【確認】 If nPrompt <> "" Then Call MsgBox(nPrompt) End If '取得したパス(ファイル)を開く For lIndex = 1 To UBound(nFilePathes()) nPrompt = nPrompt & nFilePathes(lIndex) & vbNewLine Set xlApp = CreateObject("Excel.Application") 'エクセル起動 Set xlBook = xlApp.Workbooks.Open(nFilePathes(lIndex)) '開くファイルパスをOpenメソッドに代入 'xlApp.Visible = True Next lIndex '開いたファイル内を検索 Dim wMyRange As Range Dim wAnswerRange As Range Dim wColAlphabet As String Dim wFindAddress As String Dim wFindString As String Dim wFirstAddress As String 'wColAlphabet = InputBox("検索する列名をアルファベットで入力して下さい。") wColAlphabet = "B" '部番で設定 'ここで入力エラーチェックするべきです。 wFindAddress = wColAlphabet & "1:" & wColAlphabet & "10000" wFindString = "0002" 'wFindString = InputBox("部番") Set wMyRange = xlBook.Worksheets("Sheet1").Range(wFindAddress) ' answer = Application.WorksheetFunction.Find("Sheet1", wFindAddress, wFindString) With wMyRange Set wAnswerRange = .Find(wFindString, lookin:=Excel.XlFindLookIn.xlValues) If Not wAnswerRange Is Nothing Then 'wFirstAddress = wAnswerRange.Address wFirstAddress = wAnswerRange MsgBox wFirstAddress Do Set wAnswerRange = .FindNext(wAnswerRange) 'If Not wAnswerRange Is Nothing And wAnswerRange.Address <> wFirstAddress Then If Not wAnswerRange Is Nothing And wAnswerRange <> wFirstAddress Then MsgBox wAnswerRange Else Exit Do End If Loop 'While Not wAnswerRange Is Nothing And wAnswerRange.Address <> wFirstAddress Else MsgBox "見つかりませんでした。" End If End With ' Quit メソッドを使って Excel を終了します。 xlApp.Quit ' オブジェクトを解放します。 Set xlSheet = Nothing Set xlBook = Nothing Set xlApp = Nothing
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.