a nested Do Loop for workbook and sheets

GerryZ

Well-known Member
Joined
Jul 4, 2014
Messages
1,224
Office Version
  1. 365
Platform
  1. Windows
Hello Friends
I'm learning VBA
I am trying to print on the screen the result of 2 nested Loops with the Do Loop, the code I created with some difficulty manages to print only the name of the WorkBook but it does not print the sheets names , maybe I make some mistakes
If anyone can help me
A greeting
VBA Code:
Option Explicit
Sub UnisciTuttiIFile()

Dim NomiFile As String
NomiFile = Dir("C:\Users\Utente\Documents\Vendite\Anni\")

    Do Until NomiFile = ""
        Debug.Print NomiFile
                    Do
                        Debug.Print ActiveSheet.Name
                        If ActiveSheet.Next Is Nothing Then Exit Do
                    Loop
        NomiFile = Dir
    Loop
End Sub

EDIT I've the OutPut in another sheet
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi Peters,
I'm well About You??
Any way I solved in this way
with this line of code before the Do Loop so the procedures started from the first sheet
VBA Code:
    FileDelleVendite.Worksheets(1).Select

Here is the the entire Code, of course i Used DEBUG.PRINT to check if the Loop Work Correctly


Rich (BB code):
Option Explicit
Sub UnisciTuttiIFile()
Dim NomiFile As String
Dim FileDelleVendite As Workbook
Dim RisultatoFinale As Worksheet
Dim ws As Worksheet

Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
    If ws.CodeName <> "wks1" Then ws.Delete
Next
Application.DisplayAlerts = True
Set RisultatoFinale = ThisWorkbook.Worksheets.Add

NomiFile = Dir("C:\Users\Utente\Documents\Vendite\Anni\")
    Do Until NomiFile = ""
    Set FileDelleVendite = Workbooks.Open("C:\Users\Utente\Documents\Vendite\Anni\" & NomiFile)
'   Debug.Print NomiFile
    FileDelleVendite.Worksheets(1).Select
                        Do
        '                    Debug.Print ActiveSheet.Name
                            ActiveSheet.Range("A1").CurrentRegion.Offset(1).Copy
                            RisultatoFinale.Range("A999999").End(xlUp).Offset(1, 0).PasteSpecial
                            If ActiveSheet.Next Is Nothing Then Exit Do
                            ActiveSheet.Next.Select
                        Loop
            NomiFile = Dir
            FileDelleVendite.Close
    Loop
Workbooks.Open "C:\Users\Utente\Documents\Vendite\Anni\2021.xlsx"
Worksheets(1).Range("A1:D1").Copy RisultatoFinale.Range("A1:D1")
Workbooks("2021.xlsx").Close
RisultatoFinale.Columns.AutoFit
Application.ScreenUpdating = True
End Sub
Ciao Peters ti aspetto in Italia
 
Last edited by a moderator:
Upvote 0
TRy
VBA Code:
 ActiveWorkbook.Worksheets(1).Select
Instead

VBA Code:
FileDelleVendite.Worksheets(1).Select
 
Upvote 0
Solution
TRy
VBA Code:
 ActiveWorkbook.Worksheets(1).Select
Instead

VBA Code:
FileDelleVendite.Worksheets(1).Select
Thank You! Mohadin i will try thank you for the answear
 
Upvote 0

Forum statistics

Threads
1,214,893
Messages
6,122,121
Members
449,066
Latest member
Andyg666

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