+ Reply to Thread
Results 1 to 4 of 4

VBA Macro to copy cells to another sheet based on the date value

Hybrid View

  1. #1
    Registered User
    Join Date
    03-30-2012
    Location
    athens,greece
    MS-Off Ver
    Excel 2007
    Posts
    2

    VBA Macro to copy cells to another sheet based on the date value

    Hi ,
    newbie in macro

    in sheet1 are three columns

    1 ____ A _______ B ___ C
    2 __ 30/03/12 __ a __ 51
    3 __ 30/03/12 __ b __ 52
    4 __ 31/03/12 __ c __ 53
    5 __ 31/03/12 __ d __ 54
    6 __ 01/04/12 __ e __ 55
    7 __ 01/04/12 __ f __ 56
    8 __ 01/04/12 __ g __ 57
    9 __ 02/04/12 __ h __ 58

    if in sheet2 and the cell H1 = 31/03/12

    i wont to copy in A1 :C2 the cells of sheet2 A4:C5

    like

    1 _____ A _____ B __ C ________ 31/03/12
    2 __ 31/03/12 __ c __ 53
    3 __ 31/03/12 __ d __ 54

    Or H1 = 01/04/12
    then copy A6:C8
    Last edited by lilis_hlias; 03-31-2012 at 03:53 PM.

  2. #2
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: VBA Macro to copy cells to another sheet based on the date value

    Hello and welcome to the forum.

    See if this does as you need.

    Sheet references are Sheet Code Names. Adjust the names as needed.

    Option Explicit
    
    Sub Copy_By_Date_Criteria()
    
        Dim nextrow As Long, bottomrow As Long
        Dim rngFilter As Range
        Dim sCriteria As String
        
        Application.ScreenUpdating = False
        
        With Sheet2 '//sheet code name
            nextrow = .Cells(Rows.Count, "A").End(xlUp).Row + 1
            sCriteria = .Range("H1").Value
            
            If Not IsDate(sCriteria) Then
                MsgBox ("Invalid date criteria"), vbExclamation
                Exit Sub
            End If
            
        End With
        
        With Sheet1 '//sheet code name
            .AutoFilterMode = False
            bottomrow = .Cells(Rows.Count, "A").End(xlUp).Row
            Set rngFilter = .Range("A1:C" & bottomrow)
            rngFilter.AutoFilter field:=1, Criteria1:=sCriteria
            rngFilter.Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy Sheet2.Range("A" & nextrow)
            .AutoFilterMode = False
        End With
        
        Set rngFilter = Nothing
        
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    
    End Sub
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  3. #3
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: VBA Macro to copy cells to another sheet based on the date value

    Content deleted

  4. #4
    Registered User
    Join Date
    03-30-2012
    Location
    athens,greece
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: VBA Macro to copy cells to another sheet based on the date value

    Thank you for the quick reply
    but somewhere I'm wrong

    Sub Copy_By_Date_Criteria()
    
        Dim nextrow As Long, bottomrow As Long
        Dim rngFilter As Range
        Dim sCriteria As String
        
        Application.ScreenUpdating = False
        
        With Sheets("Date").Select
            nextrow = .Cells(Rows.Count, "A").End(xlUp).Row + 1
            sCriteria = .Range("H1").Value
            
            If Not IsDate(sCriteria) Then
                MsgBox ("Invalid date criteria"), vbExclamation
                Exit Sub
            End If
            
        End With
        
        With Sheets("Data").Select
            .AutoFilterMode = False
            bottomrow = .Cells(Rows.Count, "A").End(xlUp).Row
            Set rngFilter = .Range("A1:C" & bottomrow)
            rngFilter.AutoFilter field:=1, Criteria1:=sCriteria
            rngFilter.Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy Sheets("Data").Range("A" & nextrow)
            .AutoFilterMode = False
        End With
        
        Set rngFilter = Nothing
        
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    
    End Sub
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1