Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default For Each Next woes

Good day!
I'm having difficulty with the proper syntax for the following operation:

Dim AskDate as Date
Dim Target as Variant

For Each Target in [B17:AK48]
AskDate = InputBox("Enter Date")
If Target = AskDate then
Target . Select
Exit For
Else
Endif
Next Target


Here is what I am trying to do. The user inputs a date, the macro searches
the defined array for that user-input date. If the date is found, the
selection becomes that particular cell. If the date is not found, the user
is prompted to enter a date in again. This process repeats until an input
date matches a date within the given array. I've messed with syntax for a
couple hours, so I'm wondering if someone would be kind enough to give me a
push in the right direction. Thank you for your time!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default For Each Next woes

Try taking the "ask the user the date" part out of the For Each loop, i.e.
ask the user the date first, then loop through the cells looking for a match.
Something like this:

Sub FindDate()

Do While True
AskDate = InputBox("Enter Date")
For Each Target in [B17:AK48]
If Target = AskDate then
Target . Select
Exit Sub
Endif
Next Target
Loop

End Sub

"AZ Analog" wrote:

Good day!
I'm having difficulty with the proper syntax for the following operation:

Dim AskDate as Date
Dim Target as Variant

For Each Target in [B17:AK48]
AskDate = InputBox("Enter Date")
If Target = AskDate then
Target . Select
Exit For
Else
Endif
Next Target


Here is what I am trying to do. The user inputs a date, the macro searches
the defined array for that user-input date. If the date is found, the
selection becomes that particular cell. If the date is not found, the user
is prompted to enter a date in again. This process repeats until an input
date matches a date within the given array. I've messed with syntax for a
couple hours, so I'm wondering if someone would be kind enough to give me a
push in the right direction. Thank you for your time!

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default For Each Next woes

Sub aa()
Dim rng As Range
Dim Target As Range
Dim AskDate As String
Set rng = Range("B17:AK48")
Do
AskDate = InputBox("Enter Date")
If AskDate = "" Then Exit Sub
If Not IsDate(AskDate) Then Exit Sub
MsgBox CDate(AskDate), Application.CountIf(rng, AskDate)
Loop Until Application.CountIf(rng, CDate(AskDate)) 0
For Each Target In rng
If Int(Target.Value2) = CLng(CDate(AskDate)) Then
Target.Select
Exit For
End If
Next

End Sub

--
Regards,
Tom Ogilvy




"AZ Analog" wrote in message
...
Good day!
I'm having difficulty with the proper syntax for the following operation:

Dim AskDate as Date
Dim Target as Variant

For Each Target in [B17:AK48]
AskDate = InputBox("Enter Date")
If Target = AskDate then
Target . Select
Exit For
Else
Endif
Next Target


Here is what I am trying to do. The user inputs a date, the macro

searches
the defined array for that user-input date. If the date is found, the
selection becomes that particular cell. If the date is not found, the

user
is prompted to enter a date in again. This process repeats until an input
date matches a date within the given array. I've messed with syntax for a
couple hours, so I'm wondering if someone would be kind enough to give me

a
push in the right direction. Thank you for your time!



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default For Each Next woes

Looks like Tom beat me to the reply, but I should add that I had
problems running it when using "Target" as a variable name. Changing
the variable name to "c" resolved the problem:


Sub FindDate()

Dim inputText As String ' the input string for the date
Dim AskDate As Date ' the input string converted to a Date type
Dim c As Variant ' current cell in the For Each range

inputText = InputBox("Enter Date")
If IsDate(inputText) Then
AskDate = CDate(inputText)
Else
MsgBox inputText & " is not a valid date", vbExclamation, "Invalid
Date"
Exit Sub
End If

For Each c In ActiveSheet.UsedRange ' changed from "B17:AK48"
If IsDate(c.Value) Then
If c.Value = AskDate Then
c.Select
Exit Sub
End If
End If
Next

MsgBox "Matching date not found"

End Sub


--Nick

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default For Each Next woes

Thank you all for your help! You're my heros.

"Nick Hebb" wrote:

Looks like Tom beat me to the reply, but I should add that I had
problems running it when using "Target" as a variable name. Changing
the variable name to "c" resolved the problem:


Sub FindDate()

Dim inputText As String ' the input string for the date
Dim AskDate As Date ' the input string converted to a Date type
Dim c As Variant ' current cell in the For Each range

inputText = InputBox("Enter Date")
If IsDate(inputText) Then
AskDate = CDate(inputText)
Else
MsgBox inputText & " is not a valid date", vbExclamation, "Invalid
Date"
Exit Sub
End If

For Each c In ActiveSheet.UsedRange ' changed from "B17:AK48"
If IsDate(c.Value) Then
If c.Value = AskDate Then
c.Select
Exit Sub
End If
End If
Next

MsgBox "Matching date not found"

End Sub


--Nick


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
Excel Woes Larry New Users to Excel 1 August 22nd 06 04:22 PM
Rowliner woes Kim K Excel Discussion (Misc queries) 2 August 4th 06 03:20 PM
Indexing Woes Rianne Excel Discussion (Misc queries) 5 July 11th 06 09:24 PM
Hyperlink Woes - Please Help Randy Excel Worksheet Functions 1 June 27th 06 02:07 PM
References woes WillRn Excel Programming 5 March 30th 05 10:41 PM


All times are GMT +1. The time now is 04:00 AM.

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"