Macro creating blank sheets

fari1

Active Member
Joined
May 29, 2011
Messages
362
Code:
Sub Macro1()
     '
     ' Macro1 Macro
     ' Macro recorded 15/05/2006 by Andy Pope
     '
    Dim strPath As String
    Dim strFile As String
     '
    strPath = "C:\companyinfo\Output\1\"
    strFile = Dir(strPath & "*.csv")
    Do While strFile <> ""
        With ActiveWorkbook.Worksheets.Add
            With .QueryTables.Add(Connection:="TEXT;" & strPath & strFile, _
                Destination:=.Range("A1"))
                .Parent.Name = Replace(strFile, ".csv", "")
                .TextFileParseType = xlDelimited
                .TextFileTextQualifier = xlTextQualifierDoubleQuote
                .TextFileConsecutiveDelimiter = False
                .TextFileTabDelimiter = False
                .TextFileSemicolonDelimiter = False
                .TextFileCommaDelimiter = True
                .TextFileSpaceDelimiter = False
                .TextFileColumnDataTypes = Array(1)
                .TextFileTrailingMinusNumbers = True
                .Refresh BackgroundQuery:=False
            End With
        Call deleteConnections
        Call RemNamedRanges
        End With
        strFile = Dir
    Loop
End Sub

i've above macro which takes workbooks out of a folder and creates one single workbook of all of them, the problem is it creates the blanks workbooks as well, dont sure which part of it is creating the problems, any ideas?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I don't see any obvious reason for the blank worksheets except that the corresponding .csv file is empty.
Your code creates a new sheet for every .csv file it finds in the folder, then puts the query in it - perhaps you can check for an empty sheet and delete that sheet if it is empty. You could use the likes of Application.CountA to detect non-empty cells in a range in the new sheet, and if that count is 0, delete the sheet.
 
Upvote 0
the folder in which it is looping contains only one worksheet in each workbook, and i found out that the code is creating as many blank worksheets as there as named worksheets, is there any problem with the path?
 
Upvote 0
I run the code myself, and only one sheet per .csv is created. I had to comment out Call deleteConnections and Call RemNamedRanges, because I don't have those routines. Could they be adding sheets?
 
Upvote 0
delete connections is deleting the workbooks web query connections and remnamedranges is deleting named ranges.
i got to see which thing is creating problems here, might be some issue with my folder and path.
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,220
Members
452,895
Latest member
BILLING GUY

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