+ Reply to Thread
Results 1 to 6 of 6

Thread: Searching Through Ranges Then Coppying Data From One Sheet to Another

  1. #1
    Registered User
    Join Date
    01-21-2012
    Location
    Leeds, England
    MS-Off Ver
    Excel 2007 + Excel 2010
    Posts
    3

    Searching Through Ranges Then Coppying Data From One Sheet to Another

    Hi All,

    I have been trying to figure out a way of copying some specific data from one excel spreadsheet to another. I have attached the spreadsheet showing the data that i am wanting to copy across.

    What i am wanting to achieve is to copy the filename and data about the creation dates and modification dates of a database. The tricky part is how to take the first file revision i.e. 1582 - Spool Installation - 001.db taking the date created and time created and placing this in the new sheet. Then reading down the revision numbers i.e. 1582 - Spool Installation - 008.db and taking the date modified and time modified, putting them into the new sheet.

    I am wanting to make this into a macro so that i dont have to keep copying the data across, which is a very laborious task especially when there are around 100 databases to search through.

    I have a little knowledge about VB.Net but im not too familiar with VBA for Excel. Any help would be very much appreciated.

    Cheers,

    Dan


    Test Database Sheet.xls
    Last edited by DannyGuest88; 01-23-2012 at 06:25 AM.

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,228

    Re: Searching Through Ranges Then Coppying Data From One Sheet to Another

    This macro will do it without looping:
    Option Explicit
    
    Sub DataExtract()
    Dim LR As Long
    
    Application.ScreenUpdating = False
    LR = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    Sheets("Sheet1").Range("F2:F" & LR).Offset(, 1).FormulaR1C1 = _
            "=IF(RIGHT(RC1,7)=""0001.db"", R[-1]C7+1, R[-1]C7)"
    
    With Sheets("Sheet2")
        On Error Resume Next
        .UsedRange.Cells.Clear
        
        .Range("A1:E1").Value = [{"Filename","Date Created","Time Created","Date Modified","Time Modified"}]
        With .Range("A2:A" & LR)
            .FormulaR1C1 = "=INDEX(Sheet1!C1, MATCH(ROW(R[-1]C1), Sheet1!C7, 0))"
            .Value = .Value
            .SpecialCells(xlConstants, xlErrors).ClearContents
        End With
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        
        .Range("B2:B" & LR).FormulaR1C1 = _
            "=INT(INDEX(Sheet1!C3, MATCH(ROW(R[-1]C1), Sheet1!C7, 0)))"
        .Range("C2:C" & LR).FormulaR1C1 = _
            "=MOD(INDEX(Sheet1!C3, MATCH(ROW(R[-1]C1), Sheet1!C7, 0)),1)"
        .Range("D2:D" & LR).FormulaR1C1 = _
            "=INT(INDEX(Sheet1!C5, MATCH(ROW(R[-1]C1), Sheet1!C7, 0)))"
        .Range("E2:E" & LR).FormulaR1C1 = _
            "=MOD(INDEX(Sheet1!C5, MATCH(ROW(R[-1]C1), Sheet1!C7, 0)),1)"
        
        With .Range("B2:E" & LR)
            .Value = .Value
        End With
    
        .Range("B:B,D:D").NumberFormat = "m/d/yyyy"
        .Range("C:C,E:E").NumberFormat = "[$-409]h:mm AM/PM;@"
        .Columns.AutoFit
    End With
    
    Sheets("Sheet1").Range("G:G").ClearContents
    Application.ScreenUpdating = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    01-21-2012
    Location
    Leeds, England
    MS-Off Ver
    Excel 2007 + Excel 2010
    Posts
    3

    Re: Searching Through Ranges Then Coppying Data From One Sheet to Another

    Hi JBeaucaire,

    That is a really well coded macro and works very nicely apart from it is adding the accessed time and date to sheet 2, instead it should be the date modified. I'm sorry for not making myself clear. I have tabulated what the first few data entries to what they should be.


    Filename Date Created Time Created Date Modified Time Modified
    1541 - RS91101L Tie-In Operations - 0001.db 5/1/2012 20:49 6/1/2012 11:04
    1542 - RS21306P Spool Tie In Ops - 0001.db 6/1/2012 14:07 7/1/2012 18:10
    1543 - RS21306P Spool Denizen Skid Ops - 0001.db 7/1/2012 22:06 8/1/2012 21:44

    I have managed to get the code to read the date modified column but it isn't returning the correct values from here. From what i have gathered it is returning the 1541 - RS91101L Tie-In Operations - 0001.db modified date. But instead i really need it to take the last database revisions i.e. 1541 - RS91101L Tie-In Operations - 0011.db date modified.

    Option Explicit
    
    Sub DataExtract()
    Dim LR As Long
    
    Application.ScreenUpdating = False
    LR = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    Sheets("Sheet1").Range("F2:F" & LR).Offset(, 1).FormulaR1C1 = _
            "=IF(RIGHT(RC1,7)=""0001.db"", R[-1]C7+1, R[-1]C7)"
    
    With Sheets("Sheet2")
        On Error Resume Next
        .UsedRange.Cells.Clear
        
        .Range("A1:E1").Value = [{"Filename","Date Created","Time Created","Date Modified","Time Modified"}]
        With .Range("A2:A" & LR)
            .FormulaR1C1 = "=INDEX(Sheet1!C1, MATCH(ROW(R[-1]C1), Sheet1!C7, 0))"
            .Value = .Value
            .SpecialCells(xlConstants, xlErrors).ClearContents
        End With
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        
        .Range("B2:B" & LR).FormulaR1C1 = _
            "=INT(INDEX(Sheet1!C3, MATCH(ROW(R[-1]C1), Sheet1!C7, 0)))"
        .Range("C2:C" & LR).FormulaR1C1 = _
            "=MOD(INDEX(Sheet1!C3, MATCH(ROW(R[-1]C1), Sheet1!C7, 0)),1)"
        .Range("D2:D" & LR).FormulaR1C1 = _
            "=INT(INDEX(Sheet1!C4, MATCH(ROW(R[-1]C1), Sheet1!C7, 0)))"
        .Range("E2:E" & LR).FormulaR1C1 = _
            "=MOD(INDEX(Sheet1!C4, MATCH(ROW(R[-1]C1), Sheet1!C7, 0)),1)"
        
        With .Range("B2:E" & LR)
            .Value = .Value
        End With
    
        .Range("B:B,D:D").NumberFormat = "d/m/yyyy"
        .Range("C:C,E:E").NumberFormat = "hh:mm"
        .Columns.AutoFit
    End With
    
    Sheets("Sheet1").Range("G:G").ClearContents
    Application.ScreenUpdating = True
    End Sub
    If you could help me i would be very grateful, im not too sure how to make it look for the latest revision?

    Cheers,

    Dan

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,228

    Re: Searching Through Ranges Then Coppying Data From One Sheet to Another

    Try this:
    Option Explicit
    
    Sub DataExtract()
    Dim LR As Long
    
    Application.ScreenUpdating = False
    LR = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    Sheets("Sheet1").Range("F2:F" & LR).Offset(, 1).FormulaR1C1 = _
            "=IF(RIGHT(RC1,7)=""0001.db"", R[-1]C7+1, R[-1]C7)"
    
    With Sheets("Sheet2")
        On Error Resume Next
        .UsedRange.Cells.Clear
        
        .Range("A1:E1").Value = [{"Filename","Date Created","Time Created","Date Modified","Time Modified"}]
        With .Range("A2:A" & LR)
            .FormulaR1C1 = "=INDEX(Sheet1!C1, MATCH(ROW(R[-1]C1), Sheet1!C7, 0))"
            .Value = .Value
            .SpecialCells(xlConstants, xlErrors).ClearContents
        End With
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        
        .Range("B2:B" & LR).FormulaR1C1 = _
            "=INT(INDEX(Sheet1!C3, MATCH(ROW(R[-1]C1), Sheet1!C7, 0)))"
        .Range("C2:C" & LR).FormulaR1C1 = _
            "=MOD(INDEX(Sheet1!C3, MATCH(ROW(R[-1]C1), Sheet1!C7, 0)),1)"
        .Range("D2:D" & LR).FormulaR1C1 = _
            "=INT(INDEX(Sheet1!C4, MATCH(ROW(R[-1]C1), Sheet1!C7, 0) + COUNTIF(Sheet1!C7, ROW(R[-1]C[-3])) - 1))"
        .Range("E2:E" & LR).FormulaR1C1 = _
            "=MOD(INDEX(Sheet1!C4, MATCH(ROW(R[-1]C1), Sheet1!C7, 0) + COUNTIF(Sheet1!C7, ROW(R[-1]C[-4])) - 1), 1)"
        
        With .Range("B2:E" & LR)
            .Value = .Value
        End With
    
        .Range("B:B,D:D").NumberFormat = "m/d/yyyy"
        .Range("C:C,E:E").NumberFormat = "[$-409]h:mm AM/PM;@"
        .Columns.AutoFit
    End With
    
    Sheets("Sheet1").Range("G:G").ClearContents
    Application.ScreenUpdating = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  5. #5
    Registered User
    Join Date
    01-21-2012
    Location
    Leeds, England
    MS-Off Ver
    Excel 2007 + Excel 2010
    Posts
    3

    Re: Searching Through Ranges Then Coppying Data From One Sheet to Another

    Thank you so much, this works perfectly now. .

    Cheers,

    Dan

  6. #6
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,228

    Re: Searching Through Ranges Then Coppying Data From One Sheet to Another

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0