Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 623
  • Last Modified:

Macro to pull out specific data over multiple sheets

Table example
Hi all,

Imagine I have a table like the attached - and several tables across multiple tabs.

In the first tab - I can have a macro button called say - 08/11/2013

Clicking this would create a new tab, labelled 08/11/2013, and would contain any line from tables such as the attached, whose dates are covering the period of the 8th November.

Either starting, in progress (for example the 8th falls between the Start and End dates) or ending.

Is there a good macro way of doing this? autofiltering creates a bit of a mess of it!
0
HuyenDoThanh
Asked:
HuyenDoThanh
  • 2
  • 2
1 Solution
 
Robberbaron (robr)Commented:
yes can be done...

1/ please post a small 10 line sample of data.
2/ how would we know what to put on the button.  do you want this selected or typed into call.  eg would be better to a have a button label <Process Date> with a cell holding desired date.

3/ how would the macro know what sheets to interogate ?  (each table is on a separate sheet ?)  
4/ how are the sheets named?  is the datatable always in the same location on the sheet ?
0
 
HuyenDoThanhAuthor Commented:
Okay I apologise, I was posting I guess for a general rather than specific solution and I then I could fit that for my purpose exactly.

https://dl.dropboxusercontent.com/u/15096766/Example.xlsm

I've attached an example.

So in the cutover tab, clicking on one of those dates, say the 09/11/2013 (i've made it a shape so you can assign macros to it) - will go through each sheet (here I have environments and data migration as examples with 10 lines each) and finally generate a new table/sheet.

The new sheet will be named 09/11/2013, and will contain a table listed all the Activities that are starting, running, or ending on the 9th.

Does that help?

There will be more sheets than just two, but they will be static once there.
0
 
Robberbaron (robr)Commented:
ok. try this...

1/ sheet names cant use "/" character. so i use -
2/ I also added "cutover" to the front of name to make it easy to tell what sheets to process.  ie all not "cutover"
3/ I also use a text month to remove any possible confusion regarding date.
4/ I also used a cell to hold target date, and a button to process. because it was easier for me....

there is no indication on the output where the activity came from., i guess you have other means.
Sub ProcessCutover()
    Dim checkdate As Date
    Dim cursheet As Worksheet, outputSheet As Worksheet
    Dim datacell As Range, outputrow As Range
    
    'get the date
    checkdate = Range("CutoverDate")
    
    'create new sheet. check not exist
    Set outputSheet = ActiveWorkbook.Sheets.Add(After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count))
    On Error Resume Next
    outputSheet.Name = "Cutover-" & Format(checkdate, "dd-mmm-yyyy")
    If Err.Number > 0 Then
        'there is duplicate name
        MsgBox "Duplicate named sheet", vbAbortRetryIgnore
        'what to do now ?????
    End If
    
    Set outputrow = outputSheet.Range("A1")
    
    'for each sheet not a dated one.
    For Each cursheet In ActiveWorkbook.Sheets
        If InStr(cursheet.Name, "Cutover") = 0 Then
            Set datacell = cursheet.Range("A2")
            
            'for each row of data
            Do While datacell.Value <> ""
    
              'if checkdate is in range
              If datacell.Offset(0, 1).Value <= checkdate And datacell.Offset(0, 2).Value >= checkdate Then
                 'copy row to output
                 datacell.EntireRow.Copy outputrow
                 Set outputrow = outputrow.Offset(1, 0)
              End If
              'move to next data row
              Set datacell = datacell.Offset(1, 0)
           Loop  'next row
       End If
    Next cursheet
End Sub

Open in new window

Example1a.xlsm
0
 
HuyenDoThanhAuthor Commented:
That is perfect, thank you very much.

I added in some stuff at the end to give it the same format as the other sheets - with a row of headings etc and to autofit the cells so it looks great now.

I just need to replicate the macro for two months of dates now, woo...

Either way it works great so thanks for the help.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now