I have a function written in Visual Basic, which is supposed to read in two Excel workbooks and to some textual analysis on them.
The issue that I am getting is that whenever I try to run the function in a cell in my sheet, I get the error "constant expression required" in VBA and the "#VALUE!" error in the cell.
My function line looks like
Function CriticalPercentage() As Double
and I call the function by typing "=CriticalPercentage()" in the cell.
When I click enter after typing my function call in the cell it goes to VBA, shows the error message, and Function CriticalPercentage() As Double
is highlighted in yellow. It also highlights in blue the .Count in the very last line of the following code that I have provided from my function.
Function CriticalPercentage() As Double
Debug.Print "the first line works fine"
ByVal WorkbookName1 As String, ByVal WorkbookName2 As String
' this section accesses the excel files and sheets the script will be working with
WorkbookName1 = "name of excel file"
WorkbookName2 = "name of excel file"
Workbooks.Open WorkbookName1
Workbooks.Open WorkbookName2
' define variables for the the two workbooks
Dim Workbook1, Workbook2 As Excel.Workbook
Dim Worksheet1, Worksheet2 As Excel.Worksheet
' create workbook objects to work with
Set Workbook1 = Excel.Workbooks(WorkbookName1)
Set Workbook2 = Excel.Workbooks(WorkbookName2)
' sets worksheet variables to the sheets contained in the two workbooks, respectively
Set Worksheet1 = Workbook1.Sheets(1).Select
Set Worksheet2 = Workbook2.Sheets(1).Select
'---------------------------------------------------------------------------------------
' this section of code works on reading in all the neccessary info from the ARCHER excel file
' search for [Application Name & Finding]
Dim i, j, k As Integer
Dim numRows As Integer
Const numRows = Worksheet1.UsedRange.Rows.Count
I have tried putting a Debug.Print statement right after Function CriticalPercentage() As Double but it does not show up in the immediate window, so I am led to believe that I am doing something wrong in the declaheration of the function.
I would appreciate any help on this and any insight as to why I am getting this error. if further code needs to be provided I will gladly do so.
Thanks so much!
Dim numRows As Integer(btw that really should beAs Long). Get rid of theConst. - BigBenSelecteither. - BigBen