Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Find with a variable

Having a few problems with my syntax and would appreciate any help.

I want to write a macro that will find any date I specify in my spreadsheet,
I can enter the date variable using InputBox and I can do a specific find
with the Find command but I don't know how to pass the date variable entered
into the InputBox into the Find command.

--
Regards,
Chris


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Find with a variable

Hi , Chris

Range object has a Find method. That's what you manually use the Find
dialogbox.

Inputbox returns a string that you entered,
you can pass the string to the Find methd.

str=InputBox(message,title)

If Len(str)=0 Then Exit Sub

With Worksheets(1).Range("a1:a500")
Set c = .Find(str, lookin:=xlValues)
If Not c Is Nothing Then
' Do your Processing

End If
End With

Best Regards,

sjoo.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Find with a variable

Hi , Chris

Range object has a Find method. That's what you manually use the Find
dialogbox.

Inputbox returns a string that you entered,
you can pass the string to the Find methd.

str=InputBox(message,title)

If Len(str)=0 Then Exit Sub

With Worksheets(1).Range("a1:a500")
Set c = .Find(str, lookin:=xlValues)
If Not c Is Nothing Then
' Do your Processing

End If
End With

Best Regards,

sjoo.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Find with a variable

Sometimes VBA and dates don't play nice:

Option Explicit
Sub testme01()

Dim myDate As String
Dim FoundCell As Range
Dim RngToSearch As Range

myDate = InputBox(Prompt:="enter a date")
If myDate = "" Then
Exit Sub
End If

If Year(CDate(myDate)) < 1990 _
Or Year(CDate(myDate)) 2010 Then
MsgBox "Please enter a nice date"
Exit Sub
End If

With Worksheets("sheet1")
Set RngToSearch = .UsedRange
With RngToSearch
Set FoundCell = .Cells.Find(what:=CDate(myDate), _
after:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=False)
End With
End With

If FoundCell Is Nothing Then
MsgBox Format(myDate, "mmmm dd, yyyy") & " was not found"
Else
MsgBox "Found it: " & FoundCell.Address(0, 0)
End If

End Sub

If this doesn't work, you could try changing:
..Cells.Find(what:=CDate(myDate), _
to
..Cells.Find(what:=clng(CDate(myDate)), _



Chris wrote:

Having a few problems with my syntax and would appreciate any help.

I want to write a macro that will find any date I specify in my spreadsheet,
I can enter the date variable using InputBox and I can do a specific find
with the Find command but I don't know how to pass the date variable entered
into the InputBox into the Find command.

--
Regards,
Chris


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable Find Joe Murphy[_2_] Excel Discussion (Misc queries) 1 January 7th 09 02:21 PM
Find Max from Variable Range Dan Excel Worksheet Functions 6 February 22nd 08 11:35 PM
Cells.Find with a variable El Bee Excel Discussion (Misc queries) 4 January 28th 08 09:30 PM
Find a variable ben Excel Programming 6 December 1st 04 07:27 PM
Cells.Find error Object variable or With block variable not set Peter[_21_] Excel Programming 2 May 8th 04 02:15 PM


All times are GMT +1. The time now is 02:13 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"