0

I want to use VBScript to process EXCEL instead of VBA. But I encountered code errors when calling method Range.Find, see below

Function find_range(wb, domain, var)
    Dim sheet
    Dim rg1, rg2, rg3
    Set sheet = wb.Sheets(domain)

    Set rg1 = sheet.Range("D:D").Find(var, , xlValues, xlWhole )

    If Not rg1 is Nothing Then
        'msgbox rg1.Cells(1,1).row
        Set rg2 = sheet.Cells(rg1.Cells(1,1).row, 19)
        msgbox(rg2.value)
    End if
End Function

when executed find_range function, I got

variable is undefined "xlValues" "xlWhole"

error.

So I guess that I cannot just use excel built-in constants this way in VBScript.
So what is the correct way?

7
  • 2
    By declaring them. Normally we just put the value of the const in. So func(xlCrap) becomes func(3). See the const keyword if you want stupid code at microsoft.com/en-au/download/details.aspx?id=2764
    – user6017774
    Oct 9, 2016 at 9:36
  • @Noodles Link is broken.
    – Zev Spitz
    Oct 9, 2016 at 10:13
  • Looks like it's been taken down. I put a copy up on my skydrive 1drv.ms/f/s!AvqkaKIXzvDieQFjUcKneSZhDjw
    – user6017774
    Oct 9, 2016 at 23:12
  • @Noodles there is nothing "stupid" about using named constants over plain numeric values, they enhance the readability and reusability of the code. Which is more descriptive func(3) or func(xlCrap)?
    – user692942
    Oct 11, 2016 at 23:44
  • As I program with documentation open. Using xlCrap requires me to verify that it is in fact set to 3. The very first principal of writing code is readability - one thing that means is everything about what you are doing is together on the page, no or minimal scrolling. No const hiding at the top of the file (or worse variables acting as constants). While declaring constants near where they are used is ok, but it adds extra lines.
    – user6017774
    Oct 12, 2016 at 0:13

1 Answer 1

4

Unfortunately these Named Constants are part of the Excel Object Library which VBScript has no way of referencing so the best approach is to lookup the named constants in Object Browser inside Excel VBA or online via various references then create your own named constants and use them in your code.

In this example you are using two enumerations that can be identified by looking up the Range.Find() method.

  • xlValues is a named constant in the xlFindLookIn enumeration and has the value -4163.
  • xlWhole is a named constant in the xlLookAt enumeration and has a value of 1.

So once you know the value you can define them and your code should work without any more changes being required.

Const xlValues = -4163
Const xlWhole = 1

Ideally these values should be declared in the global scope of your script so they are accessible to any function or procedure.

You might ask why not specify the numeric value? Well while this is indeed a valid approach if you use the value in multiple places you then have to modify that value in multiple places if the value ever changes (however unlikely in this scenario). With a named constant you make one change and wherever in your code that value is referenced is also changed.

It's also worth noting that VBScript is only interested in the numeric value so technically you can name the constants anything you wish. However, it is good practice to follow the naming convention, especially if you re-used the function code in Excel VBA for example.

Not the answer you're looking for? Browse other questions tagged or ask your own question.