Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 23

Discussione: copia file

  1. #1

    copia file

    Salve a tutti,

    Il mio problema è questo.
    Vorrei copiare i dati del primo foglio di più file excel in un unico file.
    Ad esempio ho 30 file excel nella stessa cartella.
    Ho la necessità di copiare i dati del primo foglio di tutti i file nella cartella in un unico file che vado a creare.
    E' possibile tutto ciò???
    Vi prego aiutatemi

  2. #2
    ciao, certo che è possibile, potresti utilizzare i namespace system.io per la gestione dei file (fare un ciclo nella certella) e poi, caricare i dati da file a file.
    Per interrogare i dati, puoi fare una select, vedi questa connectionstring http://www.connectionstrings.com/?carrier=excel mentre per utilizzare excel da .net, vedi questo articolo http://blog.shareoffice.it/emanuele/articles/200.aspx

    Ciao EMa
    Sito Web:
    http://blog.shareoffice.it/emanuele
    Blog Personale
    http://emanuelemattei.blogspot.com/

  3. #3
    Allora ti spiego meglio il problema.

    Ho una cartella dove stanno 30 file excel nominati con i numeri dal primo che si chiama 1 all'ultimo che si chiama 30 che contengono 5 fogli.
    Allora io devo fare in modo di creare un nuovo foglio excel nel quanle vanno a finire tutti i dati del primo foglio excel di tutti i file.
    Diciamo che per fare ciò in parte ci sono anche riuscito e se vuoi ti mostro anche il codice.
    Quello che vorrei ora è questo:
    Sempre all'interno della stessa cartella devo fare in modo che selezionando il percorso di un file, ad esempio il numero 5, vorrei che mi copiasse sempre in un'altro file tutti i dati del primo foglio partendo da quello selezionato fino al 30 escludendo i primi 4.
    E' possibile??

  4. #4

  5. #5
    visual basic edito quello di excel

  6. #6
    Utente di HTML.it L'avatar di gibra
    Registrato dal
    Apr 2008
    residenza
    Italy
    Messaggi
    4,244
    VBA di Excel?
    Allora devi guardati la guida, ovviamente non quella di Excel, ma quella di VBA per Excel che puoi aprire quando sei nella finestra di progettazione VBA.

    A grandi linee dovresti procedere così:

    - Crei il nuovo file Excel (Tutto.xls) che dovrà contenere tutti gli altri
    - apri il file 1.xls, selezioni la cartella da copiare
    - copi la cartella e la aggiungi al nuovo Tutto.xls
    - procedi fino all'ultimo

    Guarda qui
    http://support.microsoft.com/kb/177834/en-us

    e qui
    http://www.eggheadcafe.com/software/...copy-a-sh.aspx

    Ciao

  7. #7
    Scusa se ti disturbo ancora. Di seguito ti riporto questo codice il quale mi permette di copiare il foglio 1 di tutti i file in un unico file.


    ---------------------------------



    Private Sub CommandButton5_Click()
    On Error GoTo ErrorHandler
    Const cWbExt = "*.xls"
    Const cWshIndex = 1
    Dim strPathSep As String
    Dim strPathName As String
    Dim strMyName As String
    Dim strFilename As String
    Dim wbIn As Excel.Workbook
    Dim wshIn As Excel.Worksheet
    Dim wshOut As Excel.Worksheet
    Dim rngOut As Excel.Range

    With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    strPathSep = .PathSeparator
    With .ThisWorkbook
    strPathName = .Path
    strMyName = .Name
    With .Worksheets
    Set wshOut = .Add(Before:=.Item(1))
    End With
    End With
    End With
    If Right$(strPathName, 1) <> strPathSep Then
    strPathName = strPathName & strPathSep
    End If
    strPathName = strPathName
    strFilename = Dir(strPathName & cWbExt, vbNormal)
    Do While Len(strFilename)
    If strFilename <> strMyName Then
    'Debug.Print strFilename
    Set wbIn = Workbooks.Open(strPathName & strFilename _
    , ReadOnly:=True _
    , AddToMru:=False)
    'Debug.Print , wbIn.FullName
    Set wshIn = wbIn.Worksheets.Item(cWshIndex)
    'Debug.Print , , wshIn.Name
    With wshOut.UsedRange
    If .Rows.Count = 1 Then
    Set rngOut = .Cells(1, 1)
    Else
    Set rngOut = .Resize(1 _
    , 1).Offset(.Rows.Count)
    End If
    With wshIn.Cells
    .Range(.Item(1, 1) _
    , .Find("*" _
    , After:=.Cells(1, 1) _
    , LookIn:=xlFormulas _
    , LookAt:=xlPart _
    , SearchOrder:=xlByRows _
    , SearchDirection:=xlPrevious _
    , MatchCase:=False) _
    ).Copy rngOut
    End With
    End With
    wbIn.Close SaveChanges:=False
    End If
    strFilename = Dir
    Loop

    ExitProcedure:
    With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
    End With
    Set rngOut = Nothing
    Set wshOut = Nothing
    Set wshIn = Nothing
    Set wbIn = Nothing
    Exit Sub

    ErrorHandler:
    MsgBox Err.Description, vbCritical
    Resume ExitProcedure
    End Sub


    --------------------------------

    Lui mi crea questo nuovo file sulla cartella di lavoro che sto utilizzando, ma io vorrei fare in moddo che creasse direttamente lui un nuovo file in un percorso scelto da me o già impostato.Inoltre, mi servirebbe capire quando copia i fogli, perchè il primo fogli che devo copiare dei 30 mi dovrebbe copiare tutti e 5 i fogli mentre delgli altri 29 solo il primo. Sapresti dirmi dove effettuare le modifiche.

    Grazie

  8. #8
    Utente di HTML.it L'avatar di gibra
    Registrato dal
    Apr 2008
    residenza
    Italy
    Messaggi
    4,244
    Riformatta il codice adeguatamente, perchè è incomprensibile messo così tutto sullo stesso livello di identazione.

    Ciao

  9. #9
    Private Sub CommandButton5_Click()
    On Error GoTo ErrorHandler
    Const cWbExt = "*.xls"
    Const cWshIndex = 1
    Dim strPathSep As String
    Dim strPathName As String
    Dim strMyName As String
    Dim strFilename As String
    Dim wbIn As Excel.Workbook
    Dim wshIn As Excel.Worksheet
    Dim wshOut As Excel.Worksheet
    Dim rngOut As Excel.Range

    With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    strPathSep = .PathSeparator
    With .ThisWorkbook
    strPathName = .Path
    strMyName = .Name
    With .Worksheets
    Set wshOut = .Add(Before:=.Item(1))
    End With
    End With
    End With

    If Right$(strPathName, 1) <> strPathSep Then
    strPathName = strPathName & strPathSep
    End If

    strPathName = strPathName
    strFilename = Dir(strPathName & cWbExt, vbNormal)
    Do While Len(strFilename)
    If strFilename <> strMyName Then
    'Debug.Print strFilename
    Set wbIn = Workbooks.Open(strPathName & strFilename, ReadOnly:=True _
    , AddToMru:=False)
    'Debug.Print , wbIn.FullName
    Set wshIn = wbIn.Worksheets.Item(cWshIndex)
    'Debug.Print , , wshIn.Name
    With wshOut.UsedRange
    If .Rows.Count = 1 Then
    Set rngOut = .Cells(1, 1)
    Else
    Set rngOut = .Resize(1, 1).Offset(.Rows.Count)
    End If
    With wshIn.Cells
    .Range(.Item(1, 1) _
    , .Find("*" _
    , After:=.Cells(1, 1) _
    , LookIn:=xlFormulas _
    , LookAt:=xlPart _
    , SearchOrder:=xlByRows _
    , SearchDirection:=xlPrevious _
    , MatchCase:=False) _
    ).Copy rngOut
    End With
    End With
    wbIn.Close SaveChanges:=False
    End If
    strFilename = Dir
    Loop

    ExitProcedure:
    With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
    End With
    Set rngOut = Nothing
    Set wshOut = Nothing
    Set wshIn = Nothing
    Set wbIn = Nothing
    Exit Sub

    ErrorHandler:
    MsgBox Err.Description, vbCritical
    Resume ExitProcedure
    End Sub



    -----------------
    Questo codice lo uso tutto in un command button...

  10. #10
    Originariamente inviato da gibra
    Riformatta il codice adeguatamente, perchè è incomprensibile messo così tutto sullo stesso livello di identazione.
    E usa il tag [code]...[/code], che se no l'indentazione sparisce.
    Amaro C++, il gusto pieno dell'undefined behavior.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.