Condividi:        

[Excel]-Macro copia fogli

Vuoi potenziare i tuoi documenti Word? Non sai come si fa una macro in Excel? Devi creare una presentazione in PowerPoint?
Oppure sei passato a OpenOffice e non sei sicuro di come lavorare al meglio?

Moderatori: Anthony47, Flash30005

[Excel]-Macro copia fogli

Postdi mmitola » 26/03/12 14:58

Ciao ragazzi avrei bisogno di una mano....

Ho scritto questa macro che mi consente di copiare il contenuto presente in file diversi e mi dà la possibilità di copiare il tutto in un unico file....

Il problema è il seguente:

premettendo che tutti i file hanno la stessa identica formattazione,su quello "totale" non mi mantiene la formattazione, come posso fare?

Vi scrivo l'intero codice....per favore ditemi dove sbaglio...


Vi ringrazio anticipatamente per l'aiuto.....

Codice: Seleziona tutto
Sub MergeSelectedWorkbooks()
    Dim SummarySheet As Worksheet
    Dim FolderPath As String
    Dim SelectedFiles() As Variant
    Dim NRow As Long
    Dim FileName As String
    Dim NFile As Long
    Dim WorkBk As Workbook
    Dim SourceRange As Range
    Dim DestRange As Range
   
    ' Create a new workbook and set a variable to the first sheet.
    Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
   
    ' Modify this folder path to point to the files you want to use.
    FolderPath = "C:\Users\mmitola\Desktop\PREVISIONI\Notiziari 2012\Area EST rev01"
   
    ' Set the current directory to the the folder path.
    ChDrive FolderPath
    ChDir FolderPath
   
    ' Open the file dialog box and filter on Excel files, allowing multiple files
    ' to be selected.
    SelectedFiles = Application.GetOpenFilename( _
        filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
   
    ' NRow keeps track of where to insert new rows in the destination workbook.
    NRow = 1
   
    ' Loop through the list of returned file names
    For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
        ' Set FileName to be the current workbook file name to open.
        FileName = SelectedFiles(NFile)
       
        ' Open the current workbook.
        Set WorkBk = Workbooks.Open(FileName)
       
        ' Set the cell in column A to be the file name.
        SummarySheet.Range("A" & NRow).Value = FileName
       
        ' Set the source range to be A1 through F28.
        ' Modify this range for your workbooks. It can span multiple rows.
        Set SourceRange = WorkBk.Worksheets(1).Range("A1:AC51")
       
        ' Set the destination range to start at column B and be the same size as the source range.
        Set DestRange = SummarySheet.Range("B" & NRow)
        Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
           SourceRange.Columns.Count)
           
        ' Copy over the values from the source to the destination.
        DestRange.Value = SourceRange.Value
       
        ' Increase NRow so that we know where to copy data next.
        NRow = NRow + DestRange.Rows.Count
       
        ' Close the source workbook without saving changes.
        WorkBk.Close savechanges:=False
    Next NFile
   
    ' Call AutoFit on the destination sheet so that all data is readable.
    SummarySheet.Columns.AutoFit
End Sub

mmitola
Utente Junior
 
Post: 25
Iscritto il: 01/02/12 21:45

Sponsor
 

Re: [Excel]-Macro copia fogli

Postdi Anthony47 » 26/03/12 16:59

' Copy over the values from the source to the destination.
DestRange.Value = SourceRange.Value

Come hai scritto nel commento, questa riga copia i "valori" nell' area di destinazione.
Se vuoi mantenere anche la formattazione farai una SourceRange.Copy seguita poi da un PasteSpecial /Valori e successivamente PasteSpecial /Formato nella DestRange.

Ciao
Avatar utente
Anthony47
Moderatore
 
Post: 19196
Iscritto il: 21/03/06 16:03
Località: Ivrea

Re: [Excel]-Macro copia fogli

Postdi mmitola » 27/03/12 11:18

Ciao Anthony.

Innanzitutto ti ringrazio per la risposta....

Ma non mi è molto chiaro il tuo ragionamento....gentilmente potresti scrivermi la stringa di codice da inserire?

E poi la stringa di codice che hai richiamato devo lasciarla oppure va eliminata?

Ti ringrazio anticipatamente per la pronta risposta....

Buona giornata.
mmitola
Utente Junior
 
Post: 25
Iscritto il: 01/02/12 21:45

Re: [Excel]-Macro copia fogli

Postdi Anthony47 » 27/03/12 23:23

Perdona, visto il codice che hai scritto pensavo fosse sufficiente lo spunto...
Prova a sostituire la riga DestRange.Value = SourceRange.Value con
Codice: Seleziona tutto
SourceRange.Copy
DestRange. PasteSpecial Paste:=xlPasteValues
DestRange.PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False

Ciao
Avatar utente
Anthony47
Moderatore
 
Post: 19196
Iscritto il: 21/03/06 16:03
Località: Ivrea


Torna a Applicazioni Office Windows


Topic correlati a "[Excel]-Macro copia fogli":


Chi c’è in linea

Visitano il forum: Nessuno e 34 ospiti