+ Reply to Thread
Results 1 to 4 of 4

Copying the data from one sheet to another on the basis of yes/no condition

Hybrid View

  1. #1
    Registered User
    Join Date
    11-22-2013
    Location
    pune
    MS-Off Ver
    Excel 2003
    Posts
    2

    Question Copying the data from one sheet to another on the basis of yes/no condition

    I want to copy the selective data (Columns) from one excel sheet to another on a filter of yes and no .
    Here is my excel
    Now the problem is when i execute the code all the data of a particular column is copied and filter for yes and no is not working as per my code

    Here is my code
    Sub testIt()
    Dim r As Long, endRow As Long
    
    endRow = 20  ' of course it's best to retrieve the last used row number via a function
    'pasteRangeIndex = 1
    
    For r = 5 To endRow 'Loop through sheet1 and search for your criteria
    
        If Cells(r, Columns("H").Column).Value = "yes" Then 'Found
    
                'Copy the current cell
                
                
          
       Range("G1:G19").Select
       Selection.Copy
       Sheets("Sheet1").Select
       Range("A1:A19").Select
       Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                
                
         Sheets("Sheet3").Select
       Range("C1:C19").Select
       Selection.Copy
       Sheets("Sheet1").Select
       Range("B1:B19").Select
       Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
       
       Sheets("Sheet3").Select
       Range("D1:D19").Select
       Selection.Copy
       Sheets("Sheet1").Select
       Range("C1:C19").Select
       Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
       
      
        
              
        End If
    
    Next r
    
    
    End Sub
    Moderator Note:

    Pls use code tags around your code next time as per forum rules.
    Last edited by Fotis1991; 11-25-2013 at 06:22 AM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Copying the data from one sheet to another on the basis of yes/no condition

    Hi, and welcome to the forum. Unfortunately your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    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 However even with the code formatted correctly it's likely to be of little use unless we can see the workbook itself.

    So upload the workbook and explain clearly what is your end goal. Manually add a typical result situation and say how you have arrived at it. Don't explain what your current macro is trying to do, we like to decide for ourselves what is the best approach and all we need to know is what you start with and what you want to end up with.



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    11-22-2013
    Location
    pune
    MS-Off Ver
    Excel 2003
    Posts
    2

    Question Re: Copying the data from one sheet to another on the basis of yes/no condition

    Hi ,
    I want all the data from Sheet1 to be copied to Sheet2 based on Yes condition only .

    Following is my code but is giving everything including no condition data , however i need only "yes" one.
    Open excel(questions1.xlsx - most updated) for more info.
    Please Help.

    <Code>
    Sub testIt()
    Dim r As Long, endRow As Long

    endRow = 20 ' of course it's best to retrieve the last used row number via a function
    'pasteRangeIndex = 1

    For r = 5 To endRow 'Loop through sheet1 and search for your criteria

    If Cells(r, Columns("H").Column).Value = "yes" Then 'Found

    'Copy the current cell



    Range("G1:G19").Select
    Selection.Copy
    Sheets("Sheet1").Select
    Range("A1:A19").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False


    Sheets("Sheet3").Select
    Range("C1:C19").Select
    Selection.Copy
    Sheets("Sheet1").Select
    Range("B1:B19").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    Sheets("Sheet3").Select
    Range("D1:D19").Select
    Selection.Copy
    Sheets("Sheet1").Select
    Range("C1:C19").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False




    End If

    Next r


    End Sub
    </Code>
    Attached Files Attached Files
    Last edited by shantnuchaurasia; 11-25-2013 at 07:57 AM. Reason: please refer latest sheet (Questions1)

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Copying the data from one sheet to another on the basis of yes/no condition

    Hi,

    The easiest way for all these sorts of tasks is to use Data Filter


    First put column labels above each of the columns in your data range, i.e. in A4:K4
    Apply a dynamic range name to your data so that you don't need to worry about new records being added. i.e.

    your name = =OFFSET(Sheet1!A4,0,0,COUNTA(Sheet1!A:A),11)

    Now create a range of column labels on your output sheet. e.f. in say A5:G5 on sheet 2 copy your sheet 1 column labels. You don't need to copy them all if you only want a subset. Name this range of labels say "DataOut"

    Create a Criteria range in say A1:A2 on sheet 2 with the column G label in A1 and the word "yes" in A2. Name A1:A2 say "Crit"

    Now use the following macro

    Sub FilterData
       Sheet1.Range("Data").AdvancedFilter Action:=xlFilterCopy, copytorange:=Sheet2.Range("DataOut"), criteriarange:=Sheet2.Range("Crit")
    
    End Sub

+ 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. Replies: 8
    Last Post: 10-12-2012, 12:35 PM
  2. copying data from 1 sheet to another based on condition by VB Macro
    By kavya_2009 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-27-2011, 04:46 PM
  3. Copying rows by condition to a new sheet
    By eric_t_viking in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-12-2010, 05:54 PM
  4. Copying rows to new sheet based on condition
    By nraz76 in forum Excel General
    Replies: 5
    Last Post: 10-20-2009, 11:29 AM
  5. copy the data on basis of ID match in a Single sheet
    By ravinder_tigh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-15-2009, 07:09 AM

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