Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 149
Default Test if Cell is Empty or Doesn't Exist

Below I have a function that finds a string within a range in FindCell()
function. My sub GetDate() finds a cell containing a date just fine, except
if the cell is empty or doesn't exist.

How can I add a test to the "FindCell("To Period",
Sheets(1).Cells).Offset(0, 2)" part to display a simple msgbox if the cell
is empty or doesn't exist?


Sub GetDate()

Dim cell As Range, cellOffset As Range
Dim sDateRange As Date

Set cell = FindCell("To Period", Sheets(1).Cells).Offset(0, 2)

sDateRange = CDate(Right(cell.Value, 2) & "/" & (Left(cell.Value,
Len(cell) - 2)))

End Sub

Function FindCell(searchFor As String, _
searchRange As Range) As Range

With searchRange

Set FindCell = .Find(what:=searchFor, After:=.Cells(.Cells.Count), _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=True)

End With

End Function


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Test if Cell is Empty or Doesn't Exist

Set cell = FindCell("To Period", Sheets(1).Cells).Offset(0, 2)
' This line will not only make sure it is not blank, but verify that the
cell entry meets
' the need to be converted to a date - you can modify it to test for any
pattern:
If Cell.Value like "##*##" Then
sDateRange = CDate(Right(cell.Value, 2) & "/" & (Left(cell.Value,
Len(cell) - 2)))
Else
MsgBox "Invalid cell contents",vbExclamation,"ERROR:"
End If


"scott" wrote:

Below I have a function that finds a string within a range in FindCell()
function. My sub GetDate() finds a cell containing a date just fine, except
if the cell is empty or doesn't exist.

How can I add a test to the "FindCell("To Period",
Sheets(1).Cells).Offset(0, 2)" part to display a simple msgbox if the cell
is empty or doesn't exist?


Sub GetDate()

Dim cell As Range, cellOffset As Range
Dim sDateRange As Date

Set cell = FindCell("To Period", Sheets(1).Cells).Offset(0, 2)

sDateRange = CDate(Right(cell.Value, 2) & "/" & (Left(cell.Value,
Len(cell) - 2)))

End Sub

Function FindCell(searchFor As String, _
searchRange As Range) As Range

With searchRange

Set FindCell = .Find(what:=searchFor, After:=.Cells(.Cells.Count), _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=True)

End With

End Function



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 694
Default Test if Cell is Empty or Doesn't Exist

Hi,
Check if a cell was found by comparing to Nothing. And check if it is a date
with IsDate( ). Something like:

Sub GetDate()

Dim cell As Range, cellOffset As Range
Dim sDateRange As Date
Dim sDateString as string '<--- date string
Set cell = FindCell("To Period", Sheets(1).Cells).Offset(0, 2)

if cell is nothing then 'case no cell found, set date to zero
sDateRange=0
else
sDateString = Right(cell.Value, 2) & "/" & (Left(cell.Value,
Len(cell) - 2))
' if is a date then set date to date, else to zero
sDateRange= iif(isdate(sDateString),CDate(sDateString),0)
End if

End Sub

Regards,
Sebastien
"scott" wrote:

Below I have a function that finds a string within a range in FindCell()
function. My sub GetDate() finds a cell containing a date just fine, except
if the cell is empty or doesn't exist.

How can I add a test to the "FindCell("To Period",
Sheets(1).Cells).Offset(0, 2)" part to display a simple msgbox if the cell
is empty or doesn't exist?


Sub GetDate()

Dim cell As Range, cellOffset As Range
Dim sDateRange As Date

Set cell = FindCell("To Period", Sheets(1).Cells).Offset(0, 2)

sDateRange = CDate(Right(cell.Value, 2) & "/" & (Left(cell.Value,
Len(cell) - 2)))

End Sub

Function FindCell(searchFor As String, _
searchRange As Range) As Range

With searchRange

Set FindCell = .Find(what:=searchFor, After:=.Cells(.Cells.Count), _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=True)

End With

End Function



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
IF function to test whether the cell value is equal to empty nest terb Excel Worksheet Functions 1 April 30th 08 06:05 AM
How can I test if a cell is empty? Victor Delta Excel Discussion (Misc queries) 10 August 8th 07 11:22 PM
test expression for empty cell in =SUMIF() fgrose Excel Worksheet Functions 12 July 16th 07 04:52 PM
Better way to test for empty Recordset Tod Excel Programming 1 April 2nd 04 05:19 PM
test if a sheet exist (with the name) ? LoloSoft Excel Programming 4 February 17th 04 02:39 PM


All times are GMT +1. The time now is 05:38 PM.

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

About Us

"It's about Microsoft Excel"