?
Solved

Excel - Macro

Posted on 2000-04-10
4
Medium Priority
?
155 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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
Course of the Month8 days, 17 hours left to enroll

764 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