hopefully 3 easy questions :D

venomatic

New Member
Joined
Jun 20, 2011
Messages
36
Hi, I'm new to MrExcel and hope I can bother someone for a few quick questions. Thank you in advance!!!!!!! I apologize if some of them are too easy, and/or too weird.

1) Suppose I simply want to count the number of times I execute any program:
Code:
Static Counter As Integer
Dim Msg As String
Counter = Counter + 1
Msg = "Number of executions: " & Counter
Since the count displays 1 at the first run, is it fair to say that Counter is initialized at zero?

2) For
Code:
MsgBox Worksheets("Sheet1").Range("F3").Column
I can probably replace "Sheet1" with 1. Is this good practice?

3) Suppose I want to check if a group of cells all have formulas.
Code:
'apparently FormulaTest as Boolean isn't a good idea'
Dim FormulaTest As Variant
FormulaTest = Worksheets("Sheet2").Range("A1:A2").HasFormula
If TypeName(FormulaTest) = "Null" Then MsgBox "Mixed!"
So if A1 has a formula, and A2 doesn't, "Mixed!" is displayed. But for some reason, if I want to display or print true/false (i.e. both cells have formulas or don't have), I need to say MsgBox FormulaTest. This is all right with me, but although it works, it apparently has a run-time error.

Apparently, VBE says that the Variant can only return a Null, not a value. Assuming I understood that, is there a proper alternative to display true/false/mixed?

Thanks again :>
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
3) This shows how to probe if a range has all formulas, no formula or some yes some no.

Code:
Sub test()
    Dim formulaExists As Variant
    formulaExists = Range("A1:A2").HasFormula
    If IsNull(formulaExists) Then
        MsgBox "mixed"
    ElseIf formulaExists Then
        MsgBox "all have formula"
    Else
        MsgBox "none have formula"
    End If
End Sub
 
Upvote 0
certainly excellent work sir!
much obliged!

I'm not 100% sure on the last statement (although I've done a little if-else in C++ and C#), so I'll be reviewing it carefylly ;)
 
Upvote 0

Forum statistics

Threads
1,224,578
Messages
6,179,652
Members
452,934
Latest member
mm1t1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top