Using Windows XP and Office 07. The below code is in a word macro to extract data from an excel spreadsheet. The value "MyPAS" is first a selection within a word document used as the search value to find in excel. What I can't figure out is how to program it to input a default value if the searched value is not found.
*************START CODE******************
Function CheckAdminName()
On Error GoTo Error_Handler
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim defaultname As Range
Dim startcell As Range
Set xlApp = CreateObject("excel.application")
xlApp.Visible = True
Set xlWB = xlApp.Workbooks.Open("C:\Documents and Settings\neil.west\Desktop\Product Runner PASADMIN.xls")
With xlWB.Worksheets(1)
With xlApp.Cells
.Find(What:=Left(MyPAS, 4), After:=.Cells(2, 3), LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
With xlApp.ActiveCell
.Offset(rowOffset:=0, columnOffset:=2).Select
If Activecell.Value = "" Then
MyPAS = "MPF - "
Else
MyPAS = ActiveCell.Value + " - "
End If
End With
Line1: End With
xlApp.Workbooks.Close
End With
xlApp.Quit
GoTo DriverExit
DriverExit:
Set xlWB = Nothing
Set xlApp = Nothing
Exit Function
Error_Handler:
If Err.Number = 91 Then
MyPAS = xlApp.ActiveCell.Value + " - "
GoTo Line1
End If
End Function
************END CODE*****************
*************START CODE******************
Function CheckAdminName()
On Error GoTo Error_Handler
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim defaultname As Range
Dim startcell As Range
Set xlApp = CreateObject("excel.application")
xlApp.Visible = True
Set xlWB = xlApp.Workbooks.Open("C:\Documents and Settings\neil.west\Desktop\Product Runner PASADMIN.xls")
With xlWB.Worksheets(1)
With xlApp.Cells
.Find(What:=Left(MyPAS, 4), After:=.Cells(2, 3), LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
With xlApp.ActiveCell
.Offset(rowOffset:=0, columnOffset:=2).Select
If Activecell.Value = "" Then
MyPAS = "MPF - "
Else
MyPAS = ActiveCell.Value + " - "
End If
End With
Line1: End With
xlApp.Workbooks.Close
End With
xlApp.Quit
GoTo DriverExit
DriverExit:
Set xlWB = Nothing
Set xlApp = Nothing
Exit Function
Error_Handler:
If Err.Number = 91 Then
MyPAS = xlApp.ActiveCell.Value + " - "
GoTo Line1
End If
End Function
************END CODE*****************