Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
732to736
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
732to736
732to736
Aktuelles Verzeichnis
Verzeichnis Index
Verzeichnis Index
Übersicht Verzeichnisse
Inhaltsverzeichnis

Problem mit Close Befehl

Problem mit Close Befehl
Reinhard
Hallo Wissende,
Option Explicit
Sub tt()
Dim fs, n As Integer, zeiQ As Long, zeiZ As Long, wsZ As Worksheet
Set fs = Application.FileSearch
Set wsZ = ActiveSheet
With fs
.LookIn = "C:\My Documents"
.Filename = "*.xls"
.Execute
For n = 1 To .FoundFiles.Count
zeiZ = ActiveSheet.Range("A65536").End(xlUp).Row + 1
If n = 1 Then zeiZ = 1
Application.EnableEvents = False
Workbooks.Open .FoundFiles(n)
Application.EnableEvents = True
zeiQ = ActiveSheet.Range("A65536").End(xlUp).Row
ActiveSheet.Range("A1:B" & zeiQ).Copy
wsZ.Activate
wsZ.Cells(zeiZ, 1).Select
wsZ.Paste Link:=True
Workbooks(.FoundFiles(n)).Close savechanges:=False
Next n
End With
End Sub

Wenn ich das Makro über einen Button in der Tabelle starte kommt Fehler 9, Index außerhalb ... in der Zeile:
Workbooks(.FoundFiles(n)).Close savechanges:=False
Ich weiß nicht warum, bei Open findet er doch .FoundFiles(n)
Wenn ich das Makro im Editor starte kommt mit auskommentiertem wsZ.Activate
bei wsZ.Cells(zeiZ, 1).Select der Fehler 1004,
Sporadisch kommt mit wsZ.Activate in der Zeile wsZ.Activate ein Automatisierungsfehler mit der langen Nummer.
Wodran kann das liegen, was muss ich ändern?
Geht Paste auch ohne Activate und Select?
Danke ^ Gruß
Reinhard

5
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Benutzer
Anzeige
AW: Problem mit Close Befehl
14.02.2006 15:29:43
Josef
Hallo Reinhard!
Ungetestet!
' **********************************************************************
' Modul: Modul1 Typ: Allgemeines Modul
' **********************************************************************

Option Explicit
Sub tt()
Dim fs, n As Integer, zeiQ As Long, zeiZ As Long
Dim wsZ As Worksheet, wsQ As Worksheet, wbQ As Workbook
Dim lngCalculation As Long

Set fs = Application.FileSearch
Set wsZ = ActiveSheet

On Error GoTo ErrExit

With Application
  .ScreenUpdating = False
  .EnableEvents = False
  .DisplayAlerts = False
  lngCalculation = .Calculation
  .Calculation = xlCalculationManual
  .Cursor = xlWait
End With

With fs
  .LookIn = "C:\My Documents"
  .Filename = "*.xls"
  .Execute
  For n = 1 To .FoundFiles.Count
    zeiZ = wsZ.Range("A65536").End(xlUp).Row + 1
    If n = 1 Then zeiZ = 1
    Set wbQ = Workbooks.Open(.FoundFiles(n))
    Set wsQ = wbQ.ActiveSheet
    zeiQ = wsQ.Range("A65536").End(xlUp).Row
    wsQ.Range("A1:B" & zeiQ).Copy wsZ.Cells(zeiZ, 1)
    wbQ.Close False
    Set wbQ = Nothing
    Set wsQ = Nothing
  Next
End With

ErrExit:

Set wsZ = Nothing
Set fs = Nothing

If Err.Number > 0 Then
  MsgBox Err.Number & vbLf & Err.Description, , "Fehler"
  Err.Clear
End If

On Error GoTo 0

With Application
  .ScreenUpdating = True
  .EnableEvents = True
  .DisplayAlerts = True
  .Calculation = lngCalculation
  .Cursor = xlDefault
End With

End Sub


'******************************
'* Gruß Sepp
'*
'* Rückmeldung wäre nett!
'******************************

Anzeige
AW: Problem mit Close Befehl
14.02.2006 15:40:28
Reinhard
Hallo Josef,
die Zellen sollen nicht reinkopiert werden sondern als Verknüpfung auf ihren Ursprungsort, deshalb brauche ich das Paste mit Link=true und damit fing der Ärger an:-)
Gruß
Reinhard
AW: Problem mit Close Befehl
14.02.2006 16:00:14
Josef
Hallo Reinhard!
Dann greifen wir auf Trick 17 zurück;-))
' **********************************************************************
' Modul: Modul1 Typ: Allgemeines Modul
' **********************************************************************

