Copy a Specific Worksheet to a new Workbook using VBA

syednizamudeen

New Member
Joined
Jul 27, 2016
Messages
4
[FONT=&quot]Hi all,
[/FONT]

[FONT=&quot]
[/FONT]

[FONT=&quot]I have a folder which contains like a 100 .xlsx files. I want a Macro which uses the Folder Picker to input the source folder. then, the macro should look for each workbook in the folder and and look for every worksheet in the workbook for C15="5/7/2016".

If a specific worksheet has the value in the cell C15="5/7/2016", it has to be copied to a new workbook.
[/FONT]

[FONT=&quot]
[/FONT]

[FONT=&quot]My code goes like this

[/FONT]

[FONT=&quot] [/FONT][FONT=&quot]Sub WksToWbk()
Dim strFolder As String
Dim strFile As String
Dim wbk As Workbook
Dim wks As Worksheet
Dim cnt As Long
'Prompt user to select folder
With Application.FileDialog(4) ' msoFileDialogFolderPicker
If .Show Then
strFolder = .SelectedItems(1)
Else
MsgBox "No folder selected!", vbExclamation
Exit Sub
End If
End With
' Make sure folder path ends in \
If Right(strFolder, 1) <> "\" Then
strFolder = strFolder & "\"
End If
On Error GoTo ErrHandler
' Reduce screen flicker
Application.ScreenUpdating = False
' Get first filename
strFile = Dir(strFolder & "*.xlsx")
' Loop through files
Do While strFile <> ""
' Open workbook
Set wbk = Workbooks.Open(Filename:=strFolder & strFile)
For Each wks In ActiveWorkbook.Worksheets
If wks.Range("C15").Value = "5/7/2016" Then
wks.Copy
With ActiveWorkbook
.SaveAs Filename:="C:\Users\Meera\Documents\Copy Ws to new Wbook Macro" _
& "\File" & cnt & ".xlsx"
.Close
End With
cnt = cnt + 1
End If
Next wks
Loop
MsgBox ("Created" & cnt & " Excel Files")
ExitHandler:
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub


[/FONT]

[FONT=&quot]Present code does not close the workbook and and look for the next workbook...there is some problem with the files and worksheet looping...Please help me to correct the above code.
[/FONT]

[FONT=&quot]


[/FONT]
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try this.
Code:
Sub WksToWbk()
Dim strFolder As String
Dim strFile As String
Dim wbk As Workbook
Dim wks As Worksheet
Dim cnt As Long

    'Prompt user to select folder
    With Application.FileDialog(4)    ' msoFileDialogFolderPicker
        If .Show Then
            strFolder = .SelectedItems(1)
        Else
            MsgBox "No folder selected!", vbExclamation
            Exit Sub
        End If
    End With
    
    ' Make sure folder path ends in \
    If Right(strFolder, 1) <> "\" Then
        strFolder = strFolder & "\"
    End If
    
    On Error GoTo ErrHandler
    ' Reduce screen flicker
    Application.ScreenUpdating = False
    ' Get first filename
    strFile = Dir(strFolder & "*.xlsx")
    ' Loop through files
    Do While strFile <> ""
    ' Open workbook
        Set wbk = Workbooks.Open(Filename:=strFolder & strFile)
        
        For Each wks In ActiveWorkbook.Worksheets
            If wks.Range("C15").Value = "5/7/2016" Then
                wks.Copy
                With ActiveWorkbook
                    .SaveAs Filename:="C:\Users\Meera\Documents\Copy Ws to new Wbook Macro" _
                                      & "\File" & cnt & ".xlsx"
                    .Close
                End With
                cnt = cnt + 1
            End If
        Next
        
        wbk.Close SaveChanges:=False

        strFile = Dir()
    Loop
    
    MsgBox ("Created" & cnt & " Excel Files")
    
ExitHandler:
    Application.ScreenUpdating = True
    Exit Sub
    
ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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