Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1444to1448
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

VBA - Codeänderung

VBA - Codeänderung
30.08.2015 15:47:37
WalterK
Hallo,
mit dem folgenden Code von Sepp werden nach allen Kriterien einer ausgesuchten Spalte Blätter angelegt. Funktioniert tadellos.
Könnte man den Code so ändern, dass bei Spalten mit Datümern das Kriterium JAHR benutzt wird, sodass nach Jahren getrennte Blätter erzeugt werden.
Bis jetzt habe ich mir damit beholfen, dass ich ein Hilfsspalte mit =JAHR(C2) usw. angelegt habe. Manchen Anwendern ist das jedoch schon zu kompliziert.
Es können mehrere Spalten mit Datümern vorhanden sein.
Besten Dank für die Hilfe, Servus Walter
https://www.herber.de/bbs/user/99905.xlsm
Option Explicit
Sub KritToSheet()
Dim objShSource As Worksheet, objSh As Worksheet
Dim rng As Range, rngCopy As Range
Dim varTemp As Variant
Dim strFind As String, strFirst As String
Dim lngLast As Long, lngAct As Long
Dim rngCol As Range, intCol As Integer
On Error Resume Next
Set rngCol = Application.InputBox("Markieren Sie eine Zelle in der" & vbLf & _
"gewünschten Spalte! (Kriterium)", "Tabelle aufteilen", ActiveCell.Address, Type:=8)
If rngCol Is Nothing Then Exit Sub
intCol = rngCol(1).Column
On Error GoTo ErrExit
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
.Calculation = xlCalculationManual
.Cursor = xlWait
End With
rngCol.Parent.Copy After:=Sheets(Sheets.Count)
Set objShSource = Sheets(Sheets.Count)
With objShSource
lngLast = .Cells(Rows.Count, intCol).End(xlUp).Row
lngAct = lngLast
Do While lngAct > 1
strFind = .Cells(2, intCol)
Set rngCol = .Range(.Cells(2, intCol), .Cells(lngAct, intCol))
Set rng = rngCol.Find(what:=strFind, lookat:=xlWhole)
If Not rng Is Nothing Then
strFirst = rng.Address
Do
If rngCopy Is Nothing Then
Set rngCopy = .Rows(rng.Row)
Else
Set rngCopy = Union(rngCopy, .Rows(rng.Row))
End If
Set rng = rngCol.FindNext(rng)
Loop While Not rng Is Nothing And strFirst  rng.Address
End If
If Not rngCopy Is Nothing Then
Set objSh = Worksheets.Add(After:=Sheets(Sheets.Count))
On Error Resume Next
objSh.Name = strFind
If Err.Number  0 Then
objSh.Name = strFind & Format(Now, " hhmmss")
Err.Clear
End If
On Error GoTo ErrExit
rngCopy.Copy
objSh.Cells(2, 1).PasteSpecial xlValues
objSh.Cells(2, 1).PasteSpecial xlFormats
Application.CutCopyMode = False
objShSource.Rows(1).Copy objSh.Rows(1)
rngCopy.Delete
Set rngCopy = Nothing
Set objSh = Nothing
End If
lngAct = .Cells(Rows.Count, intCol).End(xlUp).Row
Loop
.Delete
End With
ErrExit:
Set objShSource = Nothing
Set rngCol = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
.Calculation = xlCalculationAutomatic
.Cursor = xlDefault
End With
End Sub

2
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: VBA - Codeänderung
30.08.2015 15:56:55
Sepp
Hallo Walter,
' **********************************************************************
' Modul: Tabelle1 Typ: Element der Mappe(Sheet, Workbook, ...)
' **********************************************************************

Option Explicit

Sub KritToSheet()
Dim objShSource As Worksheet, objSh As Worksheet
Dim rng As Range, rngCopy As Range
Dim varTemp As Variant
Dim strFind As String, strFirst As String
Dim lngLast As Long, lngAct As Long
Dim rngCol As Range, intCol As Integer
Dim vntLookAt As XlLookAt

On Error Resume Next
Set rngCol = Application.InputBox("Markieren Sie eine Zelle in der" & vbLf & _
  "gewünschten Spalte! (Kriterium)", "Tabelle aufteilen", ActiveCell.Address, Type:=8)

If rngCol Is Nothing Then Exit Sub

intCol = rngCol(1).Column

On Error GoTo ErrExit

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

rngCol.Parent.Copy After:=Sheets(Sheets.Count)

Set objShSource = Sheets(Sheets.Count)

With objShSource
  
  lngLast = .Cells(Rows.Count, intCol).End(xlUp).Row
  lngAct = lngLast
  
  Do While lngAct > 1
    
    If IsDate(.Cells(2, intCol)) Then
      vntLookAt = xlPart
      strFind = Year(.Cells(2, intCol))
    Else
      vntLookAt = xlWhole
      strFind = .Cells(2, intCol)
    End If
    
    Set rngCol = .Range(.Cells(2, intCol), .Cells(lngAct, intCol))
    
    Set rng = rngCol.Find(What:=strFind, LookAt:=vntLookAt)
    
    If Not rng Is Nothing Then
      strFirst = rng.Address
      Do
        If rngCopy Is Nothing Then
          Set rngCopy = .Rows(rng.Row)
        Else
          Set rngCopy = Union(rngCopy, .Rows(rng.Row))
        End If
        
        Set rng = rngCol.FindNext(rng)
        
      Loop While Not rng Is Nothing And strFirst <> rng.Address
      
    End If
    
    If Not rngCopy Is Nothing Then
      Set objSh = Worksheets.Add(After:=Sheets(Sheets.Count))
      On Error Resume Next
      objSh.Name = strFind
      If Err.Number <> 0 Then
        objSh.Name = strFind & Format(Now, " hhmmss")
        Err.Clear
      End If
      On Error GoTo ErrExit
      rngCopy.Copy
      objSh.Cells(2, 1).PasteSpecial xlValues
      objSh.Cells(2, 1).PasteSpecial xlFormats
      Application.CutCopyMode = False
      objShSource.Rows(1).Copy objSh.Rows(1)
      rngCopy.Delete
      Set rngCopy = Nothing
      Set objSh = Nothing
    End If
    
    lngAct = .Cells(Rows.Count, intCol).End(xlUp).Row
    
  Loop
  
  .Delete
  
End With

ErrExit:

Set objShSource = Nothing
Set rngCol = Nothing

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

End Sub


Gruß Sepp

Anzeige
AW: VBA - Codeänderung
30.08.2015 16:09:36
WalterK
Hallo Sepp,
Perfekt! Besten Dank und schönen Sonntag noch. Servus, Walter

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige