+ Reply to Thread
Results 1 to 6 of 6

VBA : Date Format()

Hybrid View

  1. #1
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: VBA : Date Format()

    Hi there,

    See if the following version of your code does what you need:

    
    
    Option Explicit
    
    
    Sub weektodata()
    
        Dim filtrodata2 As String
        Dim filtrodata  As Date
        Dim iWeek       As Integer
        Dim iYear       As Integer
        Dim xDStr       As String
        Dim xDWs        As Worksheet
        Dim xWs         As Worksheet
    
        xDStr = "Home"
    
        Set xDWs = Worksheets(xDStr)
    
        iWeek = xDWs.Cells(2, 4).Value
        iYear = 2019
        filtrodata = DateSerial(iYear, 1, ((iWeek - 1) * 7) + 2 - Weekday(DateSerial(iYear, 1, 1)) + 1)
    
        filtrodata2 = Format(filtrodata, "dd.MMM.aaaa")
        MsgBox (filtrodata)
    
    End Sub


    The real problem in your code is that in the following statement:

    
    filtrodata2 = Format(filtrodata, "dd.MMM.aaaa")
    you are attempting to assign a string expression to the variable "filtrodata2" which is declared as a Date variable - the situation is not helped by the fact that the "open-ended" On Error Resume Next statement prevents the "Type Mismatch" error message from giving you an indication as to where the problem is occurring.



    Just for information, the statement:

    
    Dim xWs, xDWs As Worksheet
    declares xWs as a VARIANT, and xlWs as a worksheet - the same applies to your other Dim statements, i.e. the variables whose types are not explicitly declared are implicitly declared as being of type Variant by default.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  2. #2
    Registered User
    Join Date
    04-15-2019
    Location
    italy
    MS-Off Ver
    office 365
    Posts
    3

    Re: VBA : Date Format()

    Hi Greg,

    thanks a lot for the tips.

    I've changed "filtrodata2" to string but now the result is the following:

    Attachment 620259


    Actually what i would as output in filtrodata2 is the date formatted from 03/06/2019 to 03.Jun.2019.

    This is only a part of my code. The contents of filtrodata2 will be used to filter a file.

    ActiveSheet.Range("A3:W3").AutoFilter Field:=18, Criteria1:=filtrodata2

    Attachment 620266

    where gg = dd
    mmm = mmm
    aaaa = yyyy

    Thanks
    Amedeo
    Last edited by rossofoco; 04-16-2019 at 05:51 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Converting from the 1904 date format to the 1900 date format without losing data
    By Patty McJorst in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-01-2020, 12:53 PM
  2. [SOLVED] Converting Date & Time (General format) to Date (Date format)
    By supremenuts in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-22-2017, 12:55 PM
  3. Replies: 4
    Last Post: 04-29-2015, 08:36 AM
  4. Date Format Of User PC Changes Display format date of Excel File after update
    By waihar in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-24-2014, 03:09 AM
  5. Replies: 5
    Last Post: 02-08-2013, 11:06 AM
  6. Need Formula/VBA Code to convert date in String Format to Normal Date format
    By Karthik Sen in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-18-2012, 04:54 AM
  7. Column Range with date Format MM/DD/YYYY independate of sys date Format
    By gaursh in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-18-2011, 03:31 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1