Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 149
Default Is there a FormatReverse Function?

All,

VBE Help for Format: "Returns a Variant (String) containing an expression
formatted according to instructions contained in a format expression." So,
you supply the expression and the format and VBE kicks back your desired
result. Is there a way to go the opposite direction, i.e. you supply the
result and VBE kicks back the format? (A quickly coded conceptual example is
below, which illustrates this idea with a date. Simply run TestDateFormat
and view the Immediate Window for the results).

In other words, think of how NumberFormat works; you can, for example, point
to a range object and apply the NumberFormat property to return (or set) the
number format for the given range. This is great if you are interacting with
the spreadsheet, but not so great if you are working internally within VBE.

Thanks,

Matthew Herbert

Sub TestDateFormat()
Dim strDte As String
Dim dteTest As Date

dteTest = 40109
strDte = DateFormat(CStr(dteTest))
Debug.Print "Ex 1: "; strDte
strDte = "01/22/04"
strDte = DateFormat(strDte)
Debug.Print "Ex 2: "; strDte
End Sub

Function DateFormat(strDate As String) As String
Dim intCnt As Integer
Dim intCntMo As Integer
Dim intCntDy As Integer
Dim intCntYr As Integer
Dim lngPosOne As Long
Dim lngPosTwo As Long
Dim strChr As String * 1
Dim strFormat As String

strChr = "/"
lngPosOne = 1
lngPosOne = InStr(lngPosOne, strDate, strChr, vbTextCompare)
lngPosTwo = InStr(lngPosOne + 1, strDate, strChr, vbTextCompare)

intCntMo = lngPosOne - 1
intCntDy = (lngPosTwo - lngPosOne) - 1
intCntYr = Len(strDate) - lngPosTwo

For intCnt = 1 To intCntMo
strFormat = strFormat & "m"
If intCnt = intCntMo Then
strFormat = strFormat & strChr
End If
Next intCnt

For intCnt = 1 To intCntDy
strFormat = strFormat & "d"
If intCnt = intCntDy Then
strFormat = strFormat & strChr
End If
Next intCnt

For intCnt = 1 To intCntYr
strFormat = strFormat & "y"
Next intCnt

DateFormat = strFormat

End Function

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Function Question: Collect Condition in Dialog Box - But How toInsert into Function Equation? SteveM Excel Programming 1 January 3rd 08 03:45 PM
LINKEDRANGE function - a complement to the PULL function (for getting values from a closed workbook) [email protected] Excel Worksheet Functions 0 September 5th 06 03:44 PM
Excel - User Defined Function Error: This function takes no argume BruceInCalgary Excel Programming 3 August 23rd 06 08:53 PM
Need to open the Function Arguments window from VBA for a user defined function. [email protected] Excel Programming 0 June 20th 06 03:53 PM
Emulate Index/Match combo function w/ VBA custom function Spencer Hutton Excel Worksheet Functions 2 May 2nd 05 05:26 PM


All times are GMT +1. The time now is 03:09 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"