Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register
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 Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 9th, 2008, 04: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

Reply With Quote
  #2 (permalink)  
Old July 11th, 2008, 04:56 PM
Friend of Wrox
Points: 513, Level: 8
Points: 513, Level: 8 Points: 513, Level: 8 Points: 513, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2007
Location: Davenport, IA, USA.
Posts: 163
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
--------------------------------------

Reply With Quote
  #3 (permalink)  
Old April 5th, 2012, 11:43 AM
Registered User
Points: 18, Level: 1
Points: 18, Level: 1 Points: 18, Level: 1 Points: 18, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default nested for each cell

Hi,

I have a not-so-related question regarding nested for each cell to ask (as not being able to open a new post). I am running a project, part of the code is like this:

Code:
 
 For Each cell In Range("i17:i" & x)
  If cell.Value > 0 Then
  bp = cell.Offset(0, -2)
  r = cell.Row
        For Each cell In Range(Cells(r + 1, 7), Cells(x, 7))
         t = t + cell.Value - bp
        If t >= bp * 0.15 Or t <= bp * (-0.1) Then
        Range("m1") = t
        Exit For
        End If
        Next
Next cell
Range("n1") = t
I know the problem lies with the 2 nested "For Each cell" clauses, but just couldn't solve it. Can anyone please help?

Thanks a lot!

KH
Reply With Quote
  #4 (permalink)  
Old April 5th, 2012, 10:01 PM
Friend of Wrox
Points: 2,950, Level: 22
Points: 2,950, Level: 22 Points: 2,950, Level: 22 Points: 2,950, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
Posts: 788
Thanks: 1
Thanked 51 Times in 47 Posts
Default

Hi

It is tough to comprehend. The outer loop is for Column I and the inner one is for G ..

It is better if you can explain what you are trying to do .. this piece of code should be easy for you as you have writtent it

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips &amp; Tricks (http://www.vbadud.blogspot.com)
Reply With Quote
  #5 (permalink)  
Old April 5th, 2012, 10:20 PM
Registered User
Points: 18, Level: 1
Points: 18, Level: 1 Points: 18, Level: 1 Points: 18, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default re:nested for each cell

What I am trying to do is to sum the accumulative differences at column G subject to certain constraints.

I think it doesn't matter, I am just asking if there is any way to avoid using "For each cell" twice.

Thanks for your reply.
Reply With Quote
Reply


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 02:49 PM
Name a range simply for each third cell wapfu Excel VBA 2 December 6th, 2006 02:04 PM
I need to refer a cell within a cell like =RC[ RC2 chakravarthi_os Excel VBA 1 September 24th, 2006 08:19 AM
Finding Adjacent Empty Cell keithd Excel VBA 7 July 19th, 2006 06:20 AM
Lose cell Text when editing cell in VSFlexGrid 6 bobcratchet VB How-To 0 July 30th, 2004 09:32 AM



All times are GMT -4. The time now is 11:32 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.