Every day I receive several.xls containing information about a particular item, for example, a car.
I developed this macro in which I don't need to open all the files because the macro imports all the data I need.
For Each File In Folder.Files
DoEvents
Set xlBook = xlApp.Workbooks.Open(File, False)
Set xlSheet = xlBook.Sheets(1)
On Error Resume Next
Do
ws.Cells(i, 1) = FindCarModel(xlSheet) 'MODEL:
Loop While xlSheet.Cells(j, rngQTE.Column) <> ""
ThisWorkbook.Worksheets("T_G").Cells(n, 1) = FindCarModel(xlSheet)
n = n + 1
xlBook.Close False
Set xlBook = Nothing
Next
End Sub
Private Function FindCarModel(ws As Worksheet) As String
Dim EncontraString As String
Dim Intervalo As Range
Dim i As Integer
EncontraString = "MODEL:"
With ws.Range("A:IV")
Set Intervalo = .Find(What:=EncontraString, _
After:=.Cells(1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
If Not Intervalo Is Nothing Then
i = Intervalo.Column + 1
Do While ws.Cells(Intervalo.Row, i) = ""
i = i + 1
Loop
FindCarModel = ws.Cells(Intervalo.Row, i)
End If
End With
End Function
The macro searches for the word "model" and pastes the value of the first cell to the right.
I am no longer receiving files with a single "car model".
How can I return all the car models inside the xls and not just the first one found.
Use FindNext
Option Explicit
Sub FindAll()
Dim fso As Object, ts As Object, folder, file
Dim wb As Workbook, ws As Worksheet, result As Collection
Dim xlBook As Workbook, xlSheet As Worksheet
Dim r As Long, n As Integer
Set wb = ThisWorkbook
Set ws = wb.Sheets("T_G")
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.getfolder("path-to-files")
r = 1
For Each file In folder.Files
Set xlBook = Workbooks.Open(file, False, True) ' read only
Set xlSheet = xlBook.Sheets(1)
Set result = FindCarModel(xlSheet) 'MODEL:
xlBook.Close False
Set xlBook = Nothing
For n = 1 To result.Count
r = r + 1
ws.Cells(r, 1) = result.Item(n)
Next
Next
MsgBox r & " Items found"
End Sub
Private Function FindCarModel(ws As Worksheet) As Collection
Const EncontraString = "MODEL:"
Dim Intervalo As Range, i As Integer, sFirstFind As String
Dim result As New Collection
With ws.Range("A:IV")
Set Intervalo = .Find(What:=EncontraString, _
After:=.Cells(1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
If Not Intervalo Is Nothing Then
sFirstFind = Intervalo.Address
Do
i = Intervalo.Column + 1
Do While ws.Cells(Intervalo.Row, i) = ""
i = i + 1
Loop
result.Add ws.Cells(Intervalo.Row, i).Value2
Set Intervalo = .FindNext(Intervalo)
Loop While Intervalo.Address <> sFirstFind
End If
End With
Set FindCarModel = result
End Function
Related
I am at an impasse figuring out how to catch this null range variable exception.
I am attempting to scan for a row of headers to recover data from a few rows under, the excel datasheets may have multiple "pages" with a new header and date on the next "page" if there happens to be data to fill it and this can extend to many pages.
My loop appears to break on the second pass after the find function is unable to find additional rows with the desired header. My if statement is not able to detect that the variable is blank and I get an object not set error repeatedly.
I have tried several ways at calling null exceptions such as is empty, is null, both in a few different syntax forms, but still no success.
Thanks in advance for your help!
Sub testingBreak()
Dim testing As String
Dim starting As String
testing = "testing"
starting = "starting"
Dim productNameRange() As Range
Dim PN2CellAddress As String
Dim rowCount As Integer
rowCount = 0
Dim oldCount As Integer
oldCount = 0
ReDim productNameRange(rowCount)
Dim r As Integer
Set productNameRange(rowCount) = Sheets(starting).Cells.Find( _
What:="Product Name", LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True)
If productNameRange(rowCount) Is Nothing Then
MsgBox ("Search Error: Header Not found")
Else
Do While Not IsEmpty(productNameRange(rowCount)) 'this is to search for additional rows with the same header name
oldCount = rowCount
rowCount = rowCount + 1
MsgBox rowCount & " & " & oldCount
ReDim Preserve productNameRange(rowCount)
If IsNull(productNameRange(oldCount)) Then '<<<<this if statement does not catch that the variable was not set :( <<<<<
MsgBox "null exception worked"
Else
MsgBox productNameRange(oldCount) '<<<<on second loop, I get the error "object varriable or with block varriable not set"... <<<<<<
End If
Set productNameRange(rowCount) = Sheets(starting).Range(productNameRange(oldCount).Address).FindNext( _
productNameRange(oldCount)) ' <<< does not set the next range if there is none
Loop
MsgBox rowCount & "Row(s) have been found!"
For r = 0 To rowCount - 1
MsgBox productNameRange(r)
Next r
End If
End Sub
So this seemed to fix my issue. Thank you to everyone for your help
Dim f As Variant
Private Function FindAllHeaderRows(val As String, filePath As String) As Collection
Dim rv As New Collection, g As Range
Dim addr As String
Dim wb As Workbook: Set wb = Workbooks.Open(filePath) ' workbook containing this code
Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1")
Set g = ws.Cells.Find(What:=val, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True)
If Not g Is Nothing Then addr = g.Address
Do Until g Is Nothing
rv.Add g
Set g = ws.Cells.FindNext(After:=g)
If Not g Is Nothing Then
If g.Address = addr Then Exit Do
End If
Loop
Set FindAllHeaderRows = rv
End Function 'working!
Sub testSub1()
Dim FileToOpen As String
FileToOpen = Application.GetOpenFilename(Title:="Select Data file")
Set rangeCo = FindAllHeaderRows("Product Name", FileToOpen)
For Each f In rangeCo
MsgBox f.Address 'shows address
Next f
MsgBox rangeCo.count ' shows how many
End Sub
Find Criteria Cells (Find & FindNext)
Sub FindCriteriaCells()
Const wsName As String = "Starting"
Const Criteria As String = "Product Name"
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
Dim rg As Range: Set rg = ws.UsedRange
Dim fCell As Range: Set fCell = rg.Find(What:=Criteria, _
After:=rg.Cells(rg.Rows.Count, rg.Columns.Count), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=True)
Dim Headers() As Range
Dim n As Long
If Not fCell Is Nothing Then
Dim FirstAddress As String: FirstAddress = fCell.Address
Do
ReDim Preserve Headers(0 To n)
Set Headers(n) = fCell
n = n + 1
Set fCell = rg.FindNext(After:=fCell)
Loop Until fCell.Address = FirstAddress
End If
Dim Msg As String
If n > 0 Then
Msg = "The header '" & Criteria & "' was found in " _
& n & " cell(s):" & vbLf
For n = 0 To n - 1
Msg = Msg & vbLf & Headers(n).Address(0, 0)
Next n
MsgBox Msg, vbInformation
Else
Msg = "The header '" & Criteria & "' was not found."
MsgBox Msg, vbExclamation
End If
End Sub
I have code that works. I need to change the sheet name every time I run it.
I want to apply this macro to any sheet, without considering the sheetname.
Sub DeleteColumns()
Dim ColAry, i As Long, fc As Long
Application.ScreenUpdating = False
ColAry = Array("TIENDA_ID", "QCT_NAME", "PRODUCTO_ID", "CATEGORIA", _
"FACT_NAME", "FACT_VALUE", "PRECIO HISTORICO", "CORRECIÓN", _
"W12", "W11", "W10", "W09", _
"W08", "W07", "W06", "W05", _
"W04", "W03", "W02", "W01", _
"W52", "W50", "W13", "W14", _
"W15", "W16", "CDAR ID", "QCT")
With Sheets("Sheet11")
For i = LBound(ColAry) To UBound(ColAry)
fc = 0
On Error Resume Next
fc = Application.Match(ColAry(i), .Rows(1), 0)
On Error GoTo 0
If fc > 0 Then
.Columns(fc).Delete
End If
Next i
End With
Application.ScreenUpdating = True
End Sub
Delete Columns With Specified Headers
Option Explicit
Sub DeleteColumnsTEST()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet11")
DeleteColumns ws
End Sub
Sub DeleteColumns(ByVal ws As Worksheet)
Dim ColAry As Variant: ColAry = Array( _
"TIENDA_ID", "QCT_NAME", "PRODUCTO_ID", "CATEGORIA", _
"FACT_NAME", "FACT_VALUE", "PRECIO HISTORICO", "CORRECIÓN", _
"W12", "W11", "W10", "W09", _
"W08", "W07", "W06", "W05", _
"W04", "W03", "W02", "W01", _
"W52", "W50", "W13", "W14", _
"W15", "W16", "CDAR ID", "QCT")
Application.ScreenUpdating = False
Dim fc As Variant
Dim i As Long
With ws
For i = LBound(ColAry) To UBound(ColAry)
fc = Application.Match(ColAry(i), .Rows(1), 0)
If IsNumeric(fc) Then .Columns(fc).Delete
Next i
End With
Application.ScreenUpdating = True
End Sub
I am a beginner in VBA and I am trying to fix an issue in a code written by someone else. Any help will be much appreciated.
Background:
The main macro workbook uses reference data from other excel files. The file path for the reference files are referenced within the main macro workbook in "reference workbooks" tab. They are loaded as Arrays in the macro workbook. For one of the reference data files, I need to sort and filter the data in a particular sheet, then copy all the cells still visible into a sheet in the main macro workbook.
Issue:
Currently the macro only copies and pastes the header rows and 42 blank rows. There are more than 1000 rows with data which are not copied. The data in reference files is normally between columns A:BL.
I've shared only the relevant portion of the code for this problem - there are multiple reference files otherwise.
Sub dataLoad()
Dim wb As Workbook: Set wb = ThisWorkbook
refpath = wb.path & "\Reference Files\"
path = ThisWorkbook.path
Set refiles = Sheets("Reference Workbooks")
SourceData = refiles.Range("B13").Value
wrksht'SourceData = refiles.Range("C13").Value
'1) COPY REFERENCE WORKSHEETS TO WORKBOOK:
'This allows the template to access the reference materials without having to open and close external documents
'1a)Create arrays for the reference workbooks
Dim fileArr() As Variant
ReDim fileArr(5)
fileArr = Array(SourceData)
'1b)Create arrays for the reference worksheets
Dim wrkshtArr() As Variant
ReDim wrkshtArr(5)
wrkshtArr = Array(wrkshtSourceData)
Dim intcount As Integer
Dim intsheet As Integer
Dim sheetArr() As Variant
intsheet = ActiveWorkbook.Sheets.Count
ReDim sheetArr(intsheet)
For intcount = 1 To intsheet
sheetArr(intcount) = ActiveWorkbook.Sheets(intcount).Name
Next
'1c)A For statement that(i) checks if reference worksheet is already loaded and (ii) copies them to the workbook if not
Application.ScreenUpdating = False
For i = 0 To UBound(fileArr)
testvar = False
For x = 0 To UBound(sheetArr)
If testvar = False Then
If sheetArr(x) = wrkshtArr(i) Then
testvar = True
End If
End If
Next x
If testvar = False Then
Set closedBook = Workbooks.Open(refpath & fileArr(i))
closedBook.Sheets(wrkshtArr(i)).Copy After:=Workbooks(wb.Name).Sheets(wb.Worksheets.Count)
closedBook.Close SaveChanges:=False
Worksheets(wrkshtArr(i)).Visible = xlSheetHidden
End If
Next i
With Worksheets(wrkshtSourceData)
Dim Datadate As Range: Set Datadate = .Rows("1:1").Find(What:="Date_of_Entry", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
Dim Datastatus As Range: Set Datastatus = .Rows("1:1").Find(What:="FinalStatus", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
.Activate
.UsedRange.Sort Key1:=Range(.Columns(Datadate.Column).Address(Rowabsolute:=False)), Order1:=xlDescending, Header:=xlYes
.Range(.Columns(Datastatus.Column).Address(Rowabsolute:=False)).AutoFilter Field:=1, Criteria1:="Approved"
Sheets.Add.Name = "Database2"
.UsedRange.SpecialCells(xlCellTypeVisible).Copy
Worksheets("Database2").Paste
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
Worksheets("Database2").Name = wrkshtSourceData
Worksheets(wrkshtSourceData).Visible = xlSheetHidden
End With
Application.ScreenUpdating = True
End Sub
Maybe you can set the range you need using coordinates of the complete shown data. My contributions inside '#####code lines
Please let my know if you need some edit, to this answer, because I'm not sure of this.
Sub dataLoad()
Dim wb As Workbook: Set wb = ThisWorkbook
refpath = wb.Path & "\Reference Files\"
Path = ThisWorkbook.Path
Set refiles = Sheets("Reference Workbooks")
SourceData = refiles.Range("B13").Value
wrksht 'SourceData = refiles.Range("C13").Value
'1) COPY REFERENCE WORKSHEETS TO WORKBOOK:
'This allows the template to access the reference materials without having to open and close external documents
'1a)Create arrays for the reference workbooks
Dim fileArr() As Variant
ReDim fileArr(5)
fileArr = Array(SourceData)
'1b)Create arrays for the reference worksheets
Dim wrkshtArr() As Variant
ReDim wrkshtArr(5)
wrkshtArr = Array(wrkshtSourceData)
Dim intcount As Integer
Dim intsheet As Integer
Dim sheetArr() As Variant
intsheet = ActiveWorkbook.Sheets.Count
ReDim sheetArr(intsheet)
For intcount = 1 To intsheet
sheetArr(intcount) = ActiveWorkbook.Sheets(intcount).Name
Next
'1c)A For statement that(i) checks if reference worksheet is already loaded and (ii) copies them to the workbook if not
Application.ScreenUpdating = False
For i = 0 To UBound(fileArr)
testvar = False
For x = 0 To UBound(sheetArr)
If testvar = False Then
If sheetArr(x) = wrkshtArr(i) Then
testvar = True
End If
End If
Next x
If testvar = False Then
Set closedBook = Workbooks.Open(refpath & fileArr(i))
closedBook.Sheets(wrkshtArr(i)).Copy After:=Workbooks(wb.Name).Sheets(wb.Worksheets.Count)
closedBook.Close SaveChanges:=False
Worksheets(wrkshtArr(i)).Visible = xlSheetHidden
End If
Next i
'#####################code
Dim R 'To get the last visible row
Dim C 'To get the last column
Dim L 'To get the size of the worksheet
With Worksheets(wrkshtSourceData)
Dim Datadate As Range: Set Datadate = .Rows("1:1").Find(What:="Date_of_Entry", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
Dim Datastatus As Range: Set Datastatus = .Rows("1:1").Find(What:="FinalStatus", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
.Activate
.UsedRange.Sort Key1:=Range(.Columns(Datadate.Column).Address(Rowabsolute:=False)), Order1:=xlDescending, Header:=xlYes
.Range(.Columns(Datastatus.Column).Address(Rowabsolute:=False)).AutoFilter Field:=1, Criteria1:="Approved"
Dim Sht As Worksheet: Set Sht = Sheets.Add 'Store the sheet into a var...
Sht.Name = "Database2"
L = Range("A1").EntireColumn.Rows.Count 'Set the value of L
R = Range(Cells(L, 1), Cells(L, 1)).End(xlUp).Row '... of R
C = Range("A1").End(xlToRight).Column '...and C
Dim OriginRange As Range: Set OriginRange = .Range(Cells(1, 1), Cells(R, C)).SpecialCells(xlCellTypeVisible)
'Here get the range of visible cells and store the range into the var
OriginRange.Copy
Sht.Activate
Sht.Range("A1").PasteSpecial (xlPasteAll)
Application.CutCopyMode = False
.Delete
Application.DisplayAlerts = True
Sht.Name = wrkshtSourceData
Worksheets(wrkshtSourceData).Visible = xlSheetHidden
End With
'#####################code
Application.ScreenUpdating = True
End Sub
I managed to find the answer to this through trial and error. It was simpler than I was making it out to be.
The person who wrote the code forgot to add Selection.AutoFilter in the last section of the code. Updated for your reference between ####.
Thanks a lot to anyone who attempted to resolve this/ posted the answer.
With Worksheets(wrkshtSourceData)
Dim Datadate As Range: Set Datadate = .Rows("1:1").Find(What:="Date_of_Entry", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
Dim Datastatus As Range: Set Datastatus = .Rows("1:1").Find(What:="FinalStatus", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
.Activate
####
Selection.AutoFilter
####
.UsedRange.Sort Key1:=Range(.Columns(Datadate.Column).Address(Rowabsolute:=False)), Order1:=xlDescending, Header:=xlYes
.Range(.Columns(Datastatus.Column).Address(Rowabsolute:=False)).AutoFilter Field:=1, Criteria1:="Approved"
Sheets.Add.Name = "Database2"
.UsedRange.SpecialCells(xlCellTypeVisible).Copy
Worksheets("Database2").Paste
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
Worksheets("Database2").Name = wrkshtSourceData
Worksheets(wrkshtSourceData).Visible = xlSheetHidden
End With
Application.ScreenUpdating = True
End Sub
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
Use Dir to loop over the files in turn
Sub SearchFiles()
Const FOLDER = "C:\Users\xxx\Desktop\xxx\test\"
Dim wb As Workbook, wbSrc As Workbook
Dim ws As Worksheet, wsSrc As Worksheet
Dim sText As String, sFilename As String
Dim cell As Range, rng As Range
Dim n As Long, i As Long, FirstFound As String
sText = TextBox1.Value
' location of search results
Set wb = ThisWorkbook
Set ws = wb.Sheets(1) ' results of search
ws.Cells.Clear
ws.Range("A1:B1") = Array("Search Test = ", sText)
ws.Range("A2:C2") = Array("Address", "Col A", "Col B")
ws.Range("A2:C2").Font.Bold = True
i = 3
' scan all xlsx files in folder
sFilename = Dir(FOLDER & "*.xlsx")
Do While Len(sFilename) > 0
Set wbSrc = Workbooks.Open(FOLDER & sFilename, True, True)
For Each wsSrc In wbSrc.Sheets
n = n + 1
Set rng = wsSrc.Columns(1)
Set cell = rng.Find(What:=sText, _
After:=rng.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
' text found
If Not cell Is Nothing Then
FirstFound = cell.Address
Do ' update sheet
ws.Cells(i, 1) = cell.Address(0, 0, xlA1, True)
ws.Cells(i, 2) = cell
ws.Cells(i, 3) = cell.Offset(0, 1)
i = i + 1
Set cell = rng.FindNext(After:=cell)
' repeat until back where we started
Loop Until FirstFound = cell.Address
End If
Next
wbSrc.Close
sFilename = Dir
Loop
MsgBox n & " sheets scanned", vbInformation
End Sub
I am using the code below to find text in a worksheet then loop through and find the next occurrence until it can't find any new occurrences. But I get a Runtime Error 13; Type Mismatch error when a I run it a second time. It works the first time but generates the error on the second run through the code.
The type mismatch error occurs at the Set currentFind... line
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlSh As Excel.Worksheet
Dim rcell As Range
Dim int_row As Integer
Dim str_platelet_count_address As String
Dim int_firstFind_count As Integer
Dim currentFind As Excel.Range
Dim d As Excel.Range
Dim str_firstFind_address As String
Dim bln_found As Boolean
Dim str_din As String
Dim int_platelet_count As Integer
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.DisplayAlerts = False ' ensures Excel default which is to overwrite files
Set xlWB = xlApp.Workbooks.Open(Me.txt_workday_file, True, False)
Set xlSh = xlWB.ActiveSheet
xlSh.Range("A1").Select
Set currentFind = xlSh.Cells.Find(What:="W2017", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If currentFind Is Nothing Then
MsgBox "Can't find"
GoTo Close_workbook
Else
str_firstFind_address = currentFind.Address
currentFind.Select
End If
Do Until currentFind Is Nothing
int_row = ActiveCell.Row
str_din = ActiveCell.Value
str_platelet_count_address = "J" & int_row - 1
int_platelet_count = Range(str_platelet_count_address).Value
msgbox "Found"
Cells.FindNext(After:=ActiveCell).Activate
If str_firstFind_address = ActiveCell.Address Then
int_firstFind_count = int_firstFind_count + 1
End If
If int_firstFind_count >= 1 Then GoTo Close_workbook
Loop
Close_workbook:
xlWB.Close
xlApp.Quit
Set xlApp = Nothing
Set currentFind = Nothing
Set xlWB = Nothing
Set xlSh = Nothing
DoCmd.SetWarnings False
MsgBox "Done"
End Sub