+ Reply to Thread
Results 1 to 14 of 14

Thread: Delete Content in a row while opening

  1. #1
    Registered User
    Join Date
    06-21-2011
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    24

    Delete Content in a row while opening

    Hi @ all,

    I have a workbook with several worksheets.
    Now I want that the content in row 'AA' of Worksheet '1' gets deleted everytime when I open up the workbook.
    Please be aware that it should not get deleted everytime I open up the Worksheet '1' when I switch thorugh the worksheet...
    It should only get deleted when I open up the whole workbook for the first time.


    Thank you in advance,
    Julian
    Last edited by Julian Schubert; 07-06-2011 at 08:38 AM.

  2. #2
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,228

    Re: Delete Content in a row while opening

    AA is a column, not a row ... is that what you meant?

    You'd use a Workbook_Open event to do this:

    Private Sub Workbook_Open()
    With Sheets("Sheet1")
        .Range("AA1").EntireColumn.Clear
    End With
    End Sub

    Regards

  3. #3
    Registered User
    Join Date
    06-21-2011
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Delete Content in a row while opening

    Yes, of course I mean the column - Sorry!

    Little extra: Is should start deleting the whole column starting in row '2' (there is a heading in row 1...)

    Thank you very much.

  4. #4
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,228

    Re: Delete Content in a row while opening

    Maybe then (untested):

    Private Sub Workbook_Open()
    With Sheets("Sheet1")
        .Range("AA2:AA" & Rows.Count).Clear
    End With
    End Sub

    Regards

  5. #5
    Registered User
    Join Date
    06-21-2011
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Delete Content in a row while opening

    Hello TMShucks,

    Sorry it does not work -

    maybe it is easier if the code should delete all columns 'AA' (starting with AA2) in all worksheets in the workbook...
    but only if I open up the whole workbook for the first time, not while I am working in the workbook and switching from sheet to sheet...

    Thank you

  6. #6
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,228

    Re: Delete Content in a row while opening

    Works for me and does exactly what you have asked for.

    When the workbook is opened, it will clear all cells in column AA from row 2 down.

    Activating and deactivating the sheet will have no effect.

    Regards

  7. #7
    Registered User
    Join Date
    06-21-2011
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Delete Content in a row while opening

    hmm... okay maybe it does not work because there are other codes and I tried to implement your code in the wrong position...

    this is the complet code and I put yours just on the top.

    FYI: The worksheet is called "ALL", not "Sheet1" anymore


    Private Sub Workbook_Open()
    With Sheets("ALL")
        .Range("AA2:AA" & Rows.Count).Clear
    End With
    End Sub
    
    Private Sub Worksheet_Activate()
    Dim ws As Worksheet
    Dim lngLast As Long
    Const strForbiddenWorksheetNames As String = "ALL#FTW#APP#ACC"
    
    Me.Cells.Clear
    Worksheets("FTW SS 12").Rows(1).Copy _
      Destination:=Me.Rows(1)
    
    For Each ws In ThisWorkbook.Worksheets
      If InStr(1, strForbiddenWorksheetNames, ws.Name, vbTextCompare) = 0 Then
        lngLast = ws.Cells(Rows.Count, 1).End(xlUp).Row
        ws.Range("A2:AA" & lngLast).Copy _
          Destination:=Me.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
      End If
    Next ws
    
    'Aufruf der beiden Subs für das Bereinigen der Liste
    NiederMitDenDoppelten
    '...und das Kopieren der Bereiche mit Eintragungen in Spalte AA
    kopiereGewählte
    
    Me.UsedRange.Sort Key1:=Me.Range("K2"), Order1:=xlAscending, Header:=xlYes, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    End Sub
    
    Private Sub NiederMitDenDoppelten()
    'Schlüsselfelder in der Tabelle:
    '5,6,9,11,13,27
    'entsprechen Feldern im Array fWerte:
    '1,2,5,7,9,23
    
    Dim fWerte As Variant, i As Long
    Dim dic As Object, strKeyTemp As String
    Dim rngWegmachen As Range
    
    Set dic = CreateObject("Scripting.Dictionary")
    fWerte = Me.Range("E1:AA" & Me.Cells(Rows.Count, 1).End(xlUp).Row)
    For i = LBound(fWerte, 1) + 1 To UBound(fWerte, 1)
      strKeyTemp = fWerte(i, 1) & "###" & fWerte(i, 2) & "###" & fWerte(i, 5) & "###" & fWerte(i, 7) & "###" & fWerte(i, 9) & "###" & fWerte(i, 23)
      If dic.exists(strKeyTemp) Then
        If rngWegmachen Is Nothing Then
          Set rngWegmachen = Rows(i)
        Else
          Set rngWegmachen = Union(rngWegmachen, Rows(i))
        End If
      Else
        dic(strKeyTemp) = 0
      End If
    Next i
    If Not rngWegmachen Is Nothing Then rngWegmachen.EntireRow.Delete
    Set dic = Nothing
    End Sub
    
    Sub kopiereGewählte()
    '"X" oder irgend ein anderes fest eingetragenes Kennzeichen in Spalte AA
    Dim wbZiel As Workbook
    
    Set wbZiel = Workbooks.Open("K:\_IPM Team\_Julian\TESTTimeline.xlsm")
    
    wbZiel.Worksheets(1).Cells.ClearContents
    
    With ThisWorkbook.Worksheets("ALL")
       Intersect(.Range("A:Z"), _
             .Columns("AA").SpecialCells(xlCellTypeConstants).EntireRow).Copy _
                Destination:=wbZiel.Worksheets(1).Range("A2")
    End With
    
    wbZiel.Close savechanges:=True
    Set wbZiel = Nothing
    End Sub

  8. #8
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,228

    Re: Delete Content in a row while opening

    It shouldn't make a difference where in the module it is but it *must* be in the workbook class module, not a standard module. Same with the Worksheet_Activate ... that needs to be in the worksheet class module for the sheet being activated, unless you use the Workbook_SheetActivate event which is in the workbook class module.

    The workbook class module is the module that opens when you double click on ThisWorkbook in the VBE.


    Regards

  9. #9
    Registered User
    Join Date
    06-21-2011
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Delete Content in a row while opening

    sorry - I am a rookie

    May I ask you that you adopt your code in mine so that it works?
    I am not able to do it on my own - embarassing

    Thank you in advance.

  10. #10
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,228

    Re: Delete Content in a row while opening

    That doesn't look like Rookie code to me ;-)

    See the attached sample workbook. As I don't speak German, I'm not sure if it works and does what you want ... and I don't have the workbook(s) it tries to access.

    But give this a go.

    Regards
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    06-21-2011
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Delete Content in a row while opening

    Thank you for your work.
    It lookes almost very good =)


    Little exception:

    Option Explicit
    
    Private Sub Workbook_Open()
    With Sheets("ALL")
        .Range("AA2:AA" & Rows.Count).Clear
    End With
    End Sub
    It only deletes the content in shhet "ALL" - and now sometimes I want to change the code that in all worksheets the column AA (starting AA2) gets deleted.

    what do I have to change:

    I tried this code, but it does not work:

    Option Explicit
    
    Private Sub Workbook_Open()
    For Each ws In ThisWorkbook
        .Range("AA2:AA" & Rows.Count).Clear
    End With
    End Sub

    help?
    Thank you


    P.S. "the" code took me forever to make it the way it is... you can not imagine

  12. #12
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,228

    Re: Delete Content in a row while opening

    Private Sub Workbook_Open()
    Dim ws As Worksheet
    For Each ws In Worksheets
        With ws
            .Range("AA2:AA" & Rows.Count).Clear
        End With
    Next 'ws
    End Sub

    You need to loop through the worksheets collection, not the workbook.

    Regards

  13. #13
    Registered User
    Join Date
    06-21-2011
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Delete Content in a row while opening

    ok looks perfect - thank you!

  14. #14
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,228

    Re: Delete Content in a row while opening

    You're welcome.

    If this has answered your question, please mark your thread as Solved. See my signature for details or the FAQ.

    Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0