1

I cant run my code. Please help. I need to call the strings from different cell values on different sheets in the same workbook.

Sub ll()
    Dim result_range As Range
    Dim tag, stime, filtexp, PIServer, boundarytype As String
    Dim numvals, outcode As Integer
    Dim pi_formula As String
    tag = "Sheets(""Tags"").Range(""A2"")"
    stime = "Sheets(""DATAEXTRACT"").Range(""A7"")"
    numvals = 1
    filtexp = "Sheets(""BatchConditions"").Range(""B23"")"
    PIServer = "SGSitePI"
    Mode = "inside"
    'PINCompFilDat(tag, stime, numvals,filtexp,filtcode,outcode,PIServer ,mode)
    pi_formula = "=PINCompFilDat(" & Chr(34) & tag & Chr(34) & "," & Chr(34) & stime & Chr(34) & "," & _
                    Chr(34) & numvals & Chr(34) & "," & Chr(34) & filtexp & Chr(34) & "," & Chr(34) & fitcode & Chr(34) & "," & _
                    CStr(outcode) & "," & _
                    Chr(34) & PIServer & Chr(34) & "," & _
                    Chr(34) & Mode & Chr(34) & ")"
    Set result_range = Sheets("Sheet2").Range("A3:B3")
    result_range.ClearContents
    result_range.FormulaArray = pi_formula
    result_range.CurrentRegion.Select
    Selection.Columns(1).NumberFormat = "dd-mmm-yyyy hh:mm:ss"
    result_range.Copy
    result_range.PasteSpecial xlPasteValues
End Sub
5
  • It isn't clear exactly what you are trying to do - where is the code for PINCompFilDat? Commented Mar 29, 2016 at 3:15
  • I believe it's PINCompFilDat(tagname, stime, numvals, filtexp, filtcode, outcode, PIServer, mode) (defined on page 123 here) Commented Mar 29, 2016 at 3:22
  • fitcode should not enclosed in quotes as it is an integer. filtexp is 'A filter expression used to filter out results', not a string that looks like a range object address. Commented Mar 29, 2016 at 3:24
  • @Jeeped - Your Google-fu exceeds mine. Commented Mar 29, 2016 at 3:25
  • numvals is an integer as well; no wrapping quotes. Commented Mar 29, 2016 at 3:30

1 Answer 1

1

You can't access the Sheets collection from a worksheet function. Your pi_formula variable is set to this:

=PINCompFilDat("Sheets("Tags").Range("A2")", _
               "Sheets("DATAEXTRACT").Range("A7")","1", _
               "Sheets("BatchConditions").Range("B23")","",0,"SGSitePI","inside")

This isn't the correct calling convention for the function (or even really that close - thanks, @Jeeped).

There are a couple of issues. First, lines like this...

Dim numvals, outcode As Integer

...don't do what you expect. Each declaration separated by commas on the same line has to be typed, otherwise it is considered a Variant. It should look like this:

Dim numvals As Integer, outcode As Integer

Second, what you need to pass are values, not ranges. I'm assuming that tag , stime, and filtexp are supposed to be the values in Sheets("Tags").Range("A2"), Sheets("DATAEXTRACT").Range("A7"), and Sheets("BatchConditions").Range("B23"). If so, get the values, then build your function like you would type it on the worksheet (hint - Debug.Print pi_formula).

Third, as @Jeeped points out in the comments, the Integer parameters numvals, filtexp, and outcode should not be wrapped in quotes.

Finally, you never set a value for fitcode (or declare it). Mode is also not declared. You can easily catch this type of error by typing Option Explicit at the top of your module.

My best guess is that it should look something like this:

Sub ll()
    Dim result_range As Range
    Dim tag As String, stime As String, filtexp As String
    Dim PIServer As String, boundarytype As String
    Dim numvals As Integer, outcode As Integer, fitcode As Integer
    Dim pi_formula As String, Mode As String

    tag = Sheets("Tags").Range("A2").Value
    stime = Sheets("DATAEXTRACT").Range("A7").Value
    numvals = 1
    filtexp = Sheets("BatchConditions").Range("B23").Value
    PIServer = "SGSitePI"
    Mode = "inside"
    'fitcode = something or other.
    pi_formula = "=PINCompFilDat(" & Chr(34) & tag & Chr(34) & "," & Chr(34) & stime & Chr(34) & "," & _
                    numvals & "," & Chr(34) & filtexp & Chr(34) & "," & fitcode & "," & _
                    CStr(outcode) & "," & _
                    Chr(34) & PIServer & Chr(34) & "," & _
                    Chr(34) & Mode & Chr(34) & ")"
    Set result_range = Sheets("Sheet2").Range("A3:B3")
    result_range.ClearContents
    result_range.FormulaArray = pi_formula
    result_range.CurrentRegion.Select
    Selection.columns(1).NumberFormat = "dd-mmm-yyyy hh:mm:ss"
    result_range.Copy
    result_range.PasteSpecial xlPasteValues
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.