首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VB脚本连接函数正在引发类型不匹配错误。

VB脚本连接函数正在引发类型不匹配错误。
EN

Stack Overflow用户
提问于 2014-04-15 06:23:07
回答 2查看 1.3K关注 0票数 0

我正在尝试从excel中获取数据,该excel包含每个工作表中的一个查询。

下面是我的步骤:

1)创建excel应用程序->工作簿->工作表对象

2)获取所有的工作表名称,并指向特定的工作表(我正在进行重新编码)。

3)将工作表中的所有行检索为变体数组

4)最后将每个数组变量连接到一个字符串中。

我无法完成的最后一步。当我使用for next循环时,我只将25条记录放入字符串中,尽管数组中包含超过25条元素,或者如果对数组使用联接函数,则会引发类型错配错误。

在我的excel中,查询总是放在第一列本身的多行中。

为了将数据输入到工作表名和实际查询值的数组变量中,我使用了用户定义的push函数。我希望我的数组基于no动态增长。关于价值的。

请在下面找到我的代码:

代码语言:javascript
复制
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 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-15 19:19:07

MsgBox函数在VBScript中最多可以显示1023个字符。我猜您的join()语句达到了这个限制。

票数 0
EN

Stack Overflow用户

发布于 2014-04-15 19:51:10

Dim strQry()替换为Dim strQry,否则应尝试将联接结果字符串分配给产生错误的数组。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23076038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档