Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 9th, 2008, 04:24 AM
Registered User
Join Date: Sep 2007
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
     iCount = Range("b2", cell).Count
     Range("b2", Cells(1, iCount)).EntireColumn.Delete
 End If
 Exit For
End If

Next cell

End Function

Old July 11th, 2008, 04:56 PM
Friend of Wrox
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts

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

End Function

Old April 5th, 2012, 11:43 AM
Registered User
Join Date: Oct 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default nested for each cell


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:

 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 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!

Old April 5th, 2012, 10:01 PM
Friend of Wrox
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts


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

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

VBA Tips &amp; Tricks (http://www.vbadud.blogspot.com)
Old April 5th, 2012, 10:20 PM
Registered User
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.

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 03: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

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.