Anzeige
Archiv - Navigation
1952to1956
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

Verschieben von Daten zwischen Arbeitsblättern

Verschieben von Daten zwischen Arbeitsblättern
26.11.2023 20:42:27
boléro
Ich möchte Zeilen in einem Excel-Arbeitsblatt in ein anderes anhand von einem Kriterium (Kostenart, in einem Buchhaltungs-Excel) automatisch verschieben. Ich konnte ein Macro schreiben, um den Prozess zu automatisieren, jedoch überschreibt es mir immer die erste Zeile unter der gewünschten Zielzeile, deshalb meine Frage: Wie kann ich den Code anpassen, dass es die Zeile nicht überschreibt sondern in die nächste leere Zeile eingefügt wird?

Mein Macro:
Function IsValueInArray(valueToCheck As Variant, arr As Variant) As Boolean

' Function to check if a value is in an array
Dim element As Variant
For Each element In arr
If CStr(element) = CStr(valueToCheck) Then
IsValueInArray = True
Exit Function
End If
Next element
IsValueInArray = False
End Function

Sub CopyToRange()
On Error GoTo ErrorHandler ' Enable error handling

Dim sourceSheet As Worksheet
Dim destinationSheet As Worksheet
Dim valueToCheck As Variant
Dim targetRow As Range
Dim foundCell As Range

' Set the source sheet
On Error Resume Next
Set sourceSheet = Worksheets("Raiffeisen Konto")
On Error GoTo 0 ' Reset error handling
If sourceSheet Is Nothing Then
MsgBox "The source sheet 'Raiffeisen Konto' was not found.", vbExclamation
Exit Sub
End If

' Set the destination sheet
On Error Resume Next
Set destinationSheet = Worksheets("4000-7100")
On Error GoTo 0 ' Reset error handling
If destinationSheet Is Nothing Then
MsgBox "The destination sheet '4000-7100' was not found.", vbExclamation
Exit Sub
End If

' List of values to check
Dim checkValues As Variant
checkValues = Array(4000, 4010, 4550, 4700, 4710, 4740, 4750, 6000, 6400, 6700, 6900, 7100, 7200)

' Loop through each row in the source sheet
For Each sourceRow In sourceSheet.UsedRange.Rows
' Get the value to check from the source sheet (assumed to be in column D)
valueToCheck = sourceSheet.Cells(sourceRow.Row, 4).Value

' Check if the value is in the specified list of values
If IsValueInArray(valueToCheck, checkValues) Then
' Find all occurrences of the value in the A column of the destination sheet
Set foundCell = destinationSheet.Columns(1).Find(What:=valueToCheck, LookIn:=xlValues, LookAt:=xlWhole)

' Loop through each found cell and append the row from the source sheet
Do While Not foundCell Is Nothing
' Set the target row one row below the found cell
Set targetRow = foundCell.Offset(1, 0).EntireRow

' Copy the entire row from the source sheet to the destination sheet
sourceRow.Copy targetRow

' Find the next occurrence of the value in the A column of the destination sheet
Set foundCell = destinationSheet.Columns(1).FindNext(foundCell)
Loop
End If
Next sourceRow

Exit Sub ' Exit the sub if there are no errors

ErrorHandler:
MsgBox "An error occurred: " & Err.Description
End Sub


Vielen Dank, falls mir Jemand dabei weiterhelfen kann!

8
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Verschieben von Daten zwischen Arbeitsblättern
26.11.2023 21:20:47
GerdL
Hola

'...............................

Set foundCell = destinationSheet.Columns(1).Find(What:=valueToCheck, LookIn:=xlValues, LookAt:=xlWhole)
If Not foundCell is Nothing Then
firstAddress = foundCell.Address
' Loop through each found cell and append the row from the source sheet

Do
' Set the target row one row below the found cell
Set targetRow = foundCell.Offset(1, 0).EntireRow

' Copy the entire row from the source sheet to the destination sheet
sourceRow.Copy targetRow

' Find the next occurrence of the value in the A column of the destination sheet
Set foundCell = destinationSheet.Columns(1).FindNext(foundCell)
Loop Until foundCell.Address = firstAdress
End If
Next sourceRow
'................................................

Gruß Gerd
Anzeige
AW: Verschieben von Daten zwischen Arbeitsblättern
29.11.2023 15:39:10
Piet
Nachtrag

ich hatte für meinen Bruder einmal ein amerikanisches Jornal erstellt. Da waren alle Konten nebeneinander, statt untereinander!
Er brauchte diese Schreibweise für die monatliche Umsatzsteuer Anmeldung. Zum Spalten Programmieren lkein Problem.

