Troubleshoot function

mae0429

Board Regular
Joined
Jun 12, 2008
Messages
114
Thread posted on VBA Express (thought I'd come here for a little extra help):
http://www.vbaexpress.com/forum/showthread.php?t=20372

Here's what the meat of my code is:
Code:
Option Explicit
Sub CreateCharts()
    Dim C, newRange As Range
    Dim i, j, LastRow, numShts, numCols, m, n As Long
    Dim activeShtName As String
    Dim values As Variant
'Define number of sheets and number of paramters
    numShts = 24
    numCols = 12
'Create array of target values of parameters (blanks for text or date/time values)
    values = Array(, , 0.33, 0.34, 0.31, , 0.85, 0.83, 0.22, 0.654, 0.438, 0.398)
'Locate last row on each sheet
    For i = 1 To numShts
        Worksheets("Sheet" & i).Activate
        With ActiveSheet
            LastRow = [G65536].End(xlUp).Row
        End With
'If value is <10% or >10% of target value, make the value red and bold
        For j = 1 To numCols
            For Each C In Worksheets("Sheet" & i).Range(Cells(1, j), Cells(LastRow, j))
                If IsNumeric(C) = True And IsEmpty(C) = False Then
                    If C.Value < values(j - 1) - 0.1 * values(j - 1) Or C.Value > values(j - 1) + 0.1 * values(j - 1) Then
                        C.Font.ColorIndex = 3
                        C.Font.Bold = True
                    End If
                End If
            Next C
        Next j
 
'Set series to ranges
        For m = 1 To 6
            For n = 1 To 6
                Set newRange = SettingRange(m, n, LastRow)
            Next n
        Next m
    Next i
End Sub

With my function currently being:
Code:
Function SettingRange(ShockNumber As Variant, ParameterNumber As Long, MaxRow As Variant) As Range
    Dim iRow As Long
    Dim myRange As Range
    ParameterNumber = ParameterNumber + 9
    For iRow = 2 + ShockNumber To MaxRow Step 7
        Set myRange = Application.Union(myRange, Cells(iRow, ParameterNumber))
    Next iRow
    Set SettingRange = myRange
End Function

When I run it, I get "Runtime error '5', invalid procedure call or argument" on the line where I set myRange. The function itself seemed to work for xld, but not on mine...
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Your missing a line from XLD's function and I think that is causing an error because myRange contains nothing when it is within the Union Function:

His function was:

Rich (BB code):
Function SettingRange(ShockNumber As Long, ParameterNumber As Long, MaxRow As Variant) As Range 
    Dim iRow As Long 
    Dim myRange As Range 
    ParameterNumber = ParameterNumber + 9 
    For iRow = 2 + ShockNumber To MaxRow Step 7 
        If myRange Is Nothing Then 
           Set myRange = Cells(iRow, ParameterNumber) 
        Else 
            Set myRange = Application.Union(myRange, Cells(iRow, ParameterNumber)) 
        End If 
    Next iRow 
    Set SettingRange = myRange 
End Function
You are missing what is in bold. Hope that helps.
 
Upvote 0
Hi

You are using an unqualified reference there - if you do not precede Range and Cells with the Sheet on which they reside then they refer to teh ActiveSheet which may not be what you want.

Rich (BB code):
 Set myRange = Application.Union(myRange, Cells(iRow, ParameterNumber))

Cells above is unqualified - you need to specify the parent Sheet as well.
 
Upvote 0
Set myRange = Application.Union(myRange, Cells(iRow, ParameterNumber))

myRange has no assignment prior to this line. Its value = Empty
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,479
Members
448,967
Latest member
visheshkotha

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