Results 1 to 20 of 24

Thread: Run Submacro If It Contains Data

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    403
    Location
    Quote Originally Posted by Paul_Hossler View Post
    Some ideas

    1. Not 'error checking' but more 'validity checking' -- IMHO it's better to prevent an error than to try and recover from one

    2. I prefer to avoid global variables as much as possible (Dim ws as Worksheet) and pass such things as a calling parameter (Call Markers(ws))

    3. You never defined what 'no data' means, so I went with a completely blank worksheet -- you may have to change that


    Option Explicit
    
    
    Dim ws As Worksheet
    
    
    Sub Main()
        Dim i As Long
        
        For i = 1 To 4
            Set ws = Worksheets("Data" & i)
            Call Markers(ws)
        Next
    
    
    End Sub
    
    
    Sub Markers(sht As Worksheet)
        Dim M As Double
        
        M = sht.Rows.Count  '   avoid overflow
        M = M * sht.Columns.Count
        
        If Application.WorksheetFunction.CountBlank(sht.Cells) = M Then
            MsgBox ws.Name & " is empty"
            Exit Sub
        End If
        
        MsgBox "Doing stuff on " & ws.Name
    End Sub
    Many thanks for this Paul, but as suggested by snb, this was not quite as simple!

    I think that I need to provide all the code for my "project"!

    I'm still a novice at VBA so am probably commiting some programming problems for myself along the way.

    I tried to "manipulate" the code you supplied to fit my requirements and failed miserably.

    Hopefully, I've managed to attach my form along with some "dummy" data.

    I really do appreciate your help!
    Attached Files Attached Files
    Last edited by HTSCF Fareha; 12-03-2020 at 03:05 AM.

Posting Permissions

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