Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Function to test if a cell is blank 2

Status
Not open for further replies.

BajanPOET

Programmer
Joined
Jul 12, 2002
Messages
194
Location
BB
Is there any VBA function to test if a cell is blank?

What? Who? ME????
 
You may try something like this:
If Trim(yourCell.Value & "") = "" Then

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Why reinvent the wheel?

Just use the existing ISBLANK() function.
 
I tried using the Isblank() but I couldn't get it to work. It was telling me the equivalent of 'that function doesn't exist.'

The Trim() works, but how would I use the Isblank()?

What? Who? ME????
 




What do you mean by BLANK?

VALUE is ""?

What about if there's a FORMULA in the cell? It may APPEAR to be blank, but it's not.
[tt]
=IF(A1="",TRUE,FALSE)
=IF(ISBLANK(A1),TRUE,FALSE)
[/tt]
can give different results.

Skip,

[glasses] [red][/red]
[tongue]
 
I was writing code for my Workbook_Open stub.

IsBlank() wasn't supported... I wanted to test for E3=""... the Trim() worked fine - like this:

Code:
Range("E3").Select
If Trim(ActiveCell.Value) = "" Then
   VBAProject.ThisWorkbook.Worksheets("LPO").Range("E3").Value = Date
End if

What? Who? ME????
 




Select is not necessary....
Code:
'Range("E3").Select  -- WHAT WORKBOOK & SHEET IS THIS CELL ON???
If Trim(WHATWORKBOOK.WHATSHEET.Range("E3").Value) = "" Then
   VBAProject.ThisWorkbook.Worksheets("LPO").Range("E3").Value = Date
End if

Skip,

[glasses] [red][/red]
[tongue]
 
Any excel function can be used in excel VBA by using the worksheetfunction property.

answer = Application.WorksheetFunction.IsBlank(myCell)
 
Thanks mintjulep... that's what I wanted... I'll try it.


What? Who? ME????
 
I tried that, but actually I get the message that "Object doesn't support this property or method." when I try to use that code snippet.

What? Who? ME????
 
Some worksheet functions are not usable in VBA. Use 'IsEmpty' in VBA.
 


CA,

Sorry, but the IsEmpty function in VB returns whether a variable has been initialized or not.

Skip,

[glasses] [red][/red]
[tongue]
 
Skip,
The original question was:
Is there any VBA function to test if a cell is blank?
The IsEmpty function, when used on a cell, will return False if there is either a formula or data in the cell.
 
CBA,
Try it on an empty cell vs a cell with something in it: it always returns false.

BajanPOET, I'd go with the earlier-suggested

Code:
If Trim(Range("E3").value) = "" Then
    '...
End If


[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ181-2886 before posting.
 
I will go back to my Range.Select... that worked...

I can't find IsEmpty as a function in this implementation of VBA, either :-(

Actually, regarding this code snippet (in order to remove the unnecessary Range.Select)

Code:
If Trim(WHATWORKBOOK.WHATSHEET.Range("E3").Value) = "" Then
   VBAProject.ThisWorkbook.Worksheets("LPO").Range("E3").Value = Date
End if

What would be the Workbook and the Worksheet I would need? I named that particular sheet "LPO" and the name of the *.xlt file is PurchaseOrders.xlt. So would it actually be

Code:
If Trim(PurchaseOrders.LPO.Range("E3").Value) = "" Then

[ponder]

What? Who? ME????
 
anotherhiggins,

I did and it works.
Code:
Sub aTest()
If IsEmpty(Sheet1.Range("A1")) = True Then MsgBox "A1 is empty"
End Sub

 
With ThisWorkbook.Worksheets("LPO")
If Trim(.Range("E3").Value) = "" Then .Range("E3").Value = Date
End With

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV, that worked...

What? Who? ME????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top