Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1676to1680
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Makro funktioniert nur in persönlicher Mappe

Makro funktioniert nur in persönlicher Mappe
05.03.2019 13:17:33
Alina
Hallo,
aktuell schreibe ich einen Code mit dem man mehrere Dateien eines Experiments zuerst formatieren und danach auswerten kann.
Leider habe ich nun das Problem, dass dieser Code in meiner persönlichen Mappe super funktioniert, mir in der Projektmappe aber immer ein Fehler angezeigt wird (Laufzeitfehler 1004, Anwendungs- & Objektdefinierter Fehler). In der persönlichen Mappe kann ich den Code aber nicht lassen, weil ich ein Dokument mit Buttons erstellen soll, damit jeder mit Hilfe der erstellten "Buttons-Datei" das Experiment auswerten kann.
Da ich mich leider überhaupt nicht mit VBA auskenne und den Code auch mehr oder weniger zusammengebastelt habe, weiß ich nicht wo ich etwas ändern muss und wäre für jede Hilfe dankbar...
Hier mein Code:
--> der Fehler erscheint immer bei der Zeile Columns("A:A").Select

Sub Auswertung()
Dim xFd As FileDialog
Dim xSPath As String
Dim xCSVFile As String
Dim xWsheet As String
Application.DisplayAlerts = False
Application.StatusBar = True
xWsheet = ActiveWorkbook.name
Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
xFd.Title = "Select a folder:"
If xFd.Show = -1 Then
xSPath = xFd.SelectedItems(1)
Else
Exit Sub
End If
If Right(xSPath, 1)  "\" Then xSPath = xSPath + "\"
xCSVFile = Dir(xSPath & "*.xlsx")
Do While xCSVFile  ""
Application.StatusBar = "Converting: " & xCSVFile
Workbooks.Open Filename:=xSPath & xCSVFile
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlNone, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1)), _
TrailingMinusNumbers:=True
Rows("1:4").Select
Selection.Delete Shift:=xlUp
For I = 1 To 100
If Cells(I, "B").Value = "ne" And Cells(I, "C").Value = 11 Then
Cells(I, "E").Value = 1
ElseIf Cells(I, "B").Value = "a" And Cells(I, "C").Value = 11 Then
Cells(I, "E").Value = 0
ElseIf Cells(I, "B").Value = "n" And Cells(I, "C").Value = 11 Then
Cells(I, "E").Value = 0
ElseIf Cells(I, "B").Value = "s" And Cells(I, "C").Value = 11 Then
Cells(I, "E").Value = 0
ElseIf Cells(I, "B").Value = "h" And Cells(I, "C").Value = 11 Then
Cells(I, "E").Value = 0
ElseIf Cells(I, "B").Value = "n" And Cells(I, "C").Value = 10 Then
Cells(I, "E").Value = 0
ElseIf Cells(I, "B").Value = "a" And Cells(I, "C").Value = 10 Then
Cells(I, "E").Value = 1
ElseIf Cells(I, "B").Value = "n" And Cells(I, "C").Value = 10 Then
Cells(I, "E").Value = 1
ElseIf Cells(I, "B").Value = "s" And Cells(I, "C").Value = 10 Then
Cells(I, "E").Value = 1
ElseIf Cells(I, "B").Value = "h" And Cells(I, "C").Value = 10 Then
Cells(I, "E").Value = 1
End If
Next I
ActiveWorkbook.Save
ActiveWorkbook.Close
Windows(xWsheet).Activate
xCSVFile = Dir
Loop
Application.StatusBar = False
Application.DisplayAlerts = True
End Sub
Wäre super, wenn mir jemand weiterhelfen könnte.
Liebe Grüße
Alina

6
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Makro funktioniert nur in persönlicher Mappe
05.03.2019 13:35:29
Nepumuk
Hallo Alina,
welche Zeile markiert der Debugger?
Gruß
Nepumuk
AW: Makro funktioniert nur in persönlicher Mappe
05.03.2019 13:48:02
Alina
Hallo Nepumuk,
Es wird immer die Zeile "Columns("A:A").Select" markiert.
AW: Makro funktioniert nur in persönlicher Mappe
05.03.2019 14:26:21
Nepumuk
Hallo Alina,
teste mal:
Option Explicit

