Clever-Excel-Forum

Normale Version: Makro Objektvariable nicht gesetzt
Du siehst gerade eine vereinfachte Darstellung unserer Inhalte. Normale Ansicht mit richtiger Formatierung.
Hallo Kollegen,

ich habe in Problem mit meinem Makro.

Es hat immer funktioniert, jedoch ab heute nicht mehr.
Die Fehlermeldung sagt, dass die Objektvariable nicht definiert worden ist. Ich habe schon einige Foren durchsucht, aber einfach keine passende Lösung gefunden.

bitte um Hilfe diesbezüglich.

hier mal ein Ausschnitt aus dem Code:
       
 

Code:
 Range("A2").Select
   
   
   
    Set rngSucheNach = Worksheets("SAVE").Cells(2, 4) 'D2'
    Cells.Find(What:=rngSucheNach, After:=ActiveCell, LookIn:=xlFormulas, _
           LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
           MatchCase:=False, SearchFormat:=False).Activate
   ActiveCell.Offset(-1, 0).Rows("1:1").EntireRow.Select
   Range(Selection, Selection.End(xlUp)).Select
   Application.CutCopyMode = False
   Selection.Delete Shift:=xlUp
   Range("E4").Select
   Sheets("SAVE").Select
   Rows("1:1").Select
   Selection.Copy
   Sheets("Eröffnete POs Vortag").Select
   Rows("1:1").Select
   Selection.Insert Shift:=xlDown
   Range("K10").Select

Vielen DAnk im Voraus.

LG

Lukas
Hallo Lukas,

Du verwendest jetzt Option Explicit und daher muss jede Variable deklariert werden. Zeile eingefügt ohne das unnötige Select-Gedöns zu entfernen.

PHP-Code:
Sub x()

Dim rngSucheNach As Range

 Range
("A2").Select
    
    
    
     Set rngSucheNach 
Worksheets("SAVE").Cells(24'D2'
     
Cells.Find(What:=rngSucheNachAfter:=ActiveCellLookIn:=xlFormulas_
            LookAt
:=xlPartSearchOrder:=xlByRowsSearchDirection:=xlNext_
            MatchCase
:=FalseSearchFormat:=False).Activate
    ActiveCell
.Offset(-10).Rows("1:1").EntireRow.Select
    Range
(SelectionSelection.End(xlUp)).Select
    Application
.CutCopyMode False
    Selection
.Delete Shift:=xlUp
    Range
("E4").Select
    Sheets
("SAVE").Select
    Rows
("1:1").Select
    Selection
.Copy
    Sheets
("Eröffnete POs Vortag").Select
    Rows
("1:1").Select
    Selection
.Insert Shift:=xlDown
    Range
("K10").Select

End Sub 
Hallo, 

danke für die rasche Antwort, habe das nun hinzugefügt, bekomme laut dem Debugger ein Problem bei

Code:
Set rngSucheNach = Worksheets("SAVE").Cells(2, 4) 'D2'
    Cells.Find(What:=rngSucheNach, After:=ActiveCell, LookIn:=xlFormulas, _
           LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
           MatchCase:=False, SearchFormat:=False).Activate

Ich weiß nicht, woran das liegen könnte!?

Bitte nochmals um Unterstützung.

Liebe Grüße

Lukas
Hallo Lukas,

habe es mal geändert. Sollte es nicht funktionieren, solltest Du die Datei hier hochladen.

Code:
Sub x()

Dim strSucheNach As String
Dim rngSucheNach As Range

Range("A2").Select
    
    
    
     strSucheNach = Worksheets("SAVE").Cells(2, 4) 'D2'
     Set rngSucheNach = Cells.Find(What:=strSucheNach, After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)
    If Not rngSucheNach Is Nothing Then
    rngSucheNach.Select 'eingefügt, weil ich nicht weiß, was du mit der nächsten Codezeile beabsichtigist
    ActiveCell.Offset(-1, 0).Rows("1:1").EntireRow.Select
    Range(Selection, Selection.End(xlUp)).Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlUp
    Range("E4").Select
    Sheets("SAVE").Rows("1:1").Copy
    Sheets("Eröffnete POs Vortag").Rows("1:1").Insert Shift:=xlDown
    End If
End Sub
Hallo,

Das ist nicht ganz sauber so.
Der Code, wenn esr funktionieren würde, würde , sobald kein Fund da ist immer einen Fehler verursachen.

Ich habe noch eine Variable hinzugefügt und etwas erweitert. So wie Stefan auch, habe ich aber nicht auf die Selects geschaut.

Code:
Sub x()

   Dim rngSucheNach As Range
   Dim strgSuch As String
   
   strgSuch = Worksheets("SAVE").Cells(2, 4)
   Range("A2").Select
   
   Set rngSucheNach = Cells.Find(What:=strgSuch, After:=ActiveCell, LookIn:=xlFormulas, _
          LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
          MatchCase:=False, SearchFormat:=False)
           
   If Not rngSucheNach Is Nothing Then
     rngSucheNach.Activate
     ActiveCell.Offset(-1, 0).Rows("1:1").EntireRow.Select
     Range(Selection, Selection.End(xlUp)).Select
     Application.CutCopyMode = False
     Selection.Delete Shift:=xlUp
     Range("E4").Select
     Sheets("SAVE").Select
     Rows("1:1").Select
     Selection.Copy
     Sheets("Eröffnete POs Vortag").Select
     Rows("1:1").Select
     Selection.Insert Shift:=xlDown
     Range("K10").Select
   Else
     MsgBox "Suche war erfolglos :-( "
   End If
End Sub
Hi Stefan, weißt Du warum Du diesmal schneller warst?
Nur weil ich noch eine MsgBox eingebaut und darin diese schöne Gesicht gezeichnet habe.
Ohne die Zeichnung wäre ich auch schneller gewesen. :19:
Hallo Kollegen,

vielen Dank für Eure Antworten, ich habe es nun mit dem Code

Code:
Dim rngSucheGefunden As Range
'...

Set rngSucheNach = Worksheets("SAVE").Cells(2, 4) 'D2'
   
Set rngSucheGefunden = Cells.Find(What:=rngSucheNach, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
If Not (rngSucheGefunden Is Nothing) Then
   rngSucheGefunden.Activate
Else
   MsgBox "Wert (" & rngSuchNach.Value & ") nicht gefunden!"
End If

Lösen können.

Es kam dann noch ein Problem auf, aber ich habe einfach das Ende der If-Schlife weiter unten gesetzt und siehe da es funktioniert auf einmal einwandfrei!

Danke nochmal!