[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Excel - Macro

Posted on 2000-04-10
4
Medium Priority
?
159 Views
Last Modified: 2010-05-02
I am trying to use selection.autofilter in my sub procedure. I have declared two variables newdate and newdate1 as variants and formatted them as "dd/mm/yyyy" My problem comes when I try to use them in the selection.autofilter the first variable is inserted in criteria1 which produces the correct result "dd/mm/yyyy" however, the second variable which I put into criteria2 comes up with the DateValue instead of the actual typed in date. Why is criteria2 not appearing in the format "dd/mm/yyyy" when they are both declared and processed in the same fashion?

Thanks
0
Comment
Question by:Nissarg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 22

Expert Comment

by:ture
ID: 2700900
Nissarq,

If I'm not wrong, VBA assumes that you use mm/dd/yyyy (not dd/mm/yyyy). That is the cause of your problems. To avoid misunderstandings, you can use YYYY-MM-DD format or even better DateSerial(y,m,d), as shown in this example:

Sub AutoFilterTest()
  'Declare variables
  Dim ws As Worksheet
  Dim d1 As Variant
  Dim d2 As Variant

  'Set values of variables
  Set ws = ActiveWorkbook.Sheets("Sheet2")
  d1 = DateSerial(1999, 9, 1)
  d2 = DateSerial(1999, 12, 31)

  'Make sure that AutoFilter is turned off
  ws.AutoFilterMode = False

  'Use AutoFilter to display records from d1 to d2
  ws.Range("A1").AutoFilter _
    Field:=1, _
    Criteria1:=">=" & d1, _
    Operator:=xlAnd, _
    Criteria2:="<=" & d2
End Sub

Ture Magnusson
Karlstad, Sweden
0
 

Author Comment

by:Nissarg
ID: 2721739
Hi Ture

Thanks for your response. This client insists on having the dd/mm/yyyy format. When I run my macro on NT Server it works when the locale settings are dd/mm/yyyy. Is there no way to change this on Windows 98. I have tried the Regional Settings - doesn't help.

Thanks
Nissar
0
 
LVL 22

Accepted Solution

by:
ture earned 150 total points
ID: 2721833
Nissar,

It is ok that your client wants dd/mm/yyyy date format. You'll just have to make sure that VBA interprets the date correctly.

If I am not mistaken, the CDATE function will convert a text string to a date, using the windows regional settings. Try this little prrocedure to check out the CDATE function and see if it reports the correct month.

Sub TestCDate()
  Dim t As String
  Dim d As Date
  t = "01/07/2000"
  d = CDate(t)
  MsgBox Month(d)
End Sub

/Ture
0
 

Author Comment

by:Nissarg
ID: 2768985
Have not run it yet, but I can see what you are saying.

Thanks Ture

Nissar
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question