p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > Microsoft Office > Excel VBA > Excel VBA
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.

Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 9th, 2008, 05:24 AM
Registered User
 
Join Date: Sep 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default cell is empty while using For Each cell In Range

I've got a function which check for a particular date and then format columns around it. It was previously working when I use it within a Macro and open the file I want to work on manually and manually execute the Macro. However, when I move the code to a button and try to have VBA to automatically open the files with a folder and execute the existing formating code, it's not working anymore. The problem exist within the function below. The variable "cell" appears to be empty for some reason? Can any offer any help?


Function FormatSplit(sDate As String, wb As Workbook, Optional splitLeft As Boolean)


For Each cell In Range(Range("a2"), Range("IV2").End(xlToLeft))

If Format(cell.Value, "dd/mm/yyyy") = sDate Then
 If splitLeft = True Then
     Range(cell, Cells(1, Columns.Count)).EntireColumn.Delete
    Else
     iCount = Range("b2", cell).Count
     Range("b2", Cells(1, iCount)).EntireColumn.Delete
 End If
 Exit For
End If

Next cell


End Function

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old July 11th, 2008, 05:56 PM
Friend of Wrox
Points: 474, Level: 7
Points: 474, Level: 7 Points: 474, Level: 7 Points: 474, Level: 7
Activity: 2%
Activity: 2% Activity: 2% Activity: 2%
 
Join Date: Feb 2007
Location: Davenport, IA, USA.
Posts: 150
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Try some data validation to make sure the cell is in date format first.
If certain it looks like a date, the system may not be seeing it as a date for whatever reason.

IsDate will tell you if the value is a date or not. cDate will turn it into a date if it is.
Trim will remove padding around the date.

Does this help any?

If you want to change the cell to a date if possible, create a function to change the date using cDate that will return a date if a good date is passed in or false if not... something like the function below:
--------------------------------------
Private Function ToDate(sPassed As String) As Variant

  Dim dDateToReturn As Date
  ToDate = False
  On Error GoTo IsDate_False
    dDateToReturn = CDate(sPassed)
    ToDate = dDateToReturn
  On Error Resume Next
IsDate_False:

End Function
--------------------------------------

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
[2005] GridView - empty cell shows no border snufse ASP.NET 2.0 Basics 2 July 29th, 2008 03:49 PM
Name a range simply for each third cell wapfu Excel VBA 2 December 6th, 2006 03:04 PM
I need to refer a cell within a cell like =RC[ RC2 chakravarthi_os Excel VBA 1 September 24th, 2006 09:19 AM
Finding Adjacent Empty Cell keithd Excel VBA 7 July 19th, 2006 07:20 AM
Lose cell Text when editing cell in VSFlexGrid 6 bobcratchet VB How-To 0 July 30th, 2004 10:32 AM



All times are GMT -4. The time now is 12:26 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc