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

July 9th, 2008, 05:24 AM
|
|
Registered User
|
|
Join Date: Sep 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

July 11th, 2008, 05:56 PM
|
|
Friend of Wrox
|
|
Join Date: Feb 2007
Location: Davenport, IA, USA.
Posts: 150
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
IsDate_False:
End Function
--------------------------------------
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |