Sub start()
Application.ScreenUpdating = False
Set ws1 = Sheets("Start")
Set rngx = ws1.Range("a2", ws1.Range("a65536").End(xlUp))
For Each rx In rngx
rx.NumberFormat = "0000"
rx.Value = "'" & Right("0000" & rx.Text, 4) & ""
Next
Script = "'Generato il " & Date & vbNewLine & vbNewLine
Script = Script & "Set objFSO = CreateObject(" & Chr(34) & "Scripting.FileSystemObject" & Chr(34) & ")" & vbNewLine
Script = Script & "If Not objFSO.FolderExists(" & Chr(34) & ActiveWorkbook.Path & "\Cartelle_Rete" & Chr(34) & ") Then" & vbNewLine
Script = Script & "Set objFolder = objFSO.CreateFolder(" & Chr(34) & ActiveWorkbook.Path & "\Cartelle_Rete" & Chr(34) & ")" & vbNewLine
Script = Script & "End If" & vbNewLine
'_______________________________________________________________________
For Each rx In rngx
mainpath = ActiveWorkbook.Path & "\Cartelle_Rete"
Dop = rx.Offset(, 1).Text
Filiale = rx.Offset(, 2).Text
mainpath = mainpath & "\" & Dop
Script = Script & "If Not objFSO.FolderExists(" & Chr(34) & mainpath & Chr(34) & ") Then" & vbNewLine
Script = Script & "Set objFolder = objFSO.CreateFolder(" & Chr(34) & mainpath & Chr(34) & ")" & vbNewLine
Script = Script & "End If" & vbNewLine
mainpath = mainpath & "\" & Filiale
Script = Script & "If Not objFSO.FolderExists(" & Chr(34) & mainpath & Chr(34) & ") Then" & vbNewLine
Script = Script & "Set objFolder = objFSO.CreateFolder(" & Chr(34) & mainpath & Chr(34) & ")" & vbNewLine
Script = Script & "End If" & vbNewLine
Script = Script & "If Not objFSO.FolderExists(" & Chr(34) & mainpath & "\Fatturazione" & Chr(34) & ") Then" & vbNewLine
Script = Script & "Set objFolder = objFSO.CreateFolder(" & Chr(34) & mainpath & "\" & "Fatturazione" & Chr(34) & ")" & vbNewLine
Script = Script & "End If" & vbNewLine
Script = Script & "If Not objFSO.FolderExists(" & Chr(34) & mainpath & "\Fatturazione\Note di Credito" & Chr(34) & ") Then" & vbNewLine
Script = Script & "Set objFolder = objFSO.CreateFolder(" & Chr(34) & mainpath & "\Fatturazione\Note di Credito" & Chr(34) & ")" & vbNewLine
Script = Script & "End If" & vbNewLine
Script = Script & "If Not objFSO.FolderExists(" & Chr(34) & mainpath & "\Fatturazione\Note di Debito" & Chr(34) & ") Then" & vbNewLine
Script = Script & "Set objFolder = objFSO.CreateFolder(" & Chr(34) & mainpath & "\Fatturazione\Note di Debito" & Chr(34) & ")" & vbNewLine
Script = Script & "End If" & vbNewLine
Next
'_______________________________________________________________________
Open ActiveWorkbook.Path & "\CartelleRete.vbs" For Output As #1
Print #1, Script ' Script = string with the script to write
Close #1
End Sub