Object Required

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am using this code below to check to see if a file exists in a directory as specified by a user.

Rich (BB code):
Sub find_file()
  Dim sFile1 As String
  If Target.Address <> "$E$16" Then Exit Sub
  sFile1 = "E:\SportsOps 2009\SportsOps 2009\Data\" & Format(Target, "d-mmm-yy") & ".xls"
  If Dir(sFile1) = "" Then
    MsgBox "Error"
    Exit Sub
  End If
MsgBox "OK."

End Sub

I am getting an error ("Object Required.") from the line highlighted in red.

As always, any help would be welcomed.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
where is the value of the object 'target' defined in this code? Is it a variable you're meant to be passing?
 
Upvote 0
Hmmm ... Target is a variable based on the cell value of E16.
I'm kinda lost.
 
Upvote 0
OK excelr8r ...

I gotta plea ignorance (long day). I'm uncertian as to what you mean by defining it.

Jenn
 
Upvote 0
okay, the variable target has to equal something, in this case it's going to have to represent a range

like

set target = range("A1:N77")

for example

Your code doesn't appear to tell VB what the value of target is i.e there's no definition.

My original question assumed you'd 'defined' it elsewhere and were failing to pass it to your subroutine
 
Upvote 0
Ok ... thats what I kinda thought you were going with this.
I have re-written the code with your suggestion.

Code:
Sub find_file()
  Dim sFile1 As String
  Dim target As Date
  target = Range("$E$16")
  If target.Address <> "$E$16" Then Exit Sub
  sFile1 = "E:\SportsOps 2009\SportsOps 2009\Data\" & Format(target, "d-mmm-yy") & ".xls"
  If Dir(sFile1) = "" Then
    MsgBox "Error"
    Exit Sub
  End If
    MsgBox "OK."

End Sub

Now receiving compilation error "Invalid qualifier." from the reference to 'target' in
Code:
If target.Address <> "$E$16" Then Exit Sub
.

Jenn
 
Upvote 0
If you're hard coding target to equal a given range, I'm assuming you always want the code to execute.

Why not test for blank or no data in your range

Code:
Sub find_file()
  Dim sFile1 As String
  Dim target As Date
  Set target = Range("$E$16")
  If target.Value = "" Then Exit Sub
  sFile1 = "E:\SportsOps 2009\SportsOps 2009\Data\" & Format(target, "d-mmm-yy") & ".xls"
  If Dir(sFile1) = "" Then
    MsgBox "Error"
    Exit Sub
  End If
    MsgBox "OK."

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,025
Members
448,939
Latest member
Leon Leenders

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top