Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

Simple VBA Loop

See code below.  This is my first attempt at a loop!
The precise functionality is not important.

However, note that the same process is performed on two different sheets. (Yes, I know they have bad names!).  The code is therefore duplicated.

What is the syntax to loop through 2 sheets. Perhaps the following??

Dim strSheet as string
For strSheet in  Sheets("C'YearTrendP&L-IRE(24)"), Sheets("C'tYearTrendP&L-UK(25)")

??
Sub pastey()
'
    Dim strPM1
    strPM1 = Range("PreviousMonth1")
    
    Sheets("C'YearTrendP&L-IRE(24)").Select
      
    Columns(strPM1 & ":" & strPM1).Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    
    Sheets("C'tYearTrendP&L-UK(25)").Select
    Columns(strPM1 & ":" & strPM1).Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    
    Sheets("IRETB(23) ").Select
    MsgBox "Finished Copying and Pasting"
End Sub

Open in new window

0
Patrick O'Dea
Asked:
Patrick O'Dea
3 Solutions
 
SiddharthRoutCommented:
There is no need to loop. What you are doing can be further refined to

Sub pastey()
    Dim strPM1
    strPM1 = Range("PreviousMonth1")
    
    With Sheets("C'YearTrendP&L-IRE(24)").Columns(strPM1 & ":" & strPM1)
        .Copy
        .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    End With
    
    With Sheets("C'tYearTrendP&L-UK(25)").Columns(strPM1 & ":" & strPM1)
        .Copy
        .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    End With
    
    MsgBox "Finished Copying and Pasting"
End Sub

Open in new window


Sid
0
 
SiddharthRoutCommented:
If you still want a loop then try this...

Sub pastey()
    Dim strPM1
    Dim ws As Worksheet, Wks As Worksheet
    
    '~~> Replace ????? with the relevant sheetname
    'strPM1 = Sheets("????").Range("PreviousMonth1")
    
    For Each ws In ThisWorkbook.Sheets
        If ws.Name = "C'YearTrendP&L-IRE(24)" Then
            Set Wks = Sheets("C'YearTrendP&L-IRE(24)")
        ElseIf ws.Name = "C'tYearTrendP&L-UK(25)" Then
            Set Wks = Sheets("C'tYearTrendP&L-UK(25)")
        End If
        
        If Not Wks Is Nothing Then
            With Wks.Columns(strPM1 & ":" & strPM1)
                .Copy
                .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
            End With
        End If
    Next
    
    MsgBox "Finished Copying and Pasting"
End Sub

Open in new window


Sid
0
 
MelakhCommented:
Sub pastey()
'
   Dim arrSheetNames as Variant
   Dim strPM1
   Dim i as integer

   arrSheetNames = ("C'YearTrendP&L-IRE(24)", "C'tYearTrendP&L-UK(25)")
   strPM1 = Range("PreviousMonth1")

   For i = LBound(arrSheetNames) to UBound(arrSheetNames)
      Sheets(arrSheetNames(i)).Select
       Columns(strPM1 & ":" & strPM1).Select
       Selection.Copy
       Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
           :=False, Transpose:=False
   Next i

   Sheets("IRETB(23) ").Select
   MsgBox "Finished Copying and Pasting"

End Sub

Open in new window


There are better ways to do the copy and paste, you don't need to hop between sheets in VBA, but this is a simple edit to your code so you should be able to maintain it.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Patrick O'DeaAuthor Commented:
Thanks siddharthrout,

I may need to run this procedure for up to 10 or 11 sheets.

Would a loop be neater in this case??
0
 
SiddharthRoutCommented:
I have already replied. See my 2nd post above ;)

Sid
0
 
SiddharthRoutCommented:
If it is for all sheets then the code that I gave can be further trimmed :)

Sid
0
 
SiddharthRoutCommented:
Please follow this code in case you want to perform the copy past in all the sheets.

Sub pastey()
    Dim strPM1
    Dim ws As Worksheet, Wks As Worksheet
    
    '~~> Replace ????? with the relevant sheetname
    'strPM1 = Sheets("????").Range("PreviousMonth1")
    
    For Each ws In ThisWorkbook.Sheets
        Set Wks = Sheets(ws.Name)
        
        With Wks.Columns(strPM1 & ":" & strPM1)
            .Copy
            .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        End With
    Next
    
    MsgBox "Finished Copying and Pasting"
End Sub

Open in new window


Sid
0
 
Patrick O'DeaAuthor Commented:
Thanks sid,

It is NOT for all sheets so your first solution works fine.

Can you advise what the line below means?  I would not like to use the code without understanding it.
See the following line.  Thanks.

 If Not Wks Is Nothing Then
0
 
SiddharthRoutCommented:
Sure :)

I have declared Wks as a worksheet. So when I am looping through the worksheet, when I find a match, I am storing that in Wks. I don't want the code copy paste to run each time the for 'loops' as it will give me an error as Wks would be empty. To stop that I used the line

If Not Wks Is Nothing Then

Sid
0
 
dlmilleCommented:
You were very close...

Sub pastey()
Dim sh As Variant, mySheets() As String
Dim strPM1
   
    mySheets() = Split("C'YearTrendP&L-IRE(24);C'tYearTrendP&L-UK(25)", ";")

    For Each sh In mySheets
        Columns(strPM1 & ":" & strPM1).Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    Next sh
   
    Sheets("IRETB(23) ").Select
    MsgBox "Finished Copying and Pasting"
End Sub
0
 
Patrick O'DeaAuthor Commented:
Thanks all,

Working well now.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now