+ Reply to Thread
Results 1 to 11 of 11

Copy & Paste a range after matching a input value with a column

Hybrid View

  1. #1
    Registered User
    Join Date
    04-15-2008
    Location
    Tamil Nadu, India.
    MS-Off Ver
    Microsoft Office 2016
    Posts
    582

    Copy & Paste a range after matching a input value with a column

    Dear Friends,

    I request a macro do do the following:

    1) Take input in a dialog box
    2) As per that input, check & match with values in a column
    3) select & copy the data
    4) paste it from next to last value in a column

    I have attached a sample file in which I gave sample data and details of How & What to do.

    I request experts for their support.
    Attached Files Attached Files
    Good friends are hard to find, harder to leave, and impossible to forget.

    acsishere.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Copy & Paste a range after matching a input value with a column

    Is it what you need?
    Option Explicit
    
    Sub MyJob()
    Dim InVal As String
    Dim F
    Dim LR As Integer
    Dim WkRg As Range
    
        InVal = Application.InputBox("Enter your choice", " Year selection", "2020-21", Type:=2)
        Application.ScreenUpdating = False
        ActiveSheet.AutoFilterMode = False
        LR = Cells(Rows.Count, 2).End(3).Row
        Set WkRg = Range("$A$3:$F" & LR)
    
        If (InVal = "False") Then Exit Sub
    
        WkRg.AutoFilter Field:=4, Criteria1:=InVal
        Set WkRg = WkRg.Offset(0, 1).SpecialCells(xlCellTypeVisible)
        ActiveSheet.AutoFilterMode = False
        WkRg.Copy Destination:=Cells(LR + 1, 2)
        Cells(LR + 1, 1).EntireRow.Delete
        Application.ScreenUpdating = False '
    End Sub
    Last edited by PCI; 01-23-2021 at 05:10 PM.
    - Battle without fear gives no glory - Just try

  3. #3
    Registered User
    Join Date
    04-15-2008
    Location
    Tamil Nadu, India.
    MS-Off Ver
    Microsoft Office 2016
    Posts
    582

    Re: Copy & Paste a range after matching a input value with a column

    Sir,

    Thanks for your code. Perfectly working.

    But, just a request. I changed a column.

    That is, need to copy & paste columns C to F only (in the previous sample, it was B to F).

    I tried changing code. But, ended up with errors.

    Please help me to resolve.

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Copy & Paste a range after matching a input value with a column

    Here you are

    Option Explicit
    
    Sub MyJob()
    Dim InVal As String
    Dim F
    Dim LR As Integer
    Dim WkRg As Range
    
        InVal = Application.InputBox("Enter your choice", " Year selection", "2020-21", Type:=2)
        Application.ScreenUpdating = False
        ActiveSheet.AutoFilterMode = False
        LR = Cells(Rows.Count, 2).End(3).Row
        Set WkRg = Range("$A$3:$F" & LR)
    
        If (InVal = "False") Then Exit Sub
        WkRg.AutoFilter Field:=4, Criteria1:=InVal          '  Filter on column D => 4
        Set WkRg = WkRg.Offset(0, 2).SpecialCells(xlCellTypeVisible)    '  Collect from column C => 2
        ActiveSheet.AutoFilterMode = False
        WkRg.Copy Destination:=Cells(LR + 1, 3)             '  Paste in column C => 3
        Cells(LR + 1, 1).EntireRow.Delete
        Application.ScreenUpdating = False '
    End Sub

  5. #5
    Registered User
    Join Date
    04-15-2008
    Location
    Tamil Nadu, India.
    MS-Off Ver
    Microsoft Office 2016
    Posts
    582

    Re: Copy & Paste a range after matching a input value with a column

    Sir,

    Thanks for your updated code.

    But again the same issue.

    As some formula is there in Columns A4:B5001, I dont want to disturb it while executing your Code.

    Therefore, we need to copy & paste only columns C:F, only (without disturbing col A & B).

    Please Sir.

  6. #6
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Copy & Paste a range after matching a input value with a column

    See nexr revision.
    If does not work please attach a new Excel sample with formulas
    Option Explicit
    
    Sub MyJob()
    Dim InVal As String
    Dim F
    Dim LR As Integer
    Dim WkRg As Range
    
        InVal = Application.InputBox("Enter your choice", " Year selection", "2020-21", Type:=2)
        Application.ScreenUpdating = False
        ActiveSheet.AutoFilterMode = False
        LR = Cells(Rows.Count, 3).End(3).Row
        Set WkRg = Range("$A$3:$F" & LR)
    
        If (InVal = "False") Then Exit Sub
        WkRg.AutoFilter Field:=4, Criteria1:=InVal          '  Filter on column D => 4
        Set WkRg = WkRg.Offset(0, 2).SpecialCells(xlCellTypeVisible)    '  Collect from column C => 2
        ActiveSheet.AutoFilterMode = False
        WkRg.Copy Destination:=Cells(LR + 1, 3)             '  Paste in column C => 3
        Cells(LR + 1, 3).Resize(1, 4).Delete Shift:=xlUp
        Application.ScreenUpdating = False '
    End Sub

  7. #7
    Registered User
    Join Date
    04-15-2008
    Location
    Tamil Nadu, India.
    MS-Off Ver
    Microsoft Office 2016
    Posts
    582

    Re: Copy & Paste a range after matching a input value with a column

    Sir,

    As stated, I hereby attached the Sample file with your latest code.

    Also, mentioned the formulas in Column A & B which are not to be disturbed.

    Request your kind support.
    Attached Files Attached Files

  8. #8
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,213

    Re: Copy & Paste a range after matching a input value with a column

    You need to lock row in the formula
    B4 =IFERROR(VLOOKUP(C4,Sheet2!$B$1:$C$21,2,FALSE),"")


    Then try

    Sub pes()
    Dim r As Long
        Application.ScreenUpdating = False
       r = Cells(Rows.Count, 3).End(xlUp).Row
            Range("C3:H" & r).AutoFilter Field:=2, Criteria1:=Application.InputBox("Select Year", "Year1", "2020-21")
            Range("C4:H" & r).SpecialCells(12).Copy Cells(r + 1, 3)
            Range("C3:H" & r).AutoFilter
            Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    Last edited by Bo_Ry; 01-24-2021 at 01:56 PM.

  9. #9
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Copy & Paste a range after matching a input value with a column

    As Bo_Ry mentioned it, keep in mind to freeze the range of the Vlookup
    In the next code formulas are copied down
    Option Explicit
    
    Sub MyJob()
    Dim InVal As String
    Dim LR As Integer
    Dim WkRg As Range
    
        InVal = Application.InputBox("Enter your choice", " Year selection", "2020-21", Type:=2)
        If (InVal = "False") Then Exit Sub
        ActiveSheet.AutoFilterMode = False
        LR = Cells(Rows.Count, 3).End(3).Row
        Set WkRg = Range("$C$3:$F" & LR)
        WkRg.AutoFilter Field:=2, Criteria1:=InVal          '  Filter on column D => 2
        Set WkRg = Range("$C$4:$F" & LR).SpecialCells(xlCellTypeVisible)
        WkRg.Copy Destination:=Cells(LR + 1, 3)             '  Paste in column C => 3
        ActiveSheet.AutoFilterMode = False
        LR = Cells(Rows.Count, 3).End(3).Row
        Range("A4:B4").AutoFill Destination:=Range("A4:B" & LR)
        
    End Sub

  10. #10
    Registered User
    Join Date
    04-15-2008
    Location
    Tamil Nadu, India.
    MS-Off Ver
    Microsoft Office 2016
    Posts
    582

    Re: Copy & Paste a range after matching a input value with a column

    Thanks a lot to PCI & Bo_Ry.

    I got the solution.

  11. #11
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Copy & Paste a range after matching a input value with a column

    You're welcome and thanks for the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. New to VBA, simple copy and paste range of cells based on input box, NEED HELP
    By PNick in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 04-12-2017, 04:26 PM
  2. Replies: 3
    Last Post: 03-31-2017, 02:43 PM
  3. [SOLVED] Search for multiple matching cells and copy/paste range?
    By papercut in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-01-2014, 11:24 AM
  4. [SOLVED] Copy Range And Paste It As Link On Another Sheet With Using Input Box
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-08-2014, 05:21 AM
  5. Use input box for values then copy and paste into column
    By eonizuka in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-19-2009, 04:35 PM
  6. Copy input range and paste into Format Control
    By Tibby in forum Excel General
    Replies: 2
    Last Post: 10-28-2008, 04:39 PM
  7. Input box Copy Paste Range
    By JavyD in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-15-2005, 06:50 PM

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