I need to create an Excel VBA Macro that is able to Loop through some Files and if it finds the given String it should fill the Excel Worksheet where I need to.
Currently it looks like this: I show a UserForm that has a TextBox where the String gets entered and a Button.
If the User clicks on that Button then the files should get looped through and if it finds the string in one of that files it should enter something new to the excel where the macro is called from.
I have searched on SO but with no Luck, I found this:
Sub LoopThroughFiles()
Dim StrFile As String
StrFile = Dir("C:\Users\xxx\xxx\xxx\test\*test*")
Do While Len(StrFile) > 0
Debug.Print StrFile
StrFile = Dir
Loop
End Sub
But this looks like it loops and looks if the filename has test in it and not if the actual file has a Value that is called "test".
Also the string that needs to be found is always in the first column of the files. And I would have to read the second column in that activeCell that I would get if the String is found and add that to the Excel where I call this Macro from.
Sincerly Faded ~
Edit:
Sub ReadDataFromAnotherWorkBook()
' Open Workbook A with specific location
Dim src As Workbook
Set src = Workbooks.Open("C:\Users\xxx\Desktop\xxx\test\x1x.xlsx", True, True)
Dim valueBookA As String
Dim valueBookB As Integer
valueBookA = src.Worksheets("Tabelle1").Cells(2, 1) ' Works but here I need to put the enteredValue and search for it
Cells(1, 1).Value = valueBookA
' Close Workbooks A
src.Close False
Set src = Nothing
' Dialog Answer
MsgBox valueBookA
End Sub
This gives me a Value from the read Excel which is good as a first start. I need to loop that to open up more files and also I need the part where I can search for the given String and get the value in that row.
Edit2:
This is what I have now but I cant get the value.. what am I doing wrong :/
Sub ReadDataFromAnotherWorkBook()
Dim SearchString As String
Dim SearchRange As Range, cl As Range
Dim FirstFound As String
Dim sh As Worksheet
' Open Workbook A with specific location
Dim src As Workbook
Set src = Workbooks.Open("C:\Users\x\Desktop\xxx\test\xxx.xlsx", True, True)
' Set Search value
SearchString = TextBox1.Value ' TEST mit TextBox Value -- works
Application.FindFormat.Clear
' loop through all sheets
For Each sh In src.Worksheets
' Find first instance on sheet
Set cl = sh.Cells.Find(What:=SearchString, _
After:=sh.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not cl Is Nothing Then
' if found, remember location
FirstFound = cl.Address
MsgBox FirstFound
' format found cell
Do
' cl.Font.Bold = True
' cl.Interior.ColorIndex = 3
Debug.Print FirstFound
MsgBox FirstFound ' Does not work..
' Debug.Print cl.Value
MsgBox cl.Value ' Also does not work -- I need the VALUE that is in the Excel Row or Column where the string gets found
' find next instance
Set cl = sh.Cells.FindNext(After:=cl)
' repeat until back where we started
Loop Until FirstFound = cl.Address
End If
Next
MsgBox "Value in Excel? : " + FirstFound 'cl.Value > Is empty..
MsgBox "SEARCHSTRING :: " + SearchString ' Gives me the right String
' Close Workbooks A ' Closes the Workbook
src.Close False
Set src = Nothing
End Sub
Range("A:A").Find(..... If you set this as aRangevariable you can test if anything was found and useOffset(1)to get to the value in second column next to found cellDirlist files where the name matches a pattern. You want to look into the files to search for the text, you need a different approach. If it is text files, look at stackoverflow.com/questions/17275040/…. But as you talk about a Column, I assume you are talking about Excel files. You will need to open them to look for the text