Option Explicit
Sub tt()
Dim fs, n As Integer, zeiQ As Long, zeiZ As Long
Dim wsZ As Worksheet, wsQ As Worksheet, wbQ As Workbook
Dim lngCalculation As Long

Set fs = Application.FileSearch
Set wsZ = ActiveSheet

On Error GoTo ErrExit

With Application
  .ScreenUpdating = False
  .EnableEvents = False
  .DisplayAlerts = False
  lngCalculation = .Calculation
  .Calculation = xlCalculationManual
  .Cursor = xlWait
End With

With fs
  .LookIn = "C:\My Documents"
  .Filename = "*.xls"
  .Execute
  For n = 1 To .FoundFiles.Count
    zeiZ = wsZ.Range("A65536").End(xlUp).Row + 1
    If n = 1 Then zeiZ = 1
    Set wbQ = Workbooks.Open(.FoundFiles(n))
    Set wsQ = wbQ.ActiveSheet
    zeiQ = wsQ.Range("A65536").End(xlUp).Row
    wsQ.Range("A1:B" & zeiQ).Copy
    wsZ.Cells(zeiZ, 1).PasteSpecial xlValues
    wsZ.Paste Link:=True
    Application.CutCopyMode = False
    wbQ.Close False
    Set wbQ = Nothing
    Set wsQ = Nothing
  Next
End With

ErrExit:

Set wsZ = Nothing
Set fs = Nothing

If Err.Number > 0 Then
  MsgBox Err.Number & vbLf & Err.Description, , "Fehler"
  Err.Clear
End If

On Error GoTo 0

With Application
  .ScreenUpdating = True
  .EnableEvents = True
  .DisplayAlerts = True
  .Calculation = lngCalculation
  .Cursor = xlDefault
End With

End Sub


'******************************
'* Gruß Sepp
'*
'* Rückmeldung wäre nett!
'******************************

Anzeige
AW: Problem mit Close Befehl
14.02.2006 16:24:59
Reinhard
Hallo Josef,
deine Trickkiste hätte ich sehr gerne :-)
Also erst Pastespecial mit Werten, dann Paste mit Verknüpfung.
Muss man erstmal drauf kommen, wieder was gelernt heute.
Danke
Reinhard
AW: Problem mit Close Befehl
14.02.2006 15:57:02
Reinhard
Hallo Josef,
ich habe wie achstehend gezeigt meinem Code deinem angenähert und schon läüft er.
Allerdings war mir völlig neu dass vba wohl "My Documents" (diesen Ordner habe ich gar nicht) automatisch in "Eigene Dateien" ,samt Pfad dazu, übersetzt, also wie in A1 sichtbar:
='C:\Dokumente und Einstellungen\User\Eigene Dateien\[2for1Frankfurt.xls]Tabelle1'!A1
Aha, grade getestet, existiert ein Ordner "C:\My Documents" wird in dem gesucht.
Wieder was gelernt.
Naja, bliebe noch das Problem mit Activate und Select bei Paste.
Gruß
Reinhard
Sub ttt()
Dim fs, n As Integer, zeiQ As Long, zeiZ As Long, wsZ As Worksheet, wbQ As Workbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False 'wegen Abfage Zwischenablage leeren
Set fs = Application.FileSearch
Set wsZ = ActiveSheet
With fs
.LookIn = "C:\My Documents"
.Filename = "*.xls"
.Execute
For n = 1 To .FoundFiles.Count
zeiZ = ActiveSheet.Range("A65536").End(xlUp).Row + 1
If n = 1 Then zeiZ = 1
Application.EnableEvents = False
Set wbQ = Workbooks.Open(.FoundFiles(n))
Application.EnableEvents = True
zeiQ = ActiveSheet.Range("A65536").End(xlUp).Row
ActiveSheet.Range("A1:B" & zeiQ).Copy
wsZ.Activate
wsZ.Cells(zeiZ, 1).Select
wsZ.Paste Link:=True
wbQ.Close savechanges:=True
'Workbooks(.FoundFiles(n)).Close savechanges:=False
Next n
End With
Range("A1").Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige