Avatar billede perhol Seniormester
08. februar 2011 - 10:25 Der er 3 kommentarer og
1 løsning

Hjælp til VBA makro

Nedenstående VBA kode stammer fra en fil (MergeWorkbooksCode.xls) fundet på siden Ron's Excel Tips

Der er en makro der åbner en filbrowser så man kan bladre frem til en mappe og vælge en eller flere filer som makroen så kopier et forudbestemt område fra.
Derefter oprettes en ny excel-fil hvor dataområdet fra de valgte filer sættes ind under hinanden.

Jeg ville gerne have at data i stedet bliver sat ind i ark4(DataKopi) startende i celle A5 i den fil makroen køres fra.

Hvordan gør jeg det?

Her er koden:

Option Explicit
Declare Function SetCurrentDirectoryA Lib _
                                      "kernel32" (ByVal lpPathName As String) As Long
                                     

Sub ChDirNet(szPath As String)
    SetCurrentDirectoryA szPath
End Sub


Sub MergeSpecificWorkbooks()
    Dim MyPath As String
    Dim SourceRcount As Long, FNum As Long
    Dim mybook As Workbook, BaseWks As Worksheet
    Dim sourceRange As Range, destrange As Range
    Dim rnum As Long, CalcMode As Long
    Dim SaveDriveDir As String
    Dim FName  As Variant
   
    'Change ScreenUpdating, Calculation and EnableEvents
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With
   
    SaveDriveDir = CurDir
    ChDirNet "G:\"
    FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
   
    If IsArray(FName) Then
        'Add a new workbook with one sheet
        Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
        rnum = 1
        'Loop through all files in the array(myFiles)
        For FNum = LBound(FName) To UBound(FName)
            Set mybook = Nothing
            On Error Resume Next
            Set mybook = Workbooks.Open(FName(FNum))
            On Error GoTo 0
           
            If Not mybook Is Nothing Then
                On Error Resume Next
               
                With mybook.Worksheets(1)
                    Set sourceRange = .Range("A5:F76")
                End With
               
                If Err.Number > 0 Then
                    Err.Clear
                    Set sourceRange = Nothing
                   
                Else
                    'if SourceRange use all columns then skip this file
                    If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
                        Set sourceRange = Nothing
                    End If
                   
                End If
               
                On Error GoTo 0
                If Not sourceRange Is Nothing Then
                    SourceRcount = sourceRange.Rows.Count
                   
                    If rnum + SourceRcount >= BaseWks.Rows.Count Then
                        MsgBox "Sorry there are not enough rows in the sheet"
                        BaseWks.Columns.AutoFit
                        mybook.Close savechanges:=False
                        GoTo ExitTheSub
                       
                    Else
                        'Set the destrange
                        Set destrange = BaseWks.Range("A" & rnum)
                        'we copy the values from the sourceRange to the destrange
                       
                        With sourceRange
                            Set destrange = destrange. _
                                            Resize(.Rows.Count, .Columns.Count)
                        End With
                       
                        destrange.Value = sourceRange.Value
                        rnum = rnum + SourceRcount
                    End If
                   
                End If
               
                mybook.Close savechanges:=False
            End If
           
        Next FNum
        BaseWks.Columns.AutoFit
    End If
   
ExitTheSub:
    'Restore ScreenUpdating, Calculation and EnableEvents
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
    End With
   
    ChDirNet SaveDriveDir
End Sub
Avatar billede bak Forsker
08. februar 2011 - 17:29 #1
Uden at have testet det mener jeg at dette burde virke.
Ændringen er mellem de to linier med stjerner

Option Explicit

Option Explicit
Declare Function SetCurrentDirectoryA Lib _
                                      "kernel32" (ByVal lpPathName As String) As Long
                                     

Sub ChDirNet(szPath As String)
    SetCurrentDirectoryA szPath
End Sub


Sub MergeSpecificWorkbooks()
    Dim MyPath As String
    Dim SourceRcount As Long, FNum As Long
    Dim mybook As Workbook, BaseWks As Worksheet
    Dim sourceRange As Range, destrange As Range
    Dim rnum As Long, CalcMode As Long
    Dim SaveDriveDir As String
    Dim FName  As Variant
   
    'Change ScreenUpdating, Calculation and EnableEvents
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With
   
    SaveDriveDir = CurDir
    ChDirNet "G:\"
    FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
   
    If IsArray(FName) Then
        'Add a new workbook with one sheet
        'Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
        'rnum = 1
        '*********Husk ark indsætte navnet på fanen her **************
        Set BaseWks = ThisWorkbook.Worksheets("DataKopi")
        rnum = 5
        '*************************************************************
       
        'Loop through all files in the array(myFiles)
        For FNum = LBound(FName) To UBound(FName)
            Set mybook = Nothing
            On Error Resume Next
            Set mybook = Workbooks.Open(FName(FNum))
            On Error GoTo 0
           
            If Not mybook Is Nothing Then
                On Error Resume Next
               
                With mybook.Worksheets(1)
                    Set sourceRange = .Range("A5:F76")
                End With
               
                If Err.Number > 0 Then
                    Err.Clear
                    Set sourceRange = Nothing
                   
                Else
                    'if SourceRange use all columns then skip this file
                    If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
                        Set sourceRange = Nothing
                    End If
                   
                End If
               
                On Error GoTo 0
                If Not sourceRange Is Nothing Then
                    SourceRcount = sourceRange.Rows.Count
                   
                    If rnum + SourceRcount >= BaseWks.Rows.Count Then
                        MsgBox "Sorry there are not enough rows in the sheet"
                        BaseWks.Columns.AutoFit
                        mybook.Close savechanges:=False
                        GoTo ExitTheSub
                       
                    Else
                        'Set the destrange
                        Set destrange = BaseWks.Range("A" & rnum)
                        'we copy the values from the sourceRange to the destrange
                       
                        With sourceRange
                            Set destrange = destrange. _
                                            Resize(.Rows.Count, .Columns.Count)
                        End With
                       
                        destrange.Value = sourceRange.Value
                        rnum = rnum + SourceRcount
                    End If
                   
                End If
               
                mybook.Close savechanges:=False
            End If
           
        Next FNum
        BaseWks.Columns.AutoFit
    End If
   
ExitTheSub:
    'Restore ScreenUpdating, Calculation and EnableEvents
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
    End With
   
    ChDirNet SaveDriveDir
End Sub
Avatar billede perhol Seniormester
08. februar 2011 - 19:44 #2
Det virker.
Læg et svar!
Avatar billede bak Forsker
08. februar 2011 - 19:47 #3
sådan
Avatar billede perhol Seniormester
08. februar 2011 - 21:26 #4
200 tak ;b)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Excel kurser for alle niveauer og behov – find det kursus, der passer til dig

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester