Forum Discussion
Understanding the code
- Jan 23, 2023
GetOpenFileName with MultiSelect:=True can return two completely different types of values:
- If the user selects one or more files and clicks Open, it returns an array of string values. Each item of the array is the path + filename of one of he selected files. VarType(fNameList) will be vbArray+vbString.
- If the user clicks Cancel, it returns the Boolean value False. VarType(fNameList) will be vbBoolean.
So the line If (vbBoolean <> VarType(fnameList)) Then checks that fNameList is not a Boolean, i.e. it must be an array.
The next line If (UBound(fnameList) > 0) Then will only be executed if the user clicked Open. It checks that the fNameList array has more than 0 items. This line is actually not necessary, if the user clicked Open they have selected at least one file.
I assume that the code below that loops through the selected file names, opens each workbook in turn, then loops through the sheets of the workbook. The lines
countFiles = 0
countSheets = 0
initialize the count of files (workbooks) and of sheets to 0.
GetOpenFileName with MultiSelect:=True can return two completely different types of values:
- If the user selects one or more files and clicks Open, it returns an array of string values. Each item of the array is the path + filename of one of he selected files. VarType(fNameList) will be vbArray+vbString.
- If the user clicks Cancel, it returns the Boolean value False. VarType(fNameList) will be vbBoolean.
So the line If (vbBoolean <> VarType(fnameList)) Then checks that fNameList is not a Boolean, i.e. it must be an array.
The next line If (UBound(fnameList) > 0) Then will only be executed if the user clicked Open. It checks that the fNameList array has more than 0 items. This line is actually not necessary, if the user clicked Open they have selected at least one file.
I assume that the code below that loops through the selected file names, opens each workbook in turn, then loops through the sheets of the workbook. The lines
countFiles = 0
countSheets = 0
initialize the count of files (workbooks) and of sheets to 0.