Setting Date and Count if statements within Macro

twinkle99

Board Regular
Joined
Aug 7, 2005
Messages
240
I have a Macro which imports data from an Access Table (Tbl1mdb) to an Excel Workbook (Book 1 - Sheet1). The Macro runs as expected.

Within the Macro, there are two dates which are hardcoded, an extract from the VBA is below:

"Select 'Tbl1mdb'.compdate, 'Tbl1mdb'.targdate, 'Tbl1mdb.code & "FROM 'C:\mymdb" & "WHERE ('Tbl1mdb.targdate<={ts '2007-05-22 00:00:00'} And 'Tbl1mdb.targdate>={ts '2007-04-29 00:00:00'})"

An Extract of the imported Data in Excel is also shown (columns A - C)
Book1
ABCD
1compdatetargdatecode
225/05/200700:0025/05/200700:00A
318/06/200700:0018/06/200700:00D
415/06/200700:0015/06/200700:00A
520/06/200700:0019/06/200700:00A
622/04/200700:0021/04/200700:00A
721/04/200700:0020/04/200700:00A
812/04/200700:0012/04/200700:00B
918/05/200700:0016/05/200700:00D
1022/05/200700:0019/05/200700:00D
1119/05/200700:0020/05/200700:00D
1212/05/200700:0013/05/200700:00E
1313/05/200700:0013/05/200700:00E
1416/05/200700:0017/05/200700:00A
1528/05/200700:0028/05/200700:00A
Sheet1


At the front of this Macro I would like to have one input box (or Two if one isnt possible) which would ask the user for a 'Startdate' and 'Enddate'.

These dates would then replace the dates in the code above. I am not sure how to set these dates in the code and would welcome any ideas or alternatives.

Also, at the end of the Macro I would like to run 'CountIf' Statements on the imported data in Excel. For example, if Column C (Code) = 'A' then I want the Macro to return a count of rows which meet the Statement to Sheet 2 (A1). I will be running lots of 'Countif' statements but this is an example. I would prefer this to be done in the Macro as the data in Sheet 1 will always change by number of rows depending on the dates. I also want to run a 'Countif' Statement in the Macro which compares compdate (column A) and targdate (column B) and counts if compdate > Targdate. I am really not sure how to write this in the Macro but am sure it can be done.

Many Thanks for listening
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Ok

I have added a few lines of code to the front of the Macro to see if I could set the start and end date as follows:

dim mydate1 As Date, mydate2 As Date
mydate1 = inputbox("enter start date")
mydate2 = inputbox("enter end date")

I then tried to substitute the start and end date into the macro as follows:

"Select 'Tbl1mdb'.compdate, 'Tbl1mdb'.targdate, 'Tbl1mdb.code & "FROM 'C:\mymdb" & "WHERE ('Tbl1mdb.targdate<={ts 'mydate2'} And 'Tbl1mdb.targdate>={ts 'mydate1'})"

So basically I want the data from Access which falls between Start date and end Data.

When I run the Macro it didnt import any data at all!. Is it because the start and end date must be in the same date format as the Access dates in the Macro i.e. 'yyyy-mm-dd hh:mm:ss'?, Or is it more complicated that this?....please, please help, maybe there is an alternative way of doing this but ideally I really only want one piece of code that will work for any period (as entered by the user).

I have managed to get the Countif function to work for column C

[A1] = Application.WorksheetFunction.Countif(Range("C:C"), "A"). This returned the correct result of 7.

I am still having trouble with the Countif compdate > targdate in columns A and B. I tried:

[A2] = Application.WorksheetFunction.Countif(Range("A:A") > Range("B:B")) but this is obviously wrong, any ideas anyone??

Please, please help

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,868
Messages
6,122,005
Members
449,059
Latest member
mtsheetz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top