我正在尝试从每个工作表中包含单个查询的Excel中获取数据。
所以,这是我的步骤:
1)创建一个excel应用程序 - >工作簿 - >工作表对象
2)获取所有工作表名称并指向特定工作表(我暂时正在进行编码)
3)从工作表中检索所有行到变体数组
4)最后将每个数组变量连接成一个字符串
我无法实现的最后一步。当我使用for for next循环时,我只获得25个记录到字符串中虽然数组有超过25个elemets init,或者如果我对数组使用join函数则抛出类型不匹配错误。
在我的Excel中,查询总是放在第一列本身的多行中。
要将数据放入数据表名称和实际查询值的数组变量中,我使用的是用户定义的推送功能。我希望我的数组基于no来动态增长。价值观。
请在下面找到我的代码:
ReDim arrSheetNames(-1)
ReDim k(-1)
Dim strQry()
'create an excel application object
set myExcel =CreateObject("Excel.Application")
'create an excel workbook object
set myWorkBook=myExcel.WorkBooks.Open("D:\Test.xlsx")
'Get the sheetnames into an array
For i = 1 To myWorkBook.Sheets.Count
fnPush arrSheetNames, myWorkBook.Sheets.Item(i).Name
Next
'Get th second sheet of the excel
set mysheet = myworkbook.Worksheets(arrSheetNames(0))
'Get the max row occupied in the excel file
Row=mysheet.UsedRange.Rows.Count
'Get the max column occupied in the excel file
Col=mysheet.UsedRange.columns.count
'To read the data from the entire Excel file
For i= 1 to Row
For j=1 to Col
fnPush k,mysheet.cells(i,j).value
Next
Next
m = join (arrSheetNames)
msgbox m
this is where im getting only 25 rows added to the string however there are 33 elements in the array k.
i=0
'
For i = 0 To UBound(k) Step 1
n = n & k(i)
Next
msgbox n
when i'm using this statement it's throwing an error for type mismatch
strQry = join(k)
msgbox strQry
'Save the Workbook
'myExcel.ActiveWorkbook.Save
'Close the Workbook
myExcel.ActiveWorkbook.Close
'Close Excel
myExcel.Application.Quit
Set mysheet =nothing
Set myWorkBook = nothing
Set myExcel = nothing
sub fnPush(arr, var)
dim uba
uba = UBound(arr)
redim preserve arr(uba+1)
arr(uba+1) = var
end sub
答案 0 :(得分:0)
VBScript中的MsgBox
函数最多可显示1023个字符。我猜你用join()
声明达到了这个限制。
答案 1 :(得分:0)
将Dim strQry()
替换为Dim strQry
,否则您应该尝试将连接结果字符串分配给出错的数组。