+ Reply to Thread
Results 1 to 8 of 8

Copy specific cells from one worksheet to another worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    07-21-2017
    Location
    NY
    MS-Off Ver
    16.0
    Posts
    25

    Copy specific cells from one worksheet to another worksheet

    Hi everyone, need some help with this VBA code!

    What I want to do is copy cells from a worksheet (i.e. "Input") to another worksheet (i.e. "Output") when a specific variable is entered into a specific column.

    Whenever a "1" is entered by a user (in Yes/No i.e. E, J, O etc.), I want the code to copy the matching varible under table name (in column B, G, L) to a new worksheet where these variables will form a new table for all entries with a "1" entered. I have attached an Excel sheet and the output should look like the new worksheet "Output" after the code is run. Obviously it would be blank to begin with as nothing is changed from 0 to 1 yet. The new worksheet should update automatically in real time when 0's and 1's are entered into "Input", so there shouldn't need to create a button.

    Aside from the Yes/No column, there is a Low and High column for Tables 9, 10, 11. For these, it should only look the data inputted into the 8th row, i.e. AU8, AV8, AZ8, BA8, BE8, and BF8. And rather than taking only "1", these cells should take any number (even 0) as long as Low and High are different. If they are the same, then don't move either to "Output" worksheet because no numerical range is being selected.

    Any help on the code is much appreciated!

    The code below is what I've tried to do so far, but it obviously hasn't worked.

    In Module:

    Option Explicit
    
    Sub Filter()
    Dim rng1, rng2, rng3, rng4, rng5, rng6, rng7, rng8, _
    rng9, rng10, rng11, rng12, rng13, rng14, cell, fndRng As Range
    Dim Start, Finish As Worksheet
    Dim nrow, col  As Long
    Dim Val As String
    Application.ScreenUpdating = False
    Sheets("Input").Range("A2:B5000").ClearContents
    Set Start = Sheets("Input"): Set Finish = Sheets("Output")
    With Start
        Set rng1 = .Range("E8:E" & .Cells(Rows.Count, "E").End(xlUp).Row)
        Set rng2 = .Range("J8:J" & .Cells(Rows.Count, "J").End(xlUp).Row)
        Set rng3 = .Range("O8:O" & .Cells(Rows.Count, "O").End(xlUp).Row)
        Set rng4 = .Range("U8:U" & .Cells(Rows.Count, "U").End(xlUp).Row)
        Set rng5 = .Range("Z8:Z" & .Cells(Rows.Count, "Z").End(xlUp).Row)
        Set rng6 = .Range("AF8:AF" & .Cells(Rows.Count, "AF").End(xlUp).Row)
        Set rng7 = .Range("AL8:AL" & .Cells(Rows.Count, "AL").End(xlUp).Row)
        Set rng8 = .Range("AQ8:AQ" & .Cells(Rows.Count, "AQ").End(xlUp).Row)
        Set rng9 = .Range("AU8" & .Cells(Rows.Count, "AU").End(xlUp).Row)
        Set rng10 = .Range("AV8" & .Cells(Rows.Count, "AV").End(xlUp).Row)
        Set rng11 = .Range("AZ8" & .Cells(Rows.Count, "AZ").End(xlUp).Row)
        Set rng12 = .Range("BA8" & .Cells(Rows.Count, "BA").End(xlUp).Row)
        Set rng13 = .Range("BE8" & .Cells(Rows.Count, "BE").End(xlUp).Row)
        Set rng14 = .Range("BF8" & .Cells(Rows.Count, "BF").End(xlUp).Row)
        For Each cell In Union(rng1, rng2, rng3, rng4, rng5, rng6, rng7, _
        rng8, rng9, rng10, rng11, rng12, rng13, rng14)
            Val = cell.Offset(, -1)
            If cell = "1" Then
                col = cell.Column
                With Finish
                    With .Range("B:B")
                        Set fndRng = .Find(Val, LookIn:=xlValues, lookat:=xlWhole)
                    End With
                    If fndRng Is Nothing Then
                         nrow = .Cells(Rows.Count, "A").End(xlUp).Row + 1
                        .Range("A" & nrow) = Start.Cells(1, col - 1)
                        .Range("B" & nrow) = cell.Offset(0, -1)
                    End If
                End With
            ElseIf cell = "0" Then
                With Finish
                    With .Range("B:B")
                        Set fndRng = .Find(Val, LookIn:=xlValues, lookat:=xlWhole)
                    End With
                    If Not fndRng Is Nothing Then
                        fndRng.EntireRow.Delete
                    End If
                End With
            End If
        Next cell
    End With
    Application.ScreenUpdating = True
    End Sub
    -------
    In Sheet 1 ("Input"):

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 5 Or Target.Column = 10 Or Target.Column = 15 _
    Or Target.Column = 20 Or Target.Column = 25 Or Target.Column = 30 _
    Or Target.Column = 35 Or Target.Column = 40 Or Target.Column = 44 _
    Or Target.Column = 45 Or Target.Column = 49 Or Target.Column = 50 _
    Or Target.Column = 54 Or Target.Column = 55 Then
        Call Filter
    End If
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: Copy specific cells from one worksheet to another worksheet

    Probably just setting it up for each target.range would work as well

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim Sh As Worksheet, LstRw As Long
        Set Sh = Sheets("Output")
    
        If Target.Count > 1 Then Exit Sub    ' this stops code error if more than one cell is changed at once
        '---------table1
        If Not Application.Intersect(Target, Me.Range("E8:E100")) Is Nothing Then    ' indicates the Target range
            With Sh
                LstRw = .Cells(.Rows.Count, "B").End(xlUp).Row + 1
                .Cells(LstRw, 2) = "Table 1"
                .Cells(LstRw, 3) = Target.Offset(, -3)
            End With
        End If
        '-----table 2
        If Not Application.Intersect(Target, Me.Range("J8:J100")) Is Nothing Then    ' indicates the Target range
            With Sh
                LstRw = .Cells(.Rows.Count, "B").End(xlUp).Row + 1
                .Cells(LstRw, 2) = "Table 2"
                .Cells(LstRw, 3) = Target.Offset(, -3)
            End With
        End If
        '-----table 3
        If Not Application.Intersect(Target, Me.Range("O8:O100")) Is Nothing Then    ' indicates the Target range
            With Sh
                LstRw = .Cells(.Rows.Count, "B").End(xlUp).Row + 1
                .Cells(LstRw, 2) = "Table 3"
                .Cells(LstRw, 3) = Target.Offset(, -3)
            End With
        End If
    
    
    
    End Sub
    By the way,
    Dim rng1, rng2, rng3, rng4, rng5, rng6, rng7, rng8, _
    rng9, rng10, rng11, rng12, rng13, rng14, cell, fndRng As Range
    The only variable properly set as a range would be fndRng, everything else would not be considered ranges.
    Dim rng1 as range,rng2 as range,rng3 as range......................etc..
    Last edited by davesexcel; 07-24-2017 at 09:30 AM.

  3. #3
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,337

    Re: Copy specific cells from one worksheet to another worksheet

    Hey jos283...well done...You were very close...
    Try this...
    Option Explicit
    
    Sub FilterSummary()
    Dim rng1 As Range, rng2 As Range, rng3 As Range, rng4 As Range, rng5 As Range, _
    rng6 As Range, rng7 As Range, rng8 As Range, rng9 As Range, rng10 As Range, _
    rng11 As Range, cell As Range
    Dim Start As workSheet, Finish As Worksheet
    Dim nrow as Long, col  As Long
    Dim tbl As String, Val  As String
    Application.ScreenUpdating = False
    Set Start = Sheets("Input"): Set Finish = Sheets("Output")
    Finish.Range("B3:D5000").ClearContents
    With Start
        Set rng1 = .Range("E8:E" & .Cells(Rows.Count, "E").End(xlUp).Row)
        Set rng2 = .Range("J8:J" & .Cells(Rows.Count, "J").End(xlUp).Row)
        Set rng3 = .Range("O8:O" & .Cells(Rows.Count, "O").End(xlUp).Row)
        Set rng4 = .Range("U8:U" & .Cells(Rows.Count, "U").End(xlUp).Row)
        Set rng5 = .Range("Z8:Z" & .Cells(Rows.Count, "Z").End(xlUp).Row)
        Set rng6 = .Range("AF8:AF" & .Cells(Rows.Count, "AF").End(xlUp).Row)
        Set rng7 = .Range("AL8:AL" & .Cells(Rows.Count, "AL").End(xlUp).Row)
        Set rng8 = .Range("AQ8:AQ" & .Cells(Rows.Count, "AQ").End(xlUp).Row)
        Set rng9 = .Range("AU8:AV" & .Cells(Rows.Count, "AV").End(xlUp).Row)
        Set rng10 = .Range("AZ8:BA" & .Cells(Rows.Count, "BA").End(xlUp).Row)
        Set rng11 = .Range("BE8:BF" & .Cells(Rows.Count, "BF").End(xlUp).Row)
        
        For Each cell In Union(rng1, rng2, rng3, rng4, rng5, rng6, rng7, rng8)
            If cell = "1" Then
                col = cell.Offset(, -3).Column
                tbl = .Cells(7, col)
                Val = cell.Offset(, -3)
                With Finish
                    nrow = .Cells(Rows.Count, "B").End(xlUp).Row + 1
                    .Range("B" & nrow) = tbl
                    .Range("C" & nrow) = Val
                End With
            End If
        Next cell
        For Each cell In Union(rng9, rng10, rng11)
            If cell >= 0 And cell.Offset(, 1) > cell Then
                col = cell.Offset(, -2).Column
                tbl = .Cells(7, col)
                With Finish
                    nrow = .Cells(Rows.Count, "B").End(xlUp).Row + 1
                    .Range("B" & nrow) = tbl
                    .Range("C" & nrow) = cell
                    .Range("D" & nrow) = cell.Offset(, 1)
                End With
            End If
        Next cell
    End With
    Application.ScreenUpdating = True
    End Sub
    Keep your worksheet code as is...

    Edit:
    As per davesexcel....all the other rng's will be declared as Variant which generally will be accepted until it bombs out somewhere.

    PS...What about the sample workbook in this post...Is it the same requirement but is this the original file..
    https://www.excelforum.com/excel-pro...ml#post4702759
    Last edited by sintek; 07-24-2017 at 01:48 PM.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  4. #4
    Registered User
    Join Date
    07-21-2017
    Location
    NY
    MS-Off Ver
    16.0
    Posts
    25

    Re: Copy specific cells from one worksheet to another worksheet

    Sintek -

    Yes, pretty much the same thing but an extended version of it with a few extra criteria.

    Thanks, I've adjusted it to suit my requirements but the code works again!

  5. #5
    Registered User
    Join Date
    07-21-2017
    Location
    NY
    MS-Off Ver
    16.0
    Posts
    25

    Re: Copy specific cells from one worksheet to another worksheet

    For the final 3 tables:

    Output.png

  6. #6
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,337

    Re: Copy specific cells from one worksheet to another worksheet

    Option Explicit
    
    Sub FilterSummary()
    Dim rng1 As Range, rng2 As Range, rng3 As Range, rng4 As Range, rng5 As Range, _
    rng6 As Range, rng7 As Range, rng8 As Range, rng9 As Range, rng10 As Range, _
    rng11 As Range, cell As Range
    Dim Start As Worksheet, Finish As Worksheet
    Dim nrow As Long, col  As Long
    Dim tbl As String, Val  As String
    Application.ScreenUpdating = False
    Set Start = Sheets("Input"): Set Finish = Sheets("Output")
    Finish.Range("B3:D5000").ClearContents
    With Start
        Set rng1 = .Range("E8:E" & .Cells(Rows.Count, "E").End(xlUp).Row)
        Set rng2 = .Range("J8:J" & .Cells(Rows.Count, "J").End(xlUp).Row)
        Set rng3 = .Range("O8:O" & .Cells(Rows.Count, "O").End(xlUp).Row)
        Set rng4 = .Range("U8:U" & .Cells(Rows.Count, "U").End(xlUp).Row)
        Set rng5 = .Range("Z8:Z" & .Cells(Rows.Count, "Z").End(xlUp).Row)
        Set rng6 = .Range("AF8:AF" & .Cells(Rows.Count, "AF").End(xlUp).Row)
        Set rng7 = .Range("AL8:AL" & .Cells(Rows.Count, "AL").End(xlUp).Row)
        Set rng8 = .Range("AQ8:AQ" & .Cells(Rows.Count, "AQ").End(xlUp).Row)
        Set rng9 = .Range("AU8:AV" & .Cells(Rows.Count, "AV").End(xlUp).Row)
        Set rng10 = .Range("AZ8:BA" & .Cells(Rows.Count, "BA").End(xlUp).Row)
        Set rng11 = .Range("BE8:BF" & .Cells(Rows.Count, "BF").End(xlUp).Row)
        
        For Each cell In Union(rng1, rng2, rng3, rng4, rng5, rng6, rng7, rng8)
            If cell = "1" Then
                col = cell.Offset(, -3).Column
                tbl = .Cells(7, col)
                Val = cell.Offset(, -3)
                With Finish
                    nrow = .Cells(Rows.Count, "B").End(xlUp).Row + 1
                    .Range("B" & nrow) = tbl
                    .Range("C" & nrow) = Val
                End With
            End If
        Next cell
        For Each cell In Union(rng9, rng10, rng11)
            If cell >= 0 And cell.Offset(, 1) > cell Then
                col = cell.Offset(, -2).Column
                tbl = .Cells(7, col)
                With Finish
                    nrow = .Cells(Rows.Count, "B").End(xlUp).Row + 1
                    col = cell.Column
                    .Range("B" & nrow) = tbl & "_Low"
                    .Range("C" & nrow) = cell
                    .Range("B" & nrow + 1) = tbl & "_High"
                    .Range("C" & nrow + 1) = cell.Offset(, 1)
                End With
            End If
        Next cell
    End With
    Application.ScreenUpdating = True
    End Sub

  7. #7
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Copy specific cells from one worksheet to another worksheet

    Hello jos283,

    Do you know if there is a way to get the output to look like this so that the table only uses two columns instead of three
    That is where the problem lies. With the conditions set for the Min and Max conditions.

    B.t.w. this Code of yours below;

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 5 Or Target.Column = 10 Or Target.Column = 15 _
    Or Target.Column = 20 Or Target.Column = 25 Or Target.Column = 30 _
    Or Target.Column = 35 Or Target.Column = 40 Or Target.Column = 44 _
    Or Target.Column = 45 Or Target.Column = 49 Or Target.Column = 50 _
    Or Target.Column = 54 Or Target.Column = 55 Then
        Call Filter
    End If
    End Sub
    Should actually read;

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 5 Or Target.Column = 10 Or Target.Column = 15 _
    Or Target.Column = 21 Or Target.Column = 26 Or Target.Column = 32 _
    Or Target.Column = 38 Or Target.Column = 43 Or Target.Column = 47 _
    Or Target.Column = 48 Or Target.Column = 52 Or Target.Column = 53 _
    Or Target.Column = 57 Or Target.Column = 58 Then
        Call FilterSummary
    End If
    End Sub
    Regards.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  8. #8
    Registered User
    Join Date
    07-21-2017
    Location
    NY
    MS-Off Ver
    16.0
    Posts
    25

    Re: Copy specific cells from one worksheet to another worksheet

    Thanks, spotted that!

+ 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. [SOLVED] Copy specific cells from one worksheet to another worksheet
    By jos283 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-21-2017, 07:35 AM
  2. [SOLVED] VBA to copy paste to specific worksheet from another workbook of same worksheet name
    By saleembasha in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-21-2015, 10:37 AM
  3. Replies: 8
    Last Post: 10-27-2014, 11:27 PM
  4. Macro to copy specific cells from diff worksheet
    By asach1211 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-09-2014, 09:55 AM
  5. Copy ranges and specific cells into one worksheet
    By asach1211 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-19-2014, 02:28 AM
  6. Replies: 3
    Last Post: 06-17-2013, 06:00 AM
  7. Copy Specific Cells To Another Worksheet
    By MARY-20 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-06-2013, 02:45 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