mfg Piet
AW: Verschieben von Daten zwischen Arbeitsblättern
27.11.2023 07:38:50
boléro
Lieber Gerd
Vielen Dank für deine Antwort. Leider zeigt es mir nun folgendes an: "Fehler beim Kompilieren: Next ohne For" (obwohl ich die Struktur nochmals überprüft habe)

Function IsValueInArray(valueToCheck As Variant, arr As Variant) As Boolean

' Function to check if a value is in an array
Dim element As Variant
For Each element In arr
If CStr(element) = CStr(valueToCheck) Then
IsValueInArray = True
Exit Function
End If
Next element
IsValueInArray = False
End Function

Sub CopyToRange()
On Error GoTo ErrorHandler ' Enable error handling

Dim sourceSheet As Worksheet
Dim destinationSheet As Worksheet
Dim valueToCheck As Variant
Dim targetRow As Range
Dim foundCell As Range

' Set the source sheet
On Error Resume Next
Set sourceSheet = Worksheets("Raiffeisen Konto")
On Error GoTo 0 ' Reset error handling
If sourceSheet Is Nothing Then
MsgBox "The source sheet 'Raiffeisen Konto' was not found.", vbExclamation
Exit Sub
End If

' Set the destination sheet
On Error Resume Next
Set destinationSheet = Worksheets("4000-7100")
On Error GoTo 0 ' Reset error handling
If destinationSheet Is Nothing Then
MsgBox "The destination sheet '4000-7100' was not found.", vbExclamation
Exit Sub
End If

' List of values to check
Dim checkValues As Variant
checkValues = Array(4000, 4010, 4550, 4700, 4710, 4740, 4750, 6000, 6400, 6700, 6900, 7100, 7200)

' Loop through each row in the source sheet
For Each sourceRow In sourceSheet.UsedRange.Rows
' Get the value to check from the source sheet (assumed to be in column D)
valueToCheck = sourceSheet.Cells(sourceRow.Row, 4).Value

' Check if the value is in the specified list of values
If IsValueInArray(valueToCheck, checkValues) Then
' Find all occurrences of the value in the A column of the destination sheet
Set foundCell = destinationSheet.Columns(1).Find(What:=valueToCheck, LookIn:=xlValues, LookAt:=xlWhole)
If Not foundCell Is Nothing Then
firstAdress = foundCell.Address
' Loop through each found cell and append the row from the source sheet
Do
' Set the target row one row below the found cell
Set targetRow = foundCell.Offset(1, 0).EntireRow

' Copy the entire row from the source sheet to the destination sheet
sourceRow.Copy targetRow

' Find the next occurrence of the value in the A column of the destination sheet
Set foundCell = destinationSheet.Columns(1).FindNext(foundCell)
Loop Until foundCell.Address = firstAdress
End If
Next sourceRow

Exit Sub ' Exit the sub if there are no errors

ErrorHandler:
MsgBox "An error occurred: " & Err.Description
End Sub


Muss ich einen weiteren For-Loop starten, dass es kompilieren kann?
Vielen Dank für deine Antwort! LG Manuele
Anzeige
AW: Verschieben von Daten zwischen Arbeitsblättern
27.11.2023 07:46:01
Oberschlumpf
Hi,

fehlt vllt das END IF für
If IsValueInArray(valueToCheck, checkValues) Then

?

Ciao
Thorsten
AW: Verschieben von Daten zwischen Arbeitsblättern
27.11.2023 07:51:36
GerdL
Ok, da stand noch eine weitere If-Bedingung darüber.

Versuch es mit also mit einem doppelten "End If" über "Next"
'...................
Loop Until foundCell.Address = firstAdress
End If
End If
Next sourceRow
'.....................................

Gruß Gerd
AW: Verschieben von Daten zwischen Arbeitsblättern
27.11.2023 08:13:50
boléro
https://www.herber.de/bbs/user/164591.xlsm
Vielen Dank & sorry für das Übersehen. Leider überschreibt es weiterhin die oberste Zeile mit dem neusten Eintrag im Konto Arbeitsblatt.
Ich habe mal eine Datei erstellt, um das Problem besser zu zeigen.
Vielen Dank für eure ganze Hilfe
LG Manuele

Anzeige
AW: Verschieben von Daten zwischen Arbeitsblättern
29.11.2023 15:33:32
Piet
Hallo bolero

alle Achtung, wer hat das denn programmiert?? Der ist um Klassen besser wie ich, das sage ich ganz offen.
Trotzdem steckt ein dummer Fehler im Detail. Und der ist mit einer msgBox leicht zu finden. Teste es bitte selbst!

