+ Reply to Thread
Results 1 to 7 of 7

vba copy and paste based on multiple loop criteria

Hybrid View

sarahcpa vba copy and paste based on... 04-10-2013, 06:11 PM
rylo Re: vba copy and paste based... 04-10-2013, 07:38 PM
sarahcpa Re: vba copy and paste based... 04-18-2013, 01:49 PM
Leith Ross Re: vba copy and paste based... 04-18-2013, 01:57 PM
sarahcpa Re: vba copy and paste based... 04-18-2013, 07:09 PM
rylo Re: vba copy and paste based... 04-18-2013, 07:37 PM
sarahcpa Re: vba copy and paste based... 04-19-2013, 10:29 AM
  1. #1
    Registered User
    Join Date
    04-05-2013
    Location
    Little Rock, Ar
    MS-Off Ver
    Excel 2007
    Posts
    4

    vba copy and paste based on multiple loop criteria

    Hello I really need some help with my coding in Excel 2007.
    I have 80 worksheets in my workbook. I am trying to evaluate each sheet so that if there is a "Y" in a certain range of worksheets ("M" for Monday), then that sheet's data will be pasted to the sheet that begins with P (for prior week). There are 10 of these PW1, PW2, etc.
    Dim ws  As Worksheet
    Dim ws1 As Worksheet
    
    Application.ScreenUpdating = False
    
    
    For Each ws In ActiveWorkbook.Worksheets
        If Left(ws.Name, 1) = "M" And ws.Range("B2").Value = "Y" Then   'evaluate if yes
            ws.Range("A4:R37").Copy
            ElseIf Left(ws1.Name, 1) = "P" And ws1.Range("A4").Value Is Null Then
            ws1.Range("a4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            Application.CutCopyMode = False
        End If
    Next ws
        Sheets("108 Compare").Select
        Range("A81").Select
    
    Application.ScreenUpdating = True
    the code fails on the ElseIf line.
    Last edited by Leith Ross; 04-18-2013 at 01:54 PM. Reason: Added Code Tags

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: vba copy and paste based on multiple loop criteria

    Hi

    ws1 is not assigned anywhere.

    rylo

  3. #3
    Registered User
    Join Date
    04-05-2013
    Location
    Little Rock, Ar
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: vba copy and paste based on multiple loop criteria

    Well Rylo, I'm not sure what you mean. I have ws1 setup in the Dim statement and the Elseif line. Do you have any suggestions as to how else to assign it?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: vba copy and paste based on multiple loop criteria

    Hello sarahcpa,

    Welcome to the Forum!

    The statement Dim ws1 As Worksheet creates a Worksheet Object variable. You must assign that object variable to an existing worksheet.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    04-05-2013
    Location
    Little Rock, Ar
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: vba copy and paste based on multiple loop criteria

    I'm still stumped. Tried to change the code up a bit, but now I'm getting "end if without block If" error.
    pleeeeeease someone help me you guys seem to think I know what I'm doing.

    'Application.ScreenUpdating = False
    
    Dim ws, Dest  As Worksheet
    
    
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Range("B2").Value = "Y" Then   'evaluate if it is a carryover
            ws.Range("A4:R37").Copy
            For Each Dest In ActiveWorkbook.Worksheets
                If Left(Dest.Name, 2) = "PW" And ws1.Range("A4").Value Is Null Then
                    Dest.Range("A4").Select
                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                      :=False, Transpose:=False
                    Application.CutCopyMode = False
                    Exit For
                End If
        End If
    Next ws
        
        Sheets("108 Compare").Select
        Range("A81").Select
    
    'Application.ScreenUpdating = True
    
    End Sub

  6. #6
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: vba copy and paste based on multiple loop criteria

    Hi

    Between your 2 end if statements, put in a next Dest. You haven't completed the for loop.

    rylo

  7. #7
    Registered User
    Join Date
    04-05-2013
    Location
    Little Rock, Ar
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: vba copy and paste based on multiple loop criteria

    YAY
    between your comments and more testing of individual pieces, I finally got it to work

    Application.ScreenUpdating = False
    
    ' Loop through all sheets in the workbook.
          For i = 1 To Sheets.Count
             Sheets(i).Unprotect
          Next i
    
    
    Dim ws, Dest  As Worksheet
    
    
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Range("B2").Value = "Y" Then   'evaluate if it is a carryover
            ws.Range("A4:R37").Copy
            For Each Dest In ActiveWorkbook.Worksheets
                If Left(Dest.Name, 2) = "PW" And IsEmpty(Dest.Range("A4").Value) Then
                    Dest.Range("A4").PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
                    False, Transpose:=False
                    Application.CutCopyMode = False
                    Exit For
                End If
            Next Dest
        End If
    Next ws

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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