2016-09-23 3 views
2

マクロを含むExcelシート「ツール」があるディレクトリに「表示」という名前のExcelシートがあります。 Excelシートの表示には、約400行と20列のデータがあります。文字列を検索して隣接する列の内容を検索する

私は、ユーザーからの文字列入力(たとえば:DUMMY_TEXT)を受け取り、別のマクロを実行するマクロを持っています。別のマクロは、私が打たれた場所です。

列DのExcelシート「指示」のDUMMY_TEXTを検索し、文字列入力が見つかった対応する行のQ、R、SおよびT列の内容を検索する必要があります。これは、ユーザの入力に従って動的に行わなければならない。私は、これは、単一の列にlookat:=xlWhole試合のために私が今持っているもの

Dim FoundCell As Excel.Range 

temppath = ActiveWorkbook.Path 
Workbooks.Open (temppath + "\indications.xlsx") 

Set FoundCell = Range("D1:D20000").Find(what:=LSearchValue, lookat:=xlWhole) 

Workbooks("indications.xlsx").Close 
+0

あなたはワークシートの名前を公開する気にしませんでしたか? – Jeeped

+0

はコードのコピー中に変更を忘れていました。 – S6633d

+0

あなたのコードと物語は、**ワークブック**と**ワークシート**という用語を混乱させます。 [ワークブックオブジェクト](https://msdn.microsoft.com/en-us/library/office/ff835568.aspx)は[ワークシートオブジェクト](https://msdn.microsoft.com/)と同じではありませんen-us/library/office/ff194464.aspx)。 – Jeeped

答えて

1

である列Q、R、SおよびTの内容を見つけることで打ってい

、私はのExcel Application object使用することを好みますMATCH functionRange.Find methodを超える。

Sub trwqoeiryg() 
    Dim findThis As Variant, rw As Variant, wb As Workbook 
    Dim strQ as string, strR as string, strS as string, strT as string 
    findThis = "find This" 

    'VB/VBA uses an ampersand as the preferred string concatenation operator 
    Set wb = Workbooks.Open(Filename:=temppath & "\Indications.xlsx", ReadOnly:=True) 

    With wb 
     With .Worksheets(1) '<~~set this properly! 
      rw = Application.Match(findThis, .Columns("D"), 0) 
      If Not IsError(rw) Then 
       Debug.Print .Cells(rw, "Q").Value 
       Debug.Print .Cells(rw, "R").Value 
       Debug.Print .Cells(rw, "S").Value 
       Debug.Print .Cells(rw, "T").Value 
       strQ = .Cells(rw, "Q").Value 
       strR = .Cells(rw, "R").Value 
       strS = .Cells(rw, "S").Value 
       strT = .Cells(rw, "T").Value 
       Debug.Print strQ 
       Debug.Print strR 
       Debug.Print strS 
       Debug.Print strT 
      Else 
       Debug.Print findThis & " not found." 
      End If 
     End With 
     .Close SaveChanges:=False 
    End With 
End Sub 

MATCHを介して戻さ、Range.Cells propertyが容易同じ行上の任意の列から値を返すことができるの行数一度。

+0

検索語はセルの値全体と一致する必要がありますが、大文字と小文字は区別されません。 – Jeeped

+0

私はその値をDim value1のような変数に代入します。Stringとして設定します。value1 = Debug.Print .Cells(rw、 "Q")。値 – S6633d

+0

'dim str as string'を使い' str = .Cells(rw、 Q ")。Value。オブジェクトを 'Set'するだけです。文字列値を割り当てる必要はありません。上記の私の編集を参照してください。 – Jeeped

関連する問題