Results 1 to 9 of 9

Thread: Excel: copy data between sheets

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    20

    Question Excel: copy data between sheets

    Hi all,
    I have 2 sheets "sheet1" and "sheet2". I ask from a user to copy the cell range A5:F10 from "sheet1" to the same cell range of the "sheet2". I need a code that checks if the user movements are correct or not.
    Please help!
    Thanks in advance.

  2. #2
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Excel: copy data between sheets

    Hi,

    Forgive me if I misunderstand, but why not copy it yourself? Is it a training program or something? You can very easily copy the cells from one sheet to another.

    zaza

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    20

    Re: Excel: copy data between sheets

    Thanks for your answer.
    Yes I am trying to make a training program (VB6) that checks the correct movements of a user actions. My problem is that i can't find the right references between 2 sheets, so I can solve it.

  4. #4
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excel: copy data between sheets

    kabilliu:

    This is a snippet that will get you started. I threw it together quickly, but it does run. Try to work on it and post your code if you have problems.
    Code:
    Option Explicit
    Sub Macro1()
    Dim srcSheet As Worksheet 'Source Sheet
    Dim dstSheet As Worksheet 'Destination Sheet
    Dim aRange As Range '
    Dim aCell As Range
    Dim aRow As Long
    Dim aCol As Integer
    '
    Set srcSheet = Sheets("sheet1")
    Set dstSheet = Sheets("sheet2")
    Set aRange = srcSheet.Range("A5:F10")
    
    For Each aCell In aRange
        aRow = aCell.Row
        aCol = aCell.Column
        
        'Parameters: what all do we check here?
        If aCell = dstSheet.Cells(aRow, aCol) Then
            'The cells match
            'TEST TEST TEST TEST
            MsgBox "Match: "
            'END TEST
        Else
            'The cells do not match
            'TEST TEST TEST TEST
            MsgBox aCell.Address & Chr(10) & "No Match: "
            'END TEST
        End If
    
    Next
    End Sub
    Good Learning and Good Programming!
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    20

    Re: Excel: copy data between sheets

    Thanks for your help.
    Your code helps very much and works fine.
    I would like to know if is possible to change the loop "For Each ..e.c.c." or use some function, so I can get the total result.
    Your code checks the cells on the area (A5:F10) and show me the result (match or not match) for any cell. I prefer the code to check the cells behind and finally show me the result for whole area.
    Thanks again
    Regards

  6. #6
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excel: copy data between sheets

    You can set the range in the "Set aRange" statement to whatever you want. I just did that because you wrote:
    Quote Originally Posted by kabilliu
    I ask from a user to copy the cell range A5:F10 from "sheet1" to the same cell range of the "sheet2". I need a code that checks if the user movements are correct or not.
    I think that English is not your primary language. If so, you are doing pretty good, but I can't understand "so I can get the total result". Are you trying to find the SUM of the range of cells?

    Also, please clarify: "I prefer the code to check the cells behind".

    My code is just a template for you to work from. You can do whatever you want in place of the two message box statements. After all, you are writing the program. You can set up counters for the number of correct and incorrect answers; you can create a string with a list of the incorrect answers; do whatever you want. Then, between the "Next" and "End Sub" statements, you can put your code to report the results.
    Last edited by Webtest; Oct 11th, 2005 at 07:10 AM.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    20

    Re: Excel: copy data between sheets

    Thanks for your interest.
    You are right my English is not my primary language and for this reason is not good.
    I am sorry for that.
    Anyway I retry to explain you what I want.
    Your code checks one by one the cells and shows the differences.
    I ask if is possible to change the code so checks in background (Behind the scene) the cells and finally show me if the area A5:F5 of the sheet1 is equal or not with area A5:F5 of sheet2 (Not for any cell but for whole area)

    Thanks for your time

  8. #8
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    Re: Excel: copy data between sheets

    I think this is what you're missing:

    VB Code:
    1. Dim TheTotal1 As Double, TheTotal2 As Double
    2.  
    3.  
    4. 'to run the code in the background
    5. Application.ScreenUpdating = False
    6.  
    7. 'to compare sum of the 2 ranges
    8. TheTotal1 = Application.WorksheetFunction.Sum(Sheet1.Range("A5:A10"))
    9. TheTotal2 = Application.WorksheetFunction.Sum(Sheet2.Range("A5:A10"))
    10.  
    11. If TheTotal1 = TheTotal2 Then
    12. (your code)
    13. End If
    14.  
    15. 'you have to turn it back to true
    16. Application.ScreenUpdating = True

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    20

    Re: Excel: copy data between sheets

    Thank you very much D-Niss,
    This is that I want and works perfectly.
    Thanks again!
    Regards.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width