Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel VBA Selecting Rows

Posted on 2011-02-23
9
Medium Priority
?
186 Views
Last Modified: 2012-05-11
Hello,

Starting at cell A2 I need to go row by row looking for todays date in column A. All columns that have todays date should be selected and copied to another workbook. After that I need to have several columns added and some counted.  Depending on the day there could be 100 records or none. Finally I need to have the file saved as a text, CSV, or XML file.  

Thanks for the help as always.  Please let me know of any questions.
0
Comment
Question by:jvera524
[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
  • 5
  • 4
9 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 34965322
You don't need VBA to perform these task, although if you're doing them over and over, it may make sense.

For one thing, I would turn on filtering and only show today's date.  Then it's easy to select all and copy to another worksheet.
Then it's easy to go to that sheet and sum up the desired columns and count them then save the sheet as CSV.

I guess you could even record a macro with the steps and then you can run the macro every day.
  Should I show you the steps?
0
 

Author Comment

by:jvera524
ID: 34965359
Yes over and over. If it were the same number of records each time then I understand what you stated above, but the number of records varies from day to day. This is why I thought a while or for loop would be needed to scan through column A looking for today's date.
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 2000 total points
ID: 34965590
It looks like this code could do the job, except that it doesn't save at the moment, and assumes that Sheet2 is always blank.

 
Sub ExportDailyData()
    SelectTodaysItems
    CopyToBlankSheet
    AppendTotals
    SaveSheet
End Sub

Private Sub SelectTodaysItems()
    Range("A1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$E$16").AutoFilter Field:=1, Operator:= _
        xlFilterValues, Criteria2:=Array(2, Format(Now(), "mm/dd/yyyy"))
    Range("A1:E16").Select
    Range("A2").Activate
End Sub

Private Sub CopyToBlankSheet()
    Dim strSheetName As String
    strSheetName = "Sheet2"
    
    Selection.Copy
    Sheets(strSheetName).Select
    Range("A1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    
    Range("A1").Select
    Selection.End(xlDown).Select
End Sub

Private Sub AppendTotals()
    Dim iFirstBlankRow As Integer
    
    iFirstBlankRow = Selection.Row + 1
    
    Cells(iFirstBlankRow, 2).FormulaR1C1 = "=SUM(r2c:R[-1]C)"
    Range("B" & iFirstBlankRow).AutoFill Destination:=Range("B" & iFirstBlankRow & ":E" & iFirstBlankRow), Type:=xlFillDefault
    Cells(iFirstBlankRow, 1).FormulaR1C1 = "=COUNT(r2c:R[-1]C)"
End Sub

Private Sub SaveSheet()
' TBD
End Sub

Open in new window

 
to run, paste this into a VB module, then Alt-F8 to see the macros and select ExportDailyData

Make sure you start from the sheet that has the dates.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:jvera524
ID: 34965628
Also I need to change something from above.  Todays date is not important. What I need to grab is the new records added from day to day.  So if yesterday there were 1000 records and today there are 1020 records only the 20 new records should be grabbed.  I was thinking of having the number of records stored in a variable daily and then next day when the new records are added it gets the new total and based on the difference it grabs the new records which are always at the top of spreadsheet starting on row 2.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 34965678
I suggest marking the ones that have been successfully processed then ignoring them

For example, add a new column (inserted into B?) and put an X to indicate that it's completed...this should be done when the macro completes.
0
 

Author Comment

by:jvera524
ID: 34965723
The Private Sub SelectTodaysItems() seems to always assume the number of records is the same.  Am I correct in reading?  If so, it will not work if the number of records changes from day to day and some days there are none.

I understand the most recent suggestion. Thanks
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 34965846
It filters based on today's date, so if you are always running it today, it will always be the same unless new items are added with today's date.
0
 

Author Comment

by:jvera524
ID: 34965891
I apologize for the confusion on my part.  I checked it today. There were new records some of which do not have todays date but I would still want to grab them.

0
 
LVL 22

Expert Comment

by:rspahitz
ID: 34965925
So how do you know which have been processed and which don't ?  Without that info, this task cannot work effectively.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Make the most of your online learning experience.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

718 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