+ Reply to Thread
Results 1 to 9 of 9

Issue with Copying from one sheet to another

Hybrid View

  1. #1
    Registered User
    Join Date
    04-30-2020
    Location
    Colorado USA
    MS-Off Ver
    2016
    Posts
    4

    Issue with Copying from one sheet to another

    Hello,

    Thanks in advance to whoever may have an idea. I have found some code on here that I was trying to modify for my spreadsheet to do something similar. I do not know code very well at all as a heads up.

    Basically I have a source work sheet called "Worksheet" and a destination worksheet called "BOM". If the QTY(located in row c) in the source worksheet is greater than 0 I want it to copy those specific rows and particular cells to the BOM worksheet when I click the button.

    This does copy the correct cells, but it copies all rows even if it doesnt have any QTY at all.

    Any thoughts?


    Sub Button1_Click()
    
    Dim sws As Worksheet, dws As Worksheet
    Dim slr As Long, dlr As Long
    Set sws = Sheets("Worksheet")
    Set dws = Sheets("BOM")
    slr = sws.Cells(Rows.Count, 1).End(xlUp).Row
    dlr = dws.Cells(Rows.Count, 2).End(xlUp).Row
    If dlr > 2 Then dws.Range("A3:G" & dlr).EntireRow.Clear
    sws.AutoFilterMode = False
    With sws.Rows(26)
        .AutoFilter field:=3, Criteria1:=">0"
    End With
    sws.Range("C26:C340" & slr).SpecialCells(xlCellTypeVisible).Copy dws.Range("A3")
    sws.Range("D26:D340" & slr).SpecialCells(xlCellTypeVisible).Copy dws.Range("B3")
    sws.Range("E26:E340" & slr).SpecialCells(xlCellTypeVisible).Copy dws.Range("C3")
    sws.Range("F26:F340" & slr).SpecialCells(xlCellTypeVisible).Copy dws.Range("D3")
    sws.Range("G26:G340" & slr).SpecialCells(xlCellTypeVisible).Copy dws.Range("E3")
    sws.AutoFilterMode = False
    dws.Columns("C").AutoFit
    MsgBox "Finished.", vbInformation
    End Sub
    Last edited by AliGW; 05-01-2020 at 01:55 PM. Reason: Solved tag added - no need to edit thread title or add solved to post. Thanks.

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Issue with Copying from one sheet to another

    Calling it Worksheet is a bad idea - but:

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    04-30-2020
    Location
    Colorado USA
    MS-Off Ver
    2016
    Posts
    4

    Re: Issue with Copying from one sheet to another

    Done! Sorry about that!

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Issue with Copying from one sheet to another

    Maybe:

    Sub Button1_Click()
    
    Dim sws As Worksheet, dws As Worksheet
    Dim slr As Long, dlr As Long
    Set sws = Sheets("Worksheet")
    Set dws = Sheets("BOM")
    slr = sws.Cells(Rows.Count, 1).End(xlUp).Row
    dlr = dws.Cells(Rows.Count, 2).End(xlUp).Row
    If dlr > 2 Then dws.Range("A3:G" & dlr).EntireRow.Clear
    sws.AutoFilterMode = False
    With sws.Rows(26)
        .AutoFilter field:=3, Criteria1:="<>"
    End With
    sws.Range("C26:G340" & slr).SpecialCells(xlCellTypeVisible).Copy dws.Range("A3")
    
    sws.AutoFilterMode = False
    dws.Columns("C").AutoFit
    MsgBox "Finished.", vbInformation
    End Sub

  5. #5
    Registered User
    Join Date
    04-30-2020
    Location
    Colorado USA
    MS-Off Ver
    2016
    Posts
    4

    Re: Issue with Copying from one sheet to another

    Still seem to do the same thing.. simply copies all info from the range c26:g340 regardless if row c has a value or not. I created an example sheet and have attached it.
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    11-12-2012
    Location
    Jeddah, Saudi Arabia
    MS-Off Ver
    2010, 2013, 2016, Office 365
    Posts
    521

    Re: Issue with Copying from one sheet to another

    Change this part

    With sws.Rows(26)
        .AutoFilter field:=3, Criteria1:="<>"
    End With
    To..

    With sws.Rows(25)
        .AutoFilter field:=1, Criteria1:="<>"
    End With

  7. #7
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,104

    Re: Issue with Copying from one sheet to another

    Hello Elthznd901,

    Just another option for you.

    See if the code trimmed as follows works for you:-

    Sub Button1_Click()
    
    Application.ScreenUpdating = False
    
            Sheet2.Range("A3", Sheet2.Range("E" & Sheet2.Rows.Count).End(xlUp)).Clear
            
            On Error Resume Next
            
            With Sheet1.Range("C25", Sheet1.Range("C" & Sheet1.Rows.Count).End(xlUp))
                    .AutoFilter 1, ">0"
                    .Offset(1).Resize(, 5).SpecialCells(12).Copy Sheet2.[A3]
                    .AutoFilter
            End With
            
    Application.ScreenUpdating = True
    
    End Sub
    I've used the sheet codes rather than the sheet names in the code.

    I hope that this helps.

    Cheerio,
    vcoolio.

  8. #8
    Registered User
    Join Date
    04-30-2020
    Location
    Colorado USA
    MS-Off Ver
    2016
    Posts
    4

    Re: Issue with Copying from one sheet to another

    Thank you everyone for the help! The original code when I applied it to the actual spreadsheet kept having issues (copying everything over regardless of the criteria). If anything was in a cell in column A above line 25 criteria was ignored, if anything was in b, nothing would show at all in the destination sheet. That said, the code from vcoolio seems to work and those factors do not effect it. So this is solved. I still attached another example sheet showing the issue with the original code in case anyone has interest. First set of tabs.. work. Second set and Third set you can see the issue I am talking about.

    Thanks you all for your help!
    Attached Files Attached Files

  9. #9
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,104

    Re: Issue with Copying from one sheet to another

    You're welcome Elthznd901. I'm glad that we could assist.
    Thanks for the rep as well.

    Cheerio,
    vcoolio.

+ 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. Issue in Copying Row to a New Sheet
    By lateniteNC1 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-12-2017, 12:33 PM
  2. Replies: 0
    Last Post: 05-09-2017, 11:58 AM
  3. copying sheet issue caused by hidden sheets in fhile
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 04-19-2017, 10:37 AM
  4. Target Cell location issue while copying entire sheet by Macro
    By AnimeshRoy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-04-2016, 12:41 PM
  5. Copying Data from one sheet to another... issue using End(xldown) in subroutine
    By joe31623 in forum Excel Programming / VBA / Macros
    Replies: 37
    Last Post: 02-17-2016, 05:48 PM
  6. [SOLVED] Issue with Copying data to a New Sheet After Filtering.
    By AparnaSriram in forum Excel General
    Replies: 10
    Last Post: 11-16-2012, 04:17 AM
  7. Excel 2007 : issue copying sheet w/ names in calculations
    By BobbyD1120 in forum Excel General
    Replies: 3
    Last Post: 01-23-2011, 03:58 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