Consulting

Results 1 to 10 of 10

Thread: Copying value from different sheets

  1. #1
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location

    Copying value from different sheets

    Hello, I am trying to write a code to do a QC for my day to day work.
    I have about 12 sheets of data, each representing different jurisdiction. I would like to check each row that contains data. the check being if the value in column J is less then Column K then copy range from A:C and columns J and K values to a different sheet say "QC".

    I am stuck with a object error. Can some one please help me with this.
    [VBA]Sub Quality_Check()

    Dim i As Integer 'sheet index
    Dim j As Integer 'Row Counter
    Dim Lj As Integer 'Local counter
    Dim Nr As Integer
    j = 1
    'Check for Number of Lanes
    For i = 1 To 12 Step 1
    With Worksheets(i).Activate

    Nr = .Range("BD:BD").Rows.Count

    For Lj = 10 To Nr Step 1
    MsgBox Cells(Lj, 43).Value
    MsgBox Cells(Lj, 35).Value
    If (Cells(Lj, 43).Value < Cells(Lj, 35)) Then

    With Worksheets("QC").Activate
    .Cells(j, 2).Value = Worksheets(i).Cells(Lj, 2).Value
    End With
    j = j + 1
    End If
    Next Lj

    End With

    Next i


    End Sub
    [/VBA]
    I am a Newbie, soon to be a Guru

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi there,

    Both of these:
    [vba]With Worksheets(i).Activate

    Nr = .Range("BD:BD").Rows.Count [/vba]
    [vba]With Worksheets("QC").Activate
    .Cells(j, 2).Value = Worksheets(i).Cells(Lj, 2).Value[/vba]
    ...will fall over. .Activate is a Method and thus, has no properties (such as .Cells). Untested, try:
    Sub Quality_Check()
    Dim i As Long  'sheet index
    Dim j As Long  'Row Counter
    Dim Lj As Long  'Local counter
    Dim Nr As Long
        j = 1
         
        Nr = Worksheets(1).Rows.Count
         'Check for Number of Lanes
        For i = 1 To 12 Step 1
            For Lj = 10 To Nr Step 1
                MsgBox Worksheets(i).Cells(Lj, 43).Value
                MsgBox Worksheets(i).Cells(Lj, 35).Value
                If (Worksheets(i).Cells(Lj, 43).Value < Worksheets(i).Cells(Lj, 35)) Then
                    Worksheets("QC").Cells(j, 2).Value = Worksheets(i).Cells(Lj, 2).Value
                    j = j + 1
                End If
            Next Lj
        Next i
    End Sub
    Hope that helps,

    Mark

  3. #3
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location
    GTO , I would like to determine the number of used row (rows with data) in each sheet before to make it run faster.
    Also do you know how I can present the status (Like QC in process and done )

    Also is there another way to increment the counter J= j +1 as i am seeing plenty of empty rows something like J++ AS IN C# PROGRAMMING LANGUAGE
    I am a Newbie, soon to be a Guru

  4. #4
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location
    Ok I am done with most of this, This work book has more userdefined functions that I created as per the needs. Evey time I change a cell value it recalculated the entire sheet and which is slowing done the workbook.

    How do I avoid this?.
    I am a Newbie, soon to be a Guru

  5. #5
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    You can use enable / disable events while your macro runs like:
    [VBA]Private Sub MyMacro()
    'At the beginning of the code
    Application.EnableEvents = False
    'Main Code Finishes here
    Application.EnableEvents = True
    End Sub[/VBA]
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  6. #6
    VBAX Regular Chabu's Avatar
    Joined
    Dec 2010
    Location
    Brussels
    Posts
    85
    Location
    shrivallabha,
    application.enableEvents will not prevent recalculation

    What you need to do nepotist to prevent automatic recalculation is
    [VBA]application.Calculation = xlCalculationManual[/VBA]
    or one of the other two available constants

  7. #7
    VBAX Regular Chabu's Avatar
    Joined
    Dec 2010
    Location
    Brussels
    Posts
    85
    Location
    Nepotist,

    going back to the start of your request, it is always best to control quality while you are working instead of at the end of the day.
    (Quality control versus Quality assessment (or audit)

    So on top of doing the overview as described above, why don't you add a column in each sheet that checks the error.
    (maybe even enhance it with some conditional formatting)

  8. #8
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Chabu,

    Thanks for pointing that out (as he is dealing with UDFs and formulas and not some application/ worksheet based events). This reminds me that there's KB entry for this by Kenneth Hobson.
    http://www.vbaexpress.com/kb/getarticle.php?kb_id=1035
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  9. #9
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location
    Chabu and Shrivallabha, thank you for your inputs.

    Chabu, I have tried to set the calaculation to manual but it doesn't help. Also if you guys want to look at my code for all my UDF , please look at this thread
    http://www.vbaexpress.com/forum/showthread.php?t=37503

    Please provide your inputs for increasing the performance of the workbook and also if you help address the issue of the other thread.
    Thank you
    Last edited by nepotist; 05-23-2011 at 06:58 AM.
    I am a Newbie, soon to be a Guru

  10. #10
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location
    Chabu,
    We get DATA from other consultant and this QC makes few check and creates a summary of incorrect data information.
    I am a Newbie, soon to be a Guru

Posting Permissions

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