Setze bitte in die Do Loop Schleife hinter - Set targetRow = foundCell.Offset(i, 0).EntireRow eine MsgBox zum testen
MsgBox foundCell.Address - diese MsgBox zeigt dir immer nur eine Adresse an, sie wechselt nicht nach unten!
Die Adresse ist und bleibt die Konto Nr. in Spalte A, das verändert sich nicht! Deshalb immer Eintrag in die gleiche Zelle!

Da müsst ihr eine For Next Routine einbauen, um die nächste freie Zelle zum eintragen zu ermitteln.
Und aufpassen das ihr nicht ins nächste Konto überschreibt! Dann sollte es klappen.

Probiert es mal mit meinem Code, indem ihr die Do Loop komplett durch For Next ersetzt. Auf die Schnelle, ohne Gewähr!

mfg Piet

  • If Not foundCell Is Nothing Then
    For i = 1 To 30
    If foundCell.Offset(i + 2, 0) > Empty Then _
    MsgBox "Diese Kostenstelle ist voll!": Exit Sub
    If foundCell.Offset(i, 1) > Empty Then Exit For
    Next i
    Set targetRow = foundCell.Offset(i, 0).EntireRow
    sourceRow.Copy targetRow
    End If
  • Anzeige
    AW: Verschieben von Daten zwischen Arbeitsblättern
    29.11.2023 16:32:19
    boléro
    Vielen herzlichen Dank Piet.
    Das hat geklappt :) Und ja, ohne Chat-GPT hätte ich diesen Code nicht hinbekommen..

    Der funktionierende Code:
    Function IsValueInArray(valueToCheck As Variant, arr As Variant) As Boolean
    
    ' Function to check if a value is in an array
    Dim element As Variant
    For Each element In arr
    If CStr(element) = CStr(valueToCheck) Then
    IsValueInArray = True
    Exit Function
    End If
    Next element
    IsValueInArray = False
    End Function

    Sub CopyToRange()
    On Error GoTo ErrorHandler ' Enable error handling

    Dim sourceSheet As Worksheet
    Dim destinationSheet As Worksheet
    Dim valueToCheck As Variant
    Dim targetRow As Range
    Dim foundCell As Range
    Dim i As Integer

    ' Set the source sheet
    On Error Resume Next
    Set sourceSheet = Worksheets("Raiffeisen Konto")
    On Error GoTo 0 ' Reset error handling
    If sourceSheet Is Nothing Then
    MsgBox "The source sheet 'Raiffeisen Konto' was not found.", vbExclamation
    Exit Sub
    End If

    ' Set the destination sheet
    On Error Resume Next
    Set destinationSheet = Worksheets("4000-7100")
    On Error GoTo 0 ' Reset error handling
    If destinationSheet Is Nothing Then
    MsgBox "The destination sheet '4000-7100' was not found.", vbExclamation
    Exit Sub
    End If

    ' List of values to check
    Dim checkValues As Variant
    checkValues = Array(4000, 4010, 4550, 4700, 4710, 4740, 4750, 6000, 6400, 6700, 6900, 7100, 7200)

    ' Loop through each row in the source sheet
    For Each sourceRow In sourceSheet.UsedRange.Rows
    ' Get the value to check from the source sheet (assumed to be in column D)
    valueToCheck = sourceSheet.Cells(sourceRow.Row, 4).Value

    ' Check if the value is in the specified list of values
    If IsValueInArray(valueToCheck, checkValues) Then
    ' Find all occurrences of the value in the A column of the destination sheet
    Set foundCell = destinationSheet.Columns(1).Find(What:=valueToCheck, LookIn:=xlValues, LookAt:=xlWhole)
    If Not foundCell Is Nothing Then
    ' Loop through each found cell and append the row from the source sheet
    For i = 1 To 30
    ' Check if the next row is empty
    If WorksheetFunction.CountA(foundCell.Offset(i, 0).EntireRow) = 0 Then
    ' Set targetRow to the found cell + i rows
    Set targetRow = foundCell.Offset(i, 0).EntireRow
    ' Copy the entire row from the source sheet to the destination sheet
    sourceRow.Copy targetRow
    Exit For
    End If
    Next i
    End If
    End If
    Next sourceRow

    Exit Sub ' Exit the sub if there are no errors

    ErrorHandler:
    MsgBox "An error occurred: " & Err.Description
    End Sub

    Anzeige

    Beliebteste Forumthreads (12 Monate)

    Anzeige

    Beliebteste Forumthreads (12 Monate)

    Anzeige
    Anzeige
    Anzeige