Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Excel Woes | New Users to Excel | |||
Rowliner woes | Excel Discussion (Misc queries) | |||
Indexing Woes | Excel Discussion (Misc queries) | |||
Hyperlink Woes - Please Help | Excel Worksheet Functions | |||
References woes | Excel Programming |