Public Sub Auswertung()
    
    Dim xFd As FileDialog
    Dim xSPath As String
    Dim xCSVFile As String
    Dim objWorkbook As Workbook
    Dim I As Long
    
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    xFd.Title = "Select a folder:"
    If xFd.Show = -1 Then
        xSPath = xFd.SelectedItems(1)
    Else
        Exit Sub
    End If
    Set xFd = Nothing
    
    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
        .StatusBar = True
    End With
    
    xSPath = xSPath & "\"
    xCSVFile = Dir$(xSPath & "*.xlsx")
    Do Until xCSVFile = ""
        
        Application.StatusBar = "Converting: " & xCSVFile
        
        Set objWorkbook = Workbooks.Open(Filename:=xSPath & xCSVFile, Local:=True)
        
        With objWorkbook.Worksheets(1)
            
            .Rows("1:4").Delete Shift:=xlShiftUp
            
            .Columns(1).TextToColumns Destination:=.Range("A1"), DataType:=xlDelimited, _
                TextQualifier:=xlNone, ConsecutiveDelimiter:=False, Tab:=False, _
                Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
                :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1)), _
                TrailingMinusNumbers:=True
            
            For I = 1 To 100
                
                If .Cells(I, "B").Value = "ne" And .Cells(I, "C").Value = 11 Then
                    .Cells(I, "E").Value = 1
                ElseIf .Cells(I, "B").Value = "a" And .Cells(I, "C").Value = 11 Then
                    .Cells(I, "E").Value = 0
                ElseIf .Cells(I, "B").Value = "n" And .Cells(I, "C").Value = 11 Then
                    .Cells(I, "E").Value = 0
                ElseIf .Cells(I, "B").Value = "s" And .Cells(I, "C").Value = 11 Then
                    .Cells(I, "E").Value = 0
                ElseIf .Cells(I, "B").Value = "h" And .Cells(I, "C").Value = 11 Then
                    .Cells(I, "E").Value = 0
                ElseIf .Cells(I, "B").Value = "n" And .Cells(I, "C").Value = 10 Then
                    .Cells(I, "E").Value = 0
                ElseIf .Cells(I, "B").Value = "a" And .Cells(I, "C").Value = 10 Then
                    .Cells(I, "E").Value = 1
                ElseIf .Cells(I, "B").Value = "n" And .Cells(I, "C").Value = 10 Then
                    .Cells(I, "E").Value = 1
                ElseIf .Cells(I, "B").Value = "s" And .Cells(I, "C").Value = 10 Then
                    .Cells(I, "E").Value = 1
                ElseIf .Cells(I, "B").Value = "h" And .Cells(I, "C").Value = 10 Then
                    .Cells(I, "E").Value = 1
                End If
                
            Next I
            
        End With
        
        objWorkbook.Close SaveChanges:=True
        
        xCSVFile = Dir$
        
    Loop
    
    Set objWorkbook = Nothing
    
    With Application
        .DisplayAlerts = True
        .ScreenUpdating = True
        .StatusBar = False
    End With
End Sub

Gruß
Nepumuk
Anzeige
AW: Makro funktioniert nur in persönlicher Mappe
05.03.2019 14:31:26
Nepumuk
Oooooooooooooops,
da ist noch ein Fehler drin. Diese Zeile:
Set objWorkbook = Workbooks.Open(Filename:=xSPath & xCSVFile, Local:=True)

muss so lauten:
Set objWorkbook = Workbooks.Open(Filename:=xSPath & xCSVFile)

Du hast mich verwirrt indem du die Dateien als xCSVFile nennst aber .xlsx - Dateien meinst.
Gruß
Nepumuk
AW: Makro funktioniert nur in persönlicher Mappe
05.03.2019 17:29:05
Alina
Oh wow super so funktionierts! Vielen vielen Dank!
AW: Makro funktioniert nur in persönlicher Mappe
05.03.2019 13:47:45
Daniel
Hi
wenn du den Code an einen ActiveX-Button in einen Tabellenblattmodul überträgst, musst du beachten dass dann alle Zellbezüge ohne Sheetangabe davor sich auf das Tabellenblatt des Moduls beziehen und nicht mehr auf das aktive Tabellenblatt.
dh bei allen Zellbezügen, die nach Workbook.Open folgen müsstest ein ActiveSheet voranstellen.
oder du schreibst das Makro in ein allgemeines Modul (da gilt wieder: Zellbezug ohne Sheetangabe bezieht sich auf das aktive Tabellenblatt)
und verwendest einen Formularsteuererlement-Button, dem du über sein Kontextmenü das Marko aus dem allgemeinen Modul zuweist.
Gruß Daniel
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige