breynolds0431
Active Member
- Joined
- Feb 15, 2013
- Messages
- 303
- Office Version
- 365
- 2016
- Platform
- Windows
Hi. I have error handling set up with GoTo. When I take the error handling out of the procedure, nothing errors out while the procedure runs. When I add the error handling back in, the procedure runs fine. But, I have a msgbox in the error handling and that always displays. So, it seems the VBA is running through the error handling regardless of any error.
There's a lot going on below, but the "On Error" is set right before a workbook.open.
There's a lot going on below, but the "On Error" is set right before a workbook.open.
VBA Code:
Option Explicit
Sub Find()
Dim sh1 As Worksheet, sh2 As Worksheet, wsh As Worksheet, dic As Object
Dim a As Variant, b As Variant, i As Long
If WorksheetFunction.CountA(Range("B14:B43")) = 0 Then
MsgBox "There's no data in the ID column. Please add data."
Exit Sub
Else
Dim ans
ans = MsgBox("This tool may take up to 5 minutes to complete. It's recommended that you do not " & _
"open or attempt to work with any other excel files until the process completes.", vbOKCancel )
If ans = vbCancel Then
Exit Sub
Else
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
.Calculation = xlCalculationManual
.StatusBar = "Finding Data. . . "
End With
Worksheets("MEDataGrab").Range("C14:C43").Select
Selection.TextToColumns Destination:=Range("C14"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(1, 3), TrailingMinusNumbers:=True
On Error GoTo Fail
Workbooks.Open fileName:="\\Crossdept - Former S Drive\HIT DR\MEData\MEDataFiles.xlsb", ReadOnly:=True
Application.StatusBar = "Searching Millions of Records. . ."
ThisWorkbook.Activate
Application.StatusBar = "Compiling Results from Millions of Records. . ."
Set wsh = Worksheets("MEDataGrab")
i = 14
While wsh.Cells(i, 2) <> ""
'Looks in MEData1 tab
With wsh.Cells(i, 11)
.FormulaArray = "=INDEX([MEDataFiles.xlsb]MEData1!C2,MATCH(1,IF(RC[-8]>=[MEDataFiles.xlsb]MEData1!C3," & _
"IF(RC[-8]<=[MEDataFiles.xlsb]MEData1!C4,IF(RC[-9]=[MEDataFiles.xlsb]MEData1!C1,1))),0))"
End With
With wsh.Cells(i, 12)
.FormulaArray = "=INDEX([MEDataFiles.xlsb]MEData1!C3,MATCH(1,IF(RC[-9]>=[MEDataFiles.xlsb]MEData1!C3," & _
"IF(RC[-9]<=[MEDataFiles.xlsb]MEData1!C4,IF(RC[-10]=[MEDataFiles.xlsb]MEData1!C1,1))),0))"
End With
With wsh.Cells(i, 13)
.FormulaArray = "=INDEX([MEDataFiles.xlsb]MEData1!C4,MATCH(1,IF(RC[-10]>=[MEDataFiles.xlsb]MEData1!C3," & _
"IF(RC[-10]<=[MEDataFiles.xlsb]MEData1!C4,IF(RC[-11]=[MEDataFiles.xlsb]MEData1!C1,1))),0))"
End With
'Looks in MEData2 tab
With wsh.Cells(i, 14)
.FormulaArray = "=INDEX([MEDataFiles.xlsb]MEData2!C2,MATCH(1,IF(RC[-11]>=[MEDataFiles.xlsb]MEData2!C3," & _
"IF(RC[-11]<=[MEDataFiles.xlsb]MEData2!C4,IF(RC[-12]=[MEDataFiles.xlsb]MEData2!C1,1))),0))"
End With
With wsh.Cells(i, 15)
.FormulaArray = "=INDEX([MEDataFiles.xlsb]MEData2!C3,MATCH(1,IF(RC[-12]>=[MEDataFiles.xlsb]MEData2!C3," & _
"IF(RC[-12]<=[MEDataFiles.xlsb]MEData2!C4,IF(RC[-13]=[MEDataFiles.xlsb]MEData2!C1,1))),0))"
End With
With wsh.Cells(i, 16)
.FormulaArray = "=INDEX([MEDataFiles.xlsb]MEData2!C4,MATCH(1,IF(RC[-13]>=[MEDataFiles.xlsb]MEData2!C3," & _
"IF(RC[-13]<=[MEDataFiles.xlsb]MEData2!C4,IF(RC[-14]=[MEDataFiles.xlsb]MEData2!C1,1))),0))"
End With
'Looks in MEData3 tab
With wsh.Cells(i, 17)
.FormulaArray = "=INDEX([MEDataFiles.xlsb]MEData3!C2,MATCH(1,IF(RC[-14]>=[MEDataFiles.xlsb]MEData3!C3," & _
"IF(RC[-14]<=[MEDataFiles.xlsb]MEData3!C4,IF(RC[-15]=[MEDataFiles.xlsb]MEData3!C1,1))),0))"
End With
With wsh.Cells(i, 18)
.FormulaArray = "=INDEX([MEDataFiles.xlsb]MEData3!C3,MATCH(1,IF(RC[-15]>=[MEDataFiles.xlsb]MEData3!C3," & _
"IF(RC[-15]<=[MEDataFiles.xlsb]MEData3!C4,IF(RC[-16]=[MEDataFiles.xlsb]MEData3!C1,1))),0))"
End With
With wsh.Cells(i, 19)
.FormulaArray = "=INDEX([MEDataFiles.xlsb]MEData3!C4,MATCH(1,IF(RC[-16]>=[MEDataFiles.xlsb]MEData3!C3," & _
"IF(RC[-16]<=[MEDataFiles.xlsb]MEData3!C4,IF(RC[-17]=[MEDataFiles.xlsb]MEData3!C1,1))),0))"
End With
'Looks in MEData4 tab
With wsh.Cells(i, 20)
.FormulaArray = "=INDEX([MEDataFiles.xlsb]MEData4!C2,MATCH(1,IF(RC[-17]>=[MEDataFiles.xlsb]MEData4!C3," & _
"IF(RC[-17]<=[MEDataFiles.xlsb]MEData4!C4,IF(RC[-18]=[MEDataFiles.xlsb]MEData4!C1,1))),0))"
End With
With wsh.Cells(i, 21)
.FormulaArray = "=INDEX([MEDataFiles.xlsb]MEData4!C3,MATCH(1,IF(RC[-18]>=[MEDataFiles.xlsb]MEData4!C3," & _
"IF(RC[-18]<=[MEDataFiles.xlsb]MEData4!C4,IF(RC[-19]=[MEDataFiles.xlsb]MEData4!C1,1))),0))"
End With
With wsh.Cells(i, 22)
.FormulaArray = "=INDEX([MEDataFiles.xlsb]MEData4!C4,MATCH(1,IF(RC[-19]>=[MEDataFiles.xlsb]MEData4!C3," & _
"IF(RC[-19]<=[MEDataFiles.xlsb]MEData4!C4,IF(RC[-20]=[MEDataFiles.xlsb]MEData4!C1,1))),0))"
End With
'Looks in MEData5 tab
With wsh.Cells(i, 23)
.FormulaArray = "=INDEX([MEDataFiles.xlsb]MEData5!C2,MATCH(1,IF(RC[-20]>=[MEDataFiles.xlsb]MEData5!C3," & _
"IF(RC[-20]<=[MEDataFiles.xlsb]MEData5!C4,IF(RC[-21]=[MEDataFiles.xlsb]MEData5!C1,1))),0))"
End With
With wsh.Cells(i, 24)
.FormulaArray = "=INDEX([MEDataFiles.xlsb]MEData5!C3,MATCH(1,IF(RC[-21]>=[MEDataFiles.xlsb]MEData5!C3," & _
"IF(RC[-21]<=[MEDataFiles.xlsb]MEData5!C4,IF(RC[-22]=[MEDataFiles.xlsb]MEData5!C1,1))),0))"
End With
With wsh.Cells(i, 25)
.FormulaArray = "=INDEX([MEDataFiles.xlsb]MEData5!C4,MATCH(1,IF(RC[-22]>=[MEDataFiles.xlsb]MEData5!C3," & _
"IF(RC[-22]<=[MEDataFiles.xlsb]MEData5!C4,IF(RC[-23]=[MEDataFiles.xlsb]MEData5!C1,1))),0))"
End With
'Adds formulas to columns D, E, F, and G
wsh.Cells(i, 4).FormulaR1C1 = _
"=IFERROR(RC[7],IFERROR(RC[10],IFERROR(RC[13],IFERROR(RC[16],IFERROR(RC[19],""No Data"")))))"
wsh.Cells(i, 5).FormulaR1C1 = _
"=IFERROR(RC[7],IFERROR(RC[10],IFERROR(RC[13],IFERROR(RC[16],IFERROR(RC[19],""No Data"")))))"
wsh.Cells(i, 6).FormulaR1C1 = _
"=IFERROR(RC[7],IFERROR(RC[10],IFERROR(RC[13],IFERROR(RC[16],IFERROR(RC[19],""No Data"")))))"
wsh.Cells(i, 7).FormulaR1C1 = _
"=IF(OR(RC[-5]="""",RC[-3]=""Not Found""),"""",IF(LEN(RC[-3])>2,iferror(VLOOKUP(NUMBERVALUE(LEFT(RC[-3],2))," & _
"Funding,3,FALSE),VLOOKUP(LEFT(RC[-3],2),Funding,3,FALSE)),VLOOKUP(RC[-3],Funding,3,FALSE)))"
i = i + 1
Wend
Range("D14:Y43").Select
Selection.Copy
Application.StatusBar = False
Range("D14").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("K14:Y43").ClearContents
Workbooks("MEDataFiles.xlsb").Close SaveChanges:=False
Range("B14").Select
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
.Calculation = xlCalculationAutomatic
.StatusBar = ""
.Speech.Speak ("Search has now completed!")
End With
End If
End If
Fail:
Call reset
End Sub
Sub reset()
MsgBox "Something went wrong"
If AlreadyOpen("\\Crossdept - Former S Drive\HIT DR\MEData\MEDataFiles.xlsb") Then
Application.DisplayAlerts = False
Workbooks("MEDataFiles.xlsb").Close SaveChanges:=False
Else
End If
ThisWorkbook.Sheets("MEDataGrab").Select
Range("B14").Select
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
.Calculation = xlCalculationAutomatic
.StatusBar = False
End With
End Sub
Function AlreadyOpen(sFname As String) As Boolean
Dim wkbook As Workbook
On Error Resume Next
Set wkbook = Workbooks(sFname)
AlreadyOpen = Not wkbook Is Nothing
Set wkbook = Nothing
End Function