Sheets Array

Boffa

New Member
Joined
May 8, 2019
Messages
27
Hi guys, have had a look online but couldn't find anything particular to my situation.

I want to copy an array of sheets where the sheet name ends in the word Log - The Sheet name could have anything at the start of the sheet name but it ends in Log - usually there will only be 2 sheets that end in the name 'log' but there are times there is only one hence would like to copy an array

below is what I have attempted but does not work

Sheets(Array("*LOG")).Copy

Any assistance would be appreciated..
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
I think you will need to loop through the sheets to copy them....something like this
VBA Code:
Sub MM2()
Dim ws As Worksheet
For Each ws In Worksheets
    If Right(ws.Name, 3) = "LOG" Then
        ws.Copy
        'Do more code here
    End If
Next ws
End Sub
 
Upvote 0
I think you will need to loop through the sheets to copy them....something like this
VBA Code:
Sub MM2()
Dim ws As Worksheet
For Each ws In Worksheets
    If Right(ws.Name, 3) = "LOG" Then
        ws.Copy
        'Do more code here
    End If
Next ws
End Sub
That would create one new workbook for each 'LOG' sheet whereas the OP's attempted code appears to be trying to create a new workbook containing all 'LOG' sheets.

My suggestion (also allowing for upper/lower case letters for the 'LOG' sheets) is:

VBA Code:
Sub CopyLogSheets()
  Dim ws As Worksheet
  Dim LogSheets As String
  
  For Each ws In Worksheets
    If UCase(Right(ws.Name, 3)) = "LOG" Then LogSheets = LogSheets & "/" & ws.Name
  Next ws
  If Len(LogSheets) > 0 Then Sheets(Split(Mid(LogSheets, 2), "/")).Copy
End Sub
 
Upvote 0
@Peter_SSs
I disagree ...the one line in my code that allows the OP to decide what they want to do is
Rich (BB code):
'Do more code here
They can then either save each sheet to a new workbook or each sheet to the one new workbook.
I don't think I specified in the code anywhere that it was a new workbook for each worksheet, I simply said
Rich (BB code):
ws.copy
 
Upvote 0
You can build an array to copy required sheets

Untested but maybe something like following

VBA Code:
Sub CopyLogSheets()
    Dim SheetsArray() As String
    Dim i As Integer
    Dim sh As Worksheet

    For Each sh In ThisWorkbook.Worksheets
        If UCase(sh.Name) Like "*LOG" Then
        i = i + 1
        ReDim Preserve SheetsArray(1 To i)
        SheetsArray(i) = sh.Name
        End If
    Next sh

    If i > 0 Then Worksheets(SheetsArray()).Copy
End Sub

Dave
 
Upvote 0
I don't think I specified in the code anywhere that it was a new workbook for each worksheet, I simply said
Rich (BB code):
ws.copy
But that is exactly what ws.Copy in a loop will do.

However, I am happy to wait & see if any of the suggestions is actually what the OP wants. :)
 
Upvote 0
Thanks for the quick reply's guys much appreciated

dmt32 your code worked perfectly for what i need it to do
 
Upvote 0
Thanks for the quick reply's guys much appreciated

dmt32 your code worked perfectly for what i need it to do

Glad forum was able to help you & found a solution - many thanks for feedback, very much appreciated by all.

Dave
 
Upvote 0
Thanks for the quick reply's guys much appreciated

dmt32 your code worked perfectly for what i need it to do
Glad you got something that worked for you. I agree that dmt32's code does what you wanted, though I believe the code I posted does also. :)
 
Upvote 0

Forum statistics

Threads
1,214,962
Messages
6,122,482
Members
449,088
Latest member
Melvetica

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top