+ Reply to Thread
Results 1 to 9 of 9

Modify code to import to all worksheets

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-21-2010
    Location
    Alta, Norway
    MS-Off Ver
    Excel 2003
    Posts
    119

    Modify code to import to all worksheets

    Hi,

    I am using the following code to import all files in a directory.
    Could someone please assist me in modifying the code, so that the imported files is pasted in all worksheets, not just the first (sheet1)?

    Sub Importere_filer()
    Dim mnthNum As Integer
    Dim myBook As Workbook
    Dim myRows As Long
    
    With Application
        .DisplayAlerts = False
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    With Application.FileSearch
        .NewSearch
        'Change this to your directory
        On Error GoTo ErrHandler:
        .LookIn = "H:\div\"
        .SearchSubFolders = False
        .Filename = "*.txt"
        If .Execute() > 0 Then
            For i = 1 To .FoundFiles.Count
                Workbooks.OpenText .FoundFiles(i), , , xlDelimited, , , , True
                Set myBook = ActiveWorkbook
                myRows = ThisWorkbook.Worksheets(1).Range("A65536").End(xlUp).Row + 1
                    ActiveSheet.UsedRange.Copy _
                    ThisWorkbook.Worksheets(1).Cells(myRows, 1)
                    myRows = myRows + 1
                myBook.Close
             Next i
        End If
    
    ErrHandler:
    End With
    
    With Application
        .DisplayAlerts = True
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    
    End Sub
    Duplicate: http://www.mrexcel.com/forum/showthread.php?t=484275

    Gnoke
    Last edited by gnoke; 08-10-2010 at 09:48 AM.

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Modify code to all worksheets

    Untested

    Sub Importere_filer()
    Dim mnthNum As Integer
    Dim myBook As Workbook
    Dim myRows As Long
    Dim NextRow As Long
    Dim i As Long, j As Long
    
    	With Application
    		.DisplayAlerts = False
    		.EnableEvents = False
    		.ScreenUpdating = False
    	End With
    
    	With Application.FileSearch
    		.NewSearch
    		'Change this to your directory
    		On Error GoTo ErrHandler:
    		.LookIn = "H:\div\"
    		.SearchSubFolders = False
    		.Filename = "*.txt"
    		If .Execute() > 0 Then
    			For i = 1 To .FoundFiles.Count
    				Workbooks.OpenText .FoundFiles(i), , , xlDelimited, , , , True
    				Set myBook = ActiveWorkbook
    				For j = 1 To myBook.Worksheets.Count
    					myRows = ThisWorkbook.Worksheets(1).Range("A" & ThisWorkbook.Worksheets(1).Rows.Count).End(xlUp).Row + 1
    					myBook.Worksheets(j).UsedRange.Copy _
    					ThisWorkbook.Worksheets(1).Cells(myRows, 1)
    					myRows = myRows + 1
    				myBook.Close
    			 Next i
    		End If
    
    	ErrHandler:
    	End With
    
    	With Application
    		.DisplayAlerts = True
    		.EnableEvents = True
    		.ScreenUpdating = True
    	End With
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    07-21-2010
    Location
    Alta, Norway
    MS-Off Ver
    Excel 2003
    Posts
    119

    Re: Modify code to all worksheets

    Thanks for replay!

    I get the following error: "Invalid Next control variable reference" on line number 10 from the buttom "Next i"
    Any ideas?
    Last edited by gnoke; 07-28-2010 at 04:46 AM. Reason: spelling mistake

  4. #4
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Modify code to all worksheets

    I forgot to close my new loop

    Sub Importere_filer()
    Dim mnthNum As Integer
    Dim myBook As Workbook
    Dim myRows As Long
    Dim NextRow As Long
    Dim i As Long, j As Long
    
    	With Application
    		.DisplayAlerts = False
    		.EnableEvents = False
    		.ScreenUpdating = False
    	End With
    
    	With Application.FileSearch
    		.NewSearch
    		'Change this to your directory
    		On Error GoTo ErrHandler:
    		.LookIn = "H:\div\"
    		.SearchSubFolders = False
    		.Filename = "*.txt"
    		If .Execute() > 0 Then
    			For i = 1 To .FoundFiles.Count
    				Workbooks.OpenText .FoundFiles(i), , , xlDelimited, , , , True
    				Set myBook = ActiveWorkbook
    				For j = 1 To myBook.Worksheets.Count
    					myRows = ThisWorkbook.Worksheets(1).Range("A" & ThisWorkbook.Worksheets(1).Rows.Count).End(xlUp).Row + 1
    					myBook.Worksheets(j).UsedRange.Copy _
    					ThisWorkbook.Worksheets(1).Cells(myRows, 1)
    					myRows = myRows + 1
    				Next j
    				myBook.Close
    			 Next i
    		End If
    
    	ErrHandler:
    	End With
    
    	With Application
    		.DisplayAlerts = True
    		.EnableEvents = True
    		.ScreenUpdating = True
    	End With
    
    End Sub

  5. #5
    Forum Contributor
    Join Date
    07-21-2010
    Location
    Alta, Norway
    MS-Off Ver
    Excel 2003
    Posts
    119

    Re: Modify code to all worksheets

    Thanks again for your replay!

    Now the macro runs, but I see no differens in the output compared to the prior macro.
    The text files are not imported to any sheets except the first...

    Ideas someone?
    Last edited by gnoke; 07-29-2010 at 03:24 AM.

  6. #6
    Forum Contributor
    Join Date
    07-21-2010
    Location
    Alta, Norway
    MS-Off Ver
    Excel 2003
    Posts
    119

    Re: Modify code to all worksheets

    Anyone else that have any ideas?
    Would reallt appreciate any further assistance

  7. #7
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Modify code to import to all worksheets

    Maybe I misunderstood the brief. Try this version

    Sub Importere_filer()
    Dim mnthNum As Long
    Dim myRows As Long
    Dim NextRow As Long
    Dim i As Long, j As Long
    
        With Application
            .DisplayAlerts = False
            .EnableEvents = False
            .ScreenUpdating = False
        End With
    
        With Application.FileSearch
            .NewSearch
            'Change this to your directory
            On Error GoTo ErrHandler:
            .LookIn = "H:\div\"
            .SearchSubFolders = False
            .Filename = "*.txt"
            If .Execute() > 0 Then
                For i = 1 To .FoundFiles.Count
                    Workbooks.OpenText .FoundFiles(i), , , xlDelimited, , , , True
                    For j = 1 To ThisWorkbook.Worksheets.Count
                        myRows = ThisWorkbook.Worksheets(j).Range("A" & _
                            ThisWorkbook.Worksheets(j).Rows.Count).End(xlUp).Row + 1
                        ActiveSheet.UsedRange.Copy _
                            ThisWorkbook.Worksheets(j).Cells(myRows, 1)
                    Next j
                    myBook.Close
                 Next i
            End If
            
    ErrHandler:
        End With
    
        With Application
            .DisplayAlerts = True
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    End Sub

  8. #8
    Forum Contributor
    Join Date
    07-21-2010
    Location
    Alta, Norway
    MS-Off Ver
    Excel 2003
    Posts
    119

    Re: Modify code to import to all worksheets

    Hi and thanks again Bob!

    We are defininitly getting somewhere now, but still have two issues.
    1. The macro imports to all sheets nicely, but only one text file, not all text files in the specified folder.
    2. The single text file is also imported to a new workbook...

    Gnoke

  9. #9
    Forum Contributor
    Join Date
    07-21-2010
    Location
    Alta, Norway
    MS-Off Ver
    Excel 2003
    Posts
    119

    Re: Modify code to import to all worksheets

    Solution for me:

    1. I use this code to import all text files into a single sheet:

    Sub Import_Text()
    Dim mnthNum As Integer
    Dim myBook As Workbook
    Dim myRows As Long
    
    With Application
        .DisplayAlerts = False
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    With Application.FileSearch
        .NewSearch
        'Change this to your directory
        On Error GoTo ErrHandler:
        .LookIn = "C:\sample\"
        .SearchSubFolders = False
        .Filename = "*.txt"
        If .Execute() > 0 Then
            For i = 1 To .FoundFiles.Count
                Workbooks.OpenText .FoundFiles(i), , , xlDelimited, , , , True
                Set myBook = ActiveWorkbook
                myRows = ThisWorkbook.Worksheets(1).Range("A65536").End(xlUp).Row + 1
                    ActiveSheet.UsedRange.Copy _
                    ThisWorkbook.Worksheets(1).Cells(myRows, 1)
                    myRows = myRows + 1
                myBook.Close
             Next i
        End If
    2. Then i created new macros to delete rows with specific values in specific cells (or else delete rows with empty cells).
    3. Then I recorded simple macros to e.g. arrange the rows ascending based text in column A.
    4. In the end I recorded a macro while running all the simple macros.

    This is probably not the most efficient for my job, but it does what I need
    Thanks for all help!
    Gnoke

+ 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