ilsley_SIMS Posted February 8, 2016 Posted February 8, 2016 Hi, I'm trying to reference a UDF I've built in the Range.Formula property (within VBA), but it keeps giving me the #NAME? error when I run the report from SIMS. This is how I'm trying to reference it in the VBA module: Function AOB_Cat(Category As Range, ref, joinStr As String) As String If Not IsNumeric(ref) Then ref = Chr(34) & ref & Chr(34) With Category AOB_Cat = Join(Filter(.Parent.Evaluate("if(" & .Offset(, 1).Address & _ "=" & ref & "," & .Address & ",char(2))"), Chr(2), 0), joinStr) End With End Function Sub CreateOnSubjects() Dim lastRow As Long Sheets("Sheet1").Range("AG1").value = "Subjects On Target" With Sheets("Sheet1") lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row .Range("AG2:AG" & lastRow).Formula = "=AOB_Cat($E2:$Z2,""O"","", "")" End With Sheets("Sheet1").Range("A1").Select End Sub If I use the UDF directly from the cell, it works fine, but I need to be able to put the formula directly into the cell via VBA. Any ideas? Thanks!
bobsmith Posted February 8, 2016 Posted February 8, 2016 would this get better responses in the excel area rather than MIS? 1
ilsley_SIMS Posted February 8, 2016 Author Posted February 8, 2016 I can't seem to see an Excel section... or do you mean the Office section?
ilsley_SIMS Posted February 8, 2016 Author Posted February 8, 2016 Ignore this thread... I've just figured out what was causing the problem!
vikpaw Posted February 9, 2016 Posted February 9, 2016 Ignore this thread... I've just figured out what was causing the problem! Please share so others can fix their own issues next time. I was going to say your code might be running too soon, before the data is properly in the cell. 1
ilsley_SIMS Posted February 9, 2016 Author Posted February 9, 2016 Please share so others can fix their own issues next time. I was going to say your code might be running too soon, before the data is properly in the cell. I fixed it by placing the UDF in a separate spreadsheet on our central staff shared area. In the SIMS Excel Report Template, I then added this spreadsheet as a reference in the reference library (VBA Editor > Tools > References > Browse). I then tweaked the code in the relevant sub routine like so: Sub CreateOnSubjects() Dim lastRow As Long Sheets("Sheet1").Range("AG1").value = "Subjects On Target" With Sheets("Sheet1") lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row .Range("AG2:AG" & lastRow).Formula = "='CustomFunctions.xlsm'!AOB_Cat($E2:$Z2,""O"","", "")" End With Sheets("Sheet1").Range("A1").Select End Sub So every time I run this report via SIMS now, the report template calls on the CustomFunctions.xlsm spreadsheet which contains the UDF that I wrote and works perfectly! Hope that all makes sense. 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now