jwilson7981
New Member
- Joined
- Dec 11, 2018
- Messages
- 1
I simply need to write a macro and I am very new to this part of it. I'm wanting to be able to locate a name on worksheet 1. Then using that data I need to find and search how many times that person has asked off in another workbook and return that to a cell in worksheet 1. These are 2 different workbooks. ie. I have a list of all my employees in the .xlsm file. I want it to be able to use their names and search a large workbook where I track all my schedules to see how many times they have asked off by locating their name and on that row finding how many times they have the work "OFF". So I will need to search that entire workbook. Can this be done. I can't find any where to do this.
I have the two files I'm working with. I have it where it will find the name I'm looking for but nothing else. And I would like to do this with the entire list of names without having to write an individual Sub for each name. I will paste the current code below.
I have the two files I'm working with. I have it where it will find the name I'm looking for but nothing else. And I would like to do this with the entire list of names without having to write an individual Sub for each name. I will paste the current code below.
Code:
Private Sub Find_Data()
Dim counter As Integer
Dim currentSheet As Integer
Dim notFound As Boolean
Dim yesNo As String
notFound = True
On Error Resume Next
currentSheet = ActiveSheet.Index
'datatoFind = InputBox("Please enter the value to search for")
datatoFind2 = "OFF"
datatoFind = Sheets("Request Off").Range("A3").Value
If datatoFind = "" Then Exit Sub
Workbooks("Schedule.xlsx").Activate
sheetCount = ActiveWorkbook.Sheets.Count
If IsError(CDbl(datatoFind)) = False Then datatoFind = CDbl(datatoFind)
For counter = 1 To sheetCount
Sheets(counter).Activate
Cells.Find(What:=datatoFind, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Cells.Find(What:=datatoFind2, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
If datatoFind2 = True Then
' If InStr(1, ActiveCell.Value, datatoFind) Then
' If HasMoreValues(counter + 1) Then 'Not completing the method and directly entering
' yesNo = MsgBox("Do you want to continue search?", vbYesNo)
' If yesNo = vbNo Then
' notFound = False
' Exit For
' End If
' End If
Sheets(counter).Activate
' End If
Next counter
If notFound Then
MsgBox ("Value not found")
Sheets(currentSheet).Activate
End If
End Sub
'Private Function HasMoreValues(ByVal sheetCounter As Integer) As Boolean
' HasMoreValues = False
' Dim str As String
' For counter = sheetCounter To sheetCount
' Sheets(counter).Activate
' str = Cells.Find(What:=datatoFind, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
' :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
' False, SearchFormat:=False).Value 'Not going further than this i.e. following code is not executed
'If InStr(1, str, datatoFind) Then
' HasMoreValues = True
' Exit For
'End If
'Next counter
'End Function
Last edited by a moderator: