+ Reply to Thread
Results 1 to 8 of 8

Excel VBA HELP needed! Capturing Adding and Deleting of Rows on spreadsheet.

Hybrid View

nex2022 Excel VBA HELP needed!... 12-16-2014, 09:48 AM
LJMetzger Re: Excel VBA HELP needed!... 12-16-2014, 09:49 PM
nex2022 Re: Excel VBA HELP needed!... 12-17-2014, 10:57 AM
LJMetzger Re: Excel VBA HELP needed!... 12-17-2014, 05:48 PM
nex2022 Re: Excel VBA HELP needed!... 12-18-2014, 10:06 AM
Leith Ross Re: Excel VBA HELP needed!... 12-17-2014, 07:42 PM
LJMetzger Re: Excel VBA HELP needed!... 12-18-2014, 11:48 AM
nex2022 Re: Excel VBA HELP needed!... 12-18-2014, 12:01 PM
  1. #1
    Registered User
    Join Date
    09-24-2014
    Location
    canada
    MS-Off Ver
    excel 2010
    Posts
    4

    Excel VBA HELP needed! Capturing Adding and Deleting of Rows on spreadsheet.

    Hi, i'm new to using VBAs so i'm not sure what to usually post things under whether under "ThisWorkBook" or under "Module". So please be precise. I want to capture saves, prints, open and Deleting and Adding of rows.

    I have everything covered except the capturing of adding and deleting of rows. The following is what i have put under "ThisWorkbook":

    Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Dim Evnt As String
    Evnt = "Print"
    Call Elog(Evnt)
    End Sub
    
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim Evnt As String
    Evnt = "Save"
    Call Elog(Evnt)
    End Sub
    
    Private Sub Workbook_Open()
    Dim Evnt As String
    Evnt = "Open"
    Call Elog(Evnt)
    End Sub
    
    This works perfect and I also have the following under "Module1"
    
    Sub Elog(Evnt As String)
           
        Application.ScreenUpdating = False
        Dim cRecord As Long
        cSheet = ActiveSheet.Name
        
        If SheetExists("Log") = False Then
            Sheets.Add.Name = "Log"
            Sheets("Log").Select
            ActiveSheet.Protect "Pswd", UserInterfaceOnly:=True
        End If
        
            Sheets("Log").Visible = True
            Sheets("Log").Select
            ActiveSheet.Protect "Pswd", UserInterfaceOnly:=True
            
            cRecord = Range("A1")
        If cRecord <= 2 Then
            cRecord = 3
            Range("A2").Value = "Event"
            Range("B2").Value = "User Name"
            Range("C2").Value = "Domain"
            Range("D2").Value = "Computer"
            Range("E2").Value = "Date and Time"
        End If
        
        If Len(Evnt) < 25 Then Evnt = Application.Rept(" ", 25 - Len(Evnt)) & Evnt
        
        Range("A" & cRecord).Value = Evnt
        Range("B" & cRecord).Value = Environ("UserName")
        Range("C" & cRecord).Value = Environ("USERDOMAIN")
        Range("D" & cRecord).Value = Environ("COMPUTERNAME")
        Range("E" & cRecord).Value = Now()
        cRecord = cRecord + 1
        
        If cRecord > 20002 Then
            Range("A3:A5002").Select
            dRows = Selection.Rows.Count
            Selection.EntireRow.Delete
            cRecord = cRecord - dRows
        End If
        
        Range("A1") = cRecord
        Columns.AutoFit
        Sheets(cSheet).Select
        Sheets("Log").Visible = xlVeryHidden
        Application.ScreenUpdating = True
        
    End Sub
    Function SheetExists(SheetName As String) As Boolean
        On Error GoTo SheetDoesnotExit
        If Len(Sheets(SheetName).Name) > 0 Then
            SheetExists = True
            Exit Function
        End If
    SheetDoesnotExit:
            SheetExists = False
    End Function
    Sub ViewLog()
        Sheets("Log").Visible = True
        Sheets("Log").Select
    End Sub
    Sub HideLog()
        Sheets("Log").Visible = xlVeryHidden
    End Sub
    The outcome is on a spreadsheet called LOG and looks like this:

    Event User Name Domain Computer Date and Time
    Save ammonia HRRH SRES005 04/12/2014 10:06

    All i need now is for it to capture the adding and deleting of rows. Please help me with this and let me know exactly where to add what "ThisWorkbook" or "Module1"
    Last edited by Leith Ross; 12-17-2014 at 07:43 PM. Reason: Added Code Tags

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Excel VBA HELP needed! Capturing Adding and Deleting of Rows on spreadsheet.

    Hi nex2022 and welcome to ExcelForum.com,

    See the first answer in the following thread which seems to be an excellent implementation of your add/delete problem. However it needs a 'Named Range' on each sheet to identify whether there was an addition or a deletion. http://stackoverflow.com/questions/7...-deleting-rows

    Lewis


    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here


    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

  3. #3
    Registered User
    Join Date
    09-24-2014
    Location
    canada
    MS-Off Ver
    excel 2010
    Posts
    4

    Re: Excel VBA HELP needed! Capturing Adding and Deleting of Rows on spreadsheet.

    Hi Lewis thank you for the reply. As i am still new to this where do i post the code from that thread? (under module or the tab itself that i want tracked? because i do only want one tab tracked not the rest)

    This is what the other thread suggests: And what do you mean by a date name range and what does "RowMaker" mean what am I suppose to replace it with (tab name?)

    Private Sub Worksheet_Change(ByVal Target As Range)
        Static lngRow As Long
        Dim rng1 As Range
        Set rng1 = ThisWorkbook.Names("RowMarker").RefersToRange
        If lngRow = 0 Then
        lngRow = rng1.Row
            Exit Sub
        End If
        If rng1.Row = lngRow Then Exit Sub
        If rng1.Row < lngRow Then
            MsgBox lngRow - rng1.Row & " rows removed"
        Else
            MsgBox rng1.Row - lngRow & " rows added"
        End If
        lngRow = rng1.Row
    End Sub
    Last edited by Leith Ross; 12-17-2014 at 07:43 PM. Reason: Added Code Tags

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Excel VBA HELP needed! Capturing Adding and Deleting of Rows on spreadsheet.

    The sample code worked but was incomplete, because the value of the 'last row' had to be initialized. Otherwise, the first addition or deletion would not have been recognized. See the attached file that contains the following code:

    This Workbook module - for initialization:
    Option Explicit
    
    Private Sub Workbook_Open()
    
      'Identify the last row in the Named Range
      'Store the value in the Global variable defined in the 'Sheet1' module
      Sheet1.lngRow = ThisWorkbook.Names("RowMarker").RefersToRange.Row
    
      'Put the focus on Sheet 2
      Sheets("Sheet2").Select
      Range("A1").Select
    End Sub

    Sheet1 module:
    Option Explicit
    
    'Global variable that stores the 'last row' in NAMED RANGE 'RowMarker'
    Public lngRow As Long
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim rng1 As Range
        
        'Obtain the range referred to by the NAMED RANGE 'RowMarker'
        Set rng1 = ThisWorkbook.Names("RowMarker").RefersToRange
        
        'The following lines re-intialize the range if the global 'lngRow' gets reset to ZERO
        'This usually happens during debugging and should not happen in normal operation
        'If this happens addition/deletion of lines will not be identified
        If lngRow = 0 Then
            lngRow = rng1.Row
            MsgBox "Data Integrity Error. Global variable 'lngRow' had a value of ZERO."
            Exit Sub
        End If
        
        
        'Do nothing - the number of rows is the same
        If rng1.Row = lngRow Then
          Exit Sub
        End If
        
        'Identify whether rows were added or deleted
        If rng1.Row < lngRow Then
            MsgBox lngRow - rng1.Row & " rows removed"
        Else
            MsgBox rng1.Row - lngRow & " rows added"
        End If
        
        'When rows are added or deleted the NAMED RANGE automatically gets adjusted
        'The following global variable must now be adjusted to match the NEW 'last row' in the range
        lngRow = rng1.Row
        
    End Sub
    Lewis

  5. #5
    Registered User
    Join Date
    09-24-2014
    Location
    canada
    MS-Off Ver
    excel 2010
    Posts
    4

    Re: Excel VBA HELP needed! Capturing Adding and Deleting of Rows on spreadsheet.

    Task List - Copy.xlsm

    Hey Lewis,

    That's amazing of what you did! it works perfect but is there a way to have that message box changed from that to just being input on a spreadsheet thats hidden.

    I've attached what file i'm working on. if you go to the developer tab and run the macro "viewlog" and if you highlight everthing you can see that it tracks what computer name and user name committed the action. I have save, open and print figured out and now you have figure out the add/delete rows section. how can I add that to my already VBA code. I hope you can be of further help as you have been most helpful and i'm learning lots from it too
    Last edited by nex2022; 12-18-2014 at 11:35 AM.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Excel VBA HELP needed! Capturing Adding and Deleting of Rows on spreadsheet.

    Hello nex202,

    Welcome to the Forum!

    As a new member of the forum, I have added Code Tags to your posts. This makes it easier to see what you have written in the post and what is code. It is also easier for others to copy your code for testing.

    Click here to learn about formatting tags
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  7. #7
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Excel VBA HELP needed! Capturing Adding and Deleting of Rows on spreadsheet.

    Hi,

    Nice worksheet design.

    See the attached file. You will have to copy the code from my file to yours, since my Excel 2003 loses some of the features (e.g. colors, controls, etc) that are not available in Excel 2003.

    The NAMED RANGE is now automatically defined/modified when you open the workbook.

    ThisWorkBook Module (changes in red):
    Private Sub Workbook_Open()
      Dim Evnt As String
      Evnt = "Open"
      Call Elog(Evnt)
    
      'This adds or updates NAMED RANGE 'RowMarker' on 'Sheet1'
      '
      'The same thing can be done manually as follows (Excel 2003):
      'Insert > Name > Define (allows add, change or delete)
      
      'Adds or moves the named range (Change the last number to be more than the last row that will ever be used)
      Names.Add ("RowMarker"), Sheet1.Range("$A$1000")
      
      'Identify the last row in the Named Range
      'Store the value in the Global variable defined in the 'Sheet1' module
      'The 'NAMED RANGE' must be pre-defined or a RUNTIME ERROR will occur
      Sheet1.lngRow = ThisWorkbook.Names("RowMarker").RefersToRange.Row
      
    
    End Sub
    Sheet1 Module (Summary Sheet) - all code is new:
    Option Explicit
    
    Public lngRow As Long
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim rng1 As Range
        Dim Evnt As String
    
       'Obtain the range referred to by the NAMED RANGE 'RowMarker'
        Set rng1 = ThisWorkbook.Names("RowMarker").RefersToRange
        
        'The following lines re-intialize the range if the global 'lngRow' gets reset to ZERO
        'This usually happens during debugging and should not happen in normal operation
        'If this happens addition/deletion of lines will not be identified
        If lngRow = 0 Then
            lngRow = rng1.Row
            MsgBox "Data Integrity Error. Global variable 'lngRow' had a value of ZERO."
            Exit Sub
        End If
        
        
        'Do nothing - the number of rows is the same
        If rng1.Row = lngRow Then
          Exit Sub
        End If
        
        'Identify whether rows were added or deleted
        If rng1.Row < lngRow Then
            'MsgBox lngRow - rng1.Row & " rows removed"
            Evnt = lngRow - rng1.Row & " row(s) removed"
            Call Elog(Evnt)
        Else
            'MsgBox rng1.Row - lngRow & " rows added"
            Evnt = rng1.Row - lngRow & " row(s) added"
            Call Elog(Evnt)
        End If
        
        'When rows are added or deleted the NAMED RANGE automatically gets adjusted
        'The following global variable must now be adjusted to match the NEW 'last row' in the range
        lngRow = rng1.Row
        
    End Sub
    Lewis

  8. #8
    Registered User
    Join Date
    09-24-2014
    Location
    canada
    MS-Off Ver
    excel 2010
    Posts
    4

    Re: Excel VBA HELP needed! Capturing Adding and Deleting of Rows on spreadsheet.

    Hey Lewis,

    Thank you so much!!! Finally something that's complete and could track if people aren't following instructions!!!!!!! This is gonna save me and give me people that can't follow simple instructions!!! Thanks again Lewis and greatly appreciated!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 4
    Last Post: 03-20-2014, 10:00 AM
  2. Macro needed for capturing data corresponding to empty rows
    By rajkumarmp in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2013, 09:36 AM
  3. Comapring two excel worksheets and adding or deleting rows as needed
    By dmreno in forum Excel Programming / VBA / Macros
    Replies: 25
    Last Post: 01-31-2012, 08:55 AM
  4. VBA / Macro Conditional -> Adding rows and capturing data
    By RickyWJohnson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-29-2010, 09:10 PM
  5. Adding and Deleting rows - update formulas help needed.
    By bperks in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-31-2006, 03:35 PM

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.6.0 RC 1