Dear all,
I need to resort to your wisdom as I have been struggling half day to work this VBA out.
I'm a total amateur in VBA and always resort to already posted codes, so apologies for my ignorance.
I need to search in a folder (with subfolders containing several files, including xlsm files with several sheets) a specific value.
I would like to see the contents of the row where that value is.
As an example, let's say I have a full set of excel files with payments and I need to search for the value "creditcard" and see the which excel file contains that value and list also the row (to be able to see the cost and date, for example)
As it was too complicated for me, I ended up "cleaning" the folder and now I only have a single folder with several xlsm files (only containing a single sheet).
I have the following code working,, but what it's been driving me crazy is the "(xlToLeft).Value". It works fine, BUT if I change to xlToRight it doesn't show the neighboring value. Why???
Sub SearchValue()
Dim strFirstAddress As String
Dim strSearch As String
Dim strExtension As String
Dim wOut As Worksheet
Dim wkbSource As Workbook
Dim wks As Worksheet
Dim rFound As Range
Dim lRow As Long
Const strPATH As String = "/Users/temp/" 'Folder to search
ChDir strPATH
strExtension = Dir("*.xlsm*") 'File type to search
strSearch = InputBox("Please enter the Search Keyword.") 'Search Keyword all reports
Set wOut = Worksheets.Add
wOut.Range("A1:D1") = Array("Workbook", "Workseet", "Cell", "Text in Cell")
Do While strExtension <> ""
Set wkbSource = Workbooks.Open(strPATH & strExtension)
With wkbSource
For Each wks In .Sheets
Set rFound = wks.UsedRange.Find(strSearch, LookIn:=xlValues, lookat:=xlWhole)
If Not rFound Is Nothing Then
strFirstAddress = rFound.Address
Do
lRow = wOut.Cells(Rows.Count, 1).End(xlUp).Row + 1
wOut.Cells(lRow, 1) = wkbSource.Name
wOut.Cells(lRow, 2) = wks.Name
wOut.Cells(lRow, 3) = rFound.Address
wOut.Cells(lRow, 4) = rFound.Value
wOut.Cells(lRow, 5) = wks.Cells(rFound.Row, Columns.Count).End(xlToLeft).Value
Set rFound = wks.UsedRange.FindNext(rFound)
Loop While rFound.Address <> strFirstAddress
sAddr = ""
End If
Next wks
End With
wkbSource.Close savechanges:=False
strExtension = Dir
Loop
Application.ScreenUpdating = True
End Sub
If someone could enlighthen me with the logic. I really cannot understand why it works and shows the value on the right (when I use xlToLeft) but it doesn't work at all when I use xlToRight.
Thanks!
JJCam
I need to resort to your wisdom as I have been struggling half day to work this VBA out.
I'm a total amateur in VBA and always resort to already posted codes, so apologies for my ignorance.
I need to search in a folder (with subfolders containing several files, including xlsm files with several sheets) a specific value.
I would like to see the contents of the row where that value is.
As an example, let's say I have a full set of excel files with payments and I need to search for the value "creditcard" and see the which excel file contains that value and list also the row (to be able to see the cost and date, for example)
As it was too complicated for me, I ended up "cleaning" the folder and now I only have a single folder with several xlsm files (only containing a single sheet).
I have the following code working,, but what it's been driving me crazy is the "(xlToLeft).Value". It works fine, BUT if I change to xlToRight it doesn't show the neighboring value. Why???
Sub SearchValue()
Dim strFirstAddress As String
Dim strSearch As String
Dim strExtension As String
Dim wOut As Worksheet
Dim wkbSource As Workbook
Dim wks As Worksheet
Dim rFound As Range
Dim lRow As Long
Const strPATH As String = "/Users/temp/" 'Folder to search
ChDir strPATH
strExtension = Dir("*.xlsm*") 'File type to search
strSearch = InputBox("Please enter the Search Keyword.") 'Search Keyword all reports
Set wOut = Worksheets.Add
wOut.Range("A1:D1") = Array("Workbook", "Workseet", "Cell", "Text in Cell")
Do While strExtension <> ""
Set wkbSource = Workbooks.Open(strPATH & strExtension)
With wkbSource
For Each wks In .Sheets
Set rFound = wks.UsedRange.Find(strSearch, LookIn:=xlValues, lookat:=xlWhole)
If Not rFound Is Nothing Then
strFirstAddress = rFound.Address
Do
lRow = wOut.Cells(Rows.Count, 1).End(xlUp).Row + 1
wOut.Cells(lRow, 1) = wkbSource.Name
wOut.Cells(lRow, 2) = wks.Name
wOut.Cells(lRow, 3) = rFound.Address
wOut.Cells(lRow, 4) = rFound.Value
wOut.Cells(lRow, 5) = wks.Cells(rFound.Row, Columns.Count).End(xlToLeft).Value
Set rFound = wks.UsedRange.FindNext(rFound)
Loop While rFound.Address <> strFirstAddress
sAddr = ""
End If
Next wks
End With
wkbSource.Close savechanges:=False
strExtension = Dir
Loop
Application.ScreenUpdating = True
End Sub
If someone could enlighthen me with the logic. I really cannot understand why it works and shows the value on the right (when I use xlToLeft) but it doesn't work at all when I use xlToRight.
Thanks!
JJCam