PDA

View Full Version : Delete rows of that contain a specific number.



X2600
11-27-2012, 09:22 AM
Hey guys,

I'm new to the forum here and I'm just getting back into programming. I'm not use to working with excel, so this is pretty challenging. Anyways, here is some background info if you guys choose to help me out with this issue.

Background:
1) I have a database set up with MS access
2) I have a excel spreadsheet that queries the database for specific columns: LASTNAME, FIRSTNAME, COUNTRY, GRAD, TLA, DEPARTED, REMARKS
3) There is criteria on TLA (checkbox) for yes only, a filter for departed for no only (checkbox), and a filter on remarks so that it doesn't contain records that contain info that I don't want.

Perdicament:
1) The GRAD column is the column i'm having trouble with. The query pulls up records with GRAD (graduation) dates for every month. I want it to pull up records of only grad dates of this month. The GRAD column is an integer, not a date, in the format of YYMMDD.

Need to know how to do:
1) Convert the current date to YYMMDD, and have code that compares the YYMM of the current date to the records being pulled up in excel. The rows that don't match get deleted.



If there is an easier way to do this, please let me in on it. Thank you for your time and your help in advance.


p.s. - Here's the query:

SELECT
`IMSO DATA`.LAST,
`IMSO DATA`.FIRST,
`IMSO DATA`.COUNTRY,
`IMSO DATA`.GRAD,
`IMSO DATA`.TLA,
`IMSO DATA`.DEPARTED,
`IMSO DATA`.REMARKS FROM
`IMSO DATA` `IMSO DATA`
WHERE
(`IMSO DATA`.TLA=1)
AND
(`IMSO DATA`.DEPARTED=0)
AND
(`IMSO DATA`.REMARKS Not Like '%KAFB%'
And `IMSO DATA`.REMARKS Not Like '%KEESLER%'
And `IMSO DATA`.REMARKS Not Like '%COUNTRY%')
ORDER BY `IMSO DATA`.GRAD, `IMSO DATA`.COUNTRY, `IMSO DATA`.LAST

GarysStudent
11-27-2012, 11:35 AM
This assumes that the grad dates are in column G:

Sub DateCheck()
Dim strDate As Long, tDate As Long, N As Long, L As Long
strDate = CLng(Format(Date, "yymm"))
MsgBox strDate
N = Cells(Rows.Count, "G").End(xlUp).Row
For L = N To 2 Step -1
tDate = Cells(L, "G").Value
If tDate <> strDate Then
Cells(L, "G").EntireRow.Delete
End If
Next L
End Sub

X2600
11-27-2012, 01:33 PM
Thanks that helps a lot, but when I tried it everything gets deleted. I think it's comparing it to all six numbers. YYMMDD

X2600
11-27-2012, 02:56 PM
NVM, I figured it out, easy fix.

Private Sub Workbook_Activate()
Dim strDate As Long, tDate As Long, N As Long, L As Long
strDate = CLng(Format(Date, "yymm")) & "31"
MsgBox strDate
N = Cells(Rows.Count, "D").End(xlUp).Row
For L = N To 2 Step -1
tDate = Cells(L, "D").Value
If tDate > strDate Then
Cells(L, "D").EntireRow.Delete
End If
Next L
End Sub

GarysStudent
11-27-2012, 03:04 PM
Good job!