Cell formula promlem - Excel macro

Click For Summary
SUMMARY

The forum discussion centers on resolving a "##Name" error in an Excel macro due to incorrect formula syntax involving a dynamic range. The user initially defined the dynamic range as "Patientrange" but failed to encapsulate it in quotes within the formula. The correct syntax requires concatenating the dynamic range variable with the formula string, as demonstrated by the solution: ActiveCell.Formula = "=MEDIAN(" & Patientrange & ")". This adjustment effectively resolves the error and allows the macro to function as intended.

PREREQUISITES
  • Familiarity with Excel VBA programming
  • Understanding of dynamic ranges in Excel
  • Knowledge of Excel formulas and their syntax
  • Experience with debugging Excel macros
NEXT STEPS
  • Research Excel VBA string concatenation techniques
  • Learn about defining and using dynamic ranges in Excel
  • Explore common Excel formula errors and their resolutions
  • Practice debugging Excel macros using the VBA editor
USEFUL FOR

Excel developers, data analysts, and anyone looking to enhance their skills in Excel VBA programming and formula troubleshooting.

big man
Messages
241
Reaction score
0
Hi

I have been having a problem with a section of my code in the macro that I've created. The problem is that the formulae that I want to use aren't working and soe I get a "##Name" error in the cells where the formula is supposed to have worked.

Patientrange = Range(PatAgeLet & "2" & ":" & PatAgeLet & Locator).Address
For Count = 2 To 6
FormulaRange1 = Range(Infocol & Count).Offset(0, 1).Address
FormulaRange2 = Range(Infocol & Count).Offset(0, 2).Address
NewRange = Range(FormulaRange1 & ":" & FormulaRange2).Select
With Selection
.NumberFormat = "0"
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
If Count = 2 Then
ActiveCell.Formula = "=MIN(PatientRange)"
ElseIf Count = 3 Then
ActiveCell.Formula = "=MAX(Patientrange)"
ElseIf Count = 4 Then
ActiveCell.Formula = "=MEDIAN(Patientrange)"
ElseIf Count = 5 Then
ActiveCell.Value = Rangarrsize
ElseIf Count = 6 Then
ActiveCell.Formula = "=COUNTIF(Patientrange, ""<=40"")"
End If
Next Count

As you can see by this code Patientrange is a dynamic range and so this is the range that I want to use in my formulae. However, it just isn't working at all.

Any suggestions??
 
Technology news on Phys.org
Try dropping the .address from your range specifications.

I do not see where you have initialized Infocol.

Do not give up keep reading the help on how to specify a range.
 
Thanks for the reply Integral. Sorry about that but I was only providing a segment of the code so that's why you saw that infocol wasn't initialised.

I'm not sure if you're interested at all, but I found out what the problem was. When you specify the formula with the dynamic range you must put the dynamic range in quotes so instead of having ActiveCell.Formula = "=MEDIAN(Patientrange)" you must have ActiveCell.Formula = "=MEDIAN(" & Patientrange & ")".
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
8K