+ Reply to Thread
Results 1 to 5 of 5

Execute Macro only if number of rows are 4

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-08-2012
    Location
    Dubai
    MS-Off Ver
    Excel For Mac V16 365
    Posts
    145

    Execute Macro only if number of rows are 4

    Hello!!!

    i have several sheets of data in a workbook

    i need to run a macro in all sheets only if the number of rows are 4 excluding the heading row(titles)

    If the number of rows are more than 4..then data should be as it is.

    Thanks in Advance

    GOkz

  2. #2
    Forum Guru benishiryo's Avatar
    Join Date
    03-25-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2013
    Posts
    5,156

    Re: Execute Macro only if number of rows are 4

    hi gokzee, only when it's 4, and nothing lesser? try:

    Sub test()
    
    If WorksheetFunction.CountA(Range("A:A")) = 4 Then
        'insert code here
    End If
    
    End Sub

    Thanks, if you have clicked on the * and added our rep.

    If you're satisfied with the answer, click Thread Tools above your first post, select "Mark your thread as Solved".

    "Contentment is not the fulfillment of what you want, but the realization of what you already have."


    Tips & Tutorials I Compiled | How to Get Quick & Good Answers

  3. #3
    Forum Contributor
    Join Date
    11-08-2012
    Location
    Dubai
    MS-Off Ver
    Excel For Mac V16 365
    Posts
    145

    Re: Execute Macro only if number of rows are 4

    Sub takelrgnom()
    
        Dim LastRow As Long
        Dim i As Long
        Dim ws As Worksheet
            
        For Each ws In ThisWorkbook.Worksheets
        ws.Activate
        Application.DisplayAlerts = False
        If ws.Name = "0" Then GoTo xskip
        LastRow = Cells(Rows.Count, "F").End(xlUp).Row
            
        If WorksheetFunction.CountA(Range("A:A")) = 4 Then
        
        For i = LastRow To 2 Step -1
            If Cells(i, "N").Value <> Application.Evaluate("MAX(IF(" & Range(Cells(1, "F"), Cells(LastRow, "F")).Address _
                & "=" & Cells(i, "F").Address & "," & Range(Cells(1, "N"), Cells(LastRow, "N")).Address & "))") Then
                Rows(i).Delete
            End If
        Next i
        Application.DisplayAlerts = True
    xskip:
    End If
        Next ws
     
     End Sub
    I applied the code..but no effect on data on any sheets...

    what can be the reason??

    thanks

    Gokz

  4. #4
    Forum Contributor
    Join Date
    11-08-2012
    Location
    Dubai
    MS-Off Ver
    Excel For Mac V16 365
    Posts
    145

    Re: Execute Macro only if number of rows are 4

    Yeaa...it should be 5 including the heading i guess

  5. #5
    Forum Contributor
    Join Date
    11-08-2012
    Location
    Dubai
    MS-Off Ver
    Excel For Mac V16 365
    Posts
    145

    Re: Execute Macro only if number of rows are 4

    Thanks for the quick help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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