Do While xlsheet.Cells(i, j) <> ""
MsgBox (xlsheet.Cells(i, j))
send_date(k) = CStr(xlsheet.Cells(i, j))
MsgBox (send_date(k))
number(k) = xlsheet.Cells(i, j + 3)
tuhao(k) = xlsheet.Cells(i, j + 2)
product_name(k) = xlsheet.Cells(i, j + 1)
MsgBox (send_date(k))
i = i + 1
Loop
运行MsgBox (xlsheet.Cells(i, j))是没有问题的,就是到send_date(k) = CStr(xlsheet.Cells(i, j))时会报错!
9 个解决方案
#1
send_date(k) = CStr(xlsheet.Cells(i, j))
-------------------
把k的值打印出来,看看是不是不在数组send_date的上/下限范围之内
-------------------
把k的值打印出来,看看是不是不在数组send_date的上/下限范围之内
#2
Private Sub File1_Click()
Dim i As Integer
Dim j As Integer
Dim k As String
Dim excelFile As String
Dim send_date() As String
Dim number() As Integer
Dim product_name() As String
Dim tuhao() As String
excelFile = File1.Path 'App.Path & "\myReport.xls" 'Excel文件名
If Right$(excelFile, 1) <> "\" Then excelFile = excelFile & "\"
excelFile = excelFile & File1.FileName
'建立EXCEL对象
Dim xlApp As Excel.Application '定义Excel的Application对象,Application对象相当于Excel程序
Dim xlbook As Excel.Workbook '定义Excel的Workbook对象,Workbook对象相当于Excel文件
Dim xlsheet As Excel.Worksheet '定义Excel的Worksheet对象,Worksheet对象相当于Excel文件中的一个表
Set xlApp = New Excel.Application '给Application分配内存空间,将其实例化
Set xlApp = CreateObject("Excel.Application") '创建Application对象
xlApp.Visible = False ' True '显示或隐藏被VB打开的Excel程序
Set xlbook = xlApp.Workbooks.Open(excelFile) '打开工作簿,excelFile为一个EXCEL报表文件
Set xlsheet = xlbook.Worksheets(1)
i = 1
j = 1
k = 0
Do While xlsheet.Cells(i, j) <> ""
MsgBox (xlsheet.Cells(i, j))
MsgBox (k)
send_date(k) = CStr(xlsheet.Cells(i, j))
MsgBox (send_date(k))
number(k) = xlsheet.Cells(i, j + 3)
tuhao(k) = xlsheet.Cells(i, j + 2)
product_name(k) = xlsheet.Cells(i, j + 1)
MsgBox (send_date(k))
i = i + 1
Loop
End Sub
这个完整的代码,k改为0或者1都有问题
#3
我把send_date(k) = CStr(xlsheet.Cells(i, j))改为send_date(1) = CStr(xlsheet.Cells(i, j)),还是报相同的错误!请高人指点!
#4
Dim send_date() As String
Dim number() As Integer
Dim product_name() As String
Dim tuhao() As String
--------------------------
定义的时候没给数组确定大小,使用数组前,你可以用Redim关键字指定大小:
......
k = 0
Do While xlsheet.Cells(i, j) <> ""
Redim Preserve send_date(k)
send_date(k) = CStr(xlsheet.Cells(i, j))
Redim Preserve number(k)
number(k) = xlsheet.Cells(i, j + 3)
Redim Preserve tuhao(k)
tuhao(k) = xlsheet.Cells(i, j + 2)
Redim Preserve product_name(k)
product_name(k) = xlsheet.Cells(i, j + 1)
i = i + 1
k=k+1
Loop
......
Dim number() As Integer
Dim product_name() As String
Dim tuhao() As String
--------------------------
定义的时候没给数组确定大小,使用数组前,你可以用Redim关键字指定大小:
......
k = 0
Do While xlsheet.Cells(i, j) <> ""
Redim Preserve send_date(k)
send_date(k) = CStr(xlsheet.Cells(i, j))
Redim Preserve number(k)
number(k) = xlsheet.Cells(i, j + 3)
Redim Preserve tuhao(k)
tuhao(k) = xlsheet.Cells(i, j + 2)
Redim Preserve product_name(k)
product_name(k) = xlsheet.Cells(i, j + 1)
i = i + 1
k=k+1
Loop
......
#5
楼上的方法不行啊,编译的时候就报错,array already dimensioned.
#6
我必须在申明里面确定了数组的大小,比如要开是这样定义:dim tuhao(100) as string,这样才不会报错。难道vb不能支持动态数组?
#7
声明里可以不用规定大小,但是你给数组赋值的时候,要通过redim给定大小,但是如果是用另外一个数组给他,可以不用redim大小
比如 Dim byRemain() As Byte
Dim OneMsg() As Byte
byRemain = bWockMsg ' bWockMsg是前边已知的一个数组
lLength= UBound(bWockMsg)
ReDim OneMsg(lLength)
CopyMemory OneMsg(0), byRemain(0), lLength
比如 Dim byRemain() As Byte
Dim OneMsg() As Byte
byRemain = bWockMsg ' bWockMsg是前边已知的一个数组
lLength= UBound(bWockMsg)
ReDim OneMsg(lLength)
CopyMemory OneMsg(0), byRemain(0), lLength
#8
那我前面的程序该怎么改呢?
#9
Dim send_date() As String
Dim number() As Integer
Dim product_name() As String
Dim tuhao() As String 我觉得你没必要都用数组 因为我看你的程序 都是只用到了他们一个元素 比如send_date(k) = CStr(xlsheet.Cells(i, j)) 有必要用数组么 要想用的话 之前给数组设定大小
Dim number() As Integer
Dim product_name() As String
Dim tuhao() As String 我觉得你没必要都用数组 因为我看你的程序 都是只用到了他们一个元素 比如send_date(k) = CStr(xlsheet.Cells(i, j)) 有必要用数组么 要想用的话 之前给数组设定大小
#1
send_date(k) = CStr(xlsheet.Cells(i, j))
-------------------
把k的值打印出来,看看是不是不在数组send_date的上/下限范围之内
-------------------
把k的值打印出来,看看是不是不在数组send_date的上/下限范围之内
#2
Private Sub File1_Click()
Dim i As Integer
Dim j As Integer
Dim k As String
Dim excelFile As String
Dim send_date() As String
Dim number() As Integer
Dim product_name() As String
Dim tuhao() As String
excelFile = File1.Path 'App.Path & "\myReport.xls" 'Excel文件名
If Right$(excelFile, 1) <> "\" Then excelFile = excelFile & "\"
excelFile = excelFile & File1.FileName
'建立EXCEL对象
Dim xlApp As Excel.Application '定义Excel的Application对象,Application对象相当于Excel程序
Dim xlbook As Excel.Workbook '定义Excel的Workbook对象,Workbook对象相当于Excel文件
Dim xlsheet As Excel.Worksheet '定义Excel的Worksheet对象,Worksheet对象相当于Excel文件中的一个表
Set xlApp = New Excel.Application '给Application分配内存空间,将其实例化
Set xlApp = CreateObject("Excel.Application") '创建Application对象
xlApp.Visible = False ' True '显示或隐藏被VB打开的Excel程序
Set xlbook = xlApp.Workbooks.Open(excelFile) '打开工作簿,excelFile为一个EXCEL报表文件
Set xlsheet = xlbook.Worksheets(1)
i = 1
j = 1
k = 0
Do While xlsheet.Cells(i, j) <> ""
MsgBox (xlsheet.Cells(i, j))
MsgBox (k)
send_date(k) = CStr(xlsheet.Cells(i, j))
MsgBox (send_date(k))
number(k) = xlsheet.Cells(i, j + 3)
tuhao(k) = xlsheet.Cells(i, j + 2)
product_name(k) = xlsheet.Cells(i, j + 1)
MsgBox (send_date(k))
i = i + 1
Loop
End Sub
这个完整的代码,k改为0或者1都有问题
#3
我把send_date(k) = CStr(xlsheet.Cells(i, j))改为send_date(1) = CStr(xlsheet.Cells(i, j)),还是报相同的错误!请高人指点!
#4
Dim send_date() As String
Dim number() As Integer
Dim product_name() As String
Dim tuhao() As String
--------------------------
定义的时候没给数组确定大小,使用数组前,你可以用Redim关键字指定大小:
......
k = 0
Do While xlsheet.Cells(i, j) <> ""
Redim Preserve send_date(k)
send_date(k) = CStr(xlsheet.Cells(i, j))
Redim Preserve number(k)
number(k) = xlsheet.Cells(i, j + 3)
Redim Preserve tuhao(k)
tuhao(k) = xlsheet.Cells(i, j + 2)
Redim Preserve product_name(k)
product_name(k) = xlsheet.Cells(i, j + 1)
i = i + 1
k=k+1
Loop
......
Dim number() As Integer
Dim product_name() As String
Dim tuhao() As String
--------------------------
定义的时候没给数组确定大小,使用数组前,你可以用Redim关键字指定大小:
......
k = 0
Do While xlsheet.Cells(i, j) <> ""
Redim Preserve send_date(k)
send_date(k) = CStr(xlsheet.Cells(i, j))
Redim Preserve number(k)
number(k) = xlsheet.Cells(i, j + 3)
Redim Preserve tuhao(k)
tuhao(k) = xlsheet.Cells(i, j + 2)
Redim Preserve product_name(k)
product_name(k) = xlsheet.Cells(i, j + 1)
i = i + 1
k=k+1
Loop
......
#5
楼上的方法不行啊,编译的时候就报错,array already dimensioned.
#6
我必须在申明里面确定了数组的大小,比如要开是这样定义:dim tuhao(100) as string,这样才不会报错。难道vb不能支持动态数组?
#7
声明里可以不用规定大小,但是你给数组赋值的时候,要通过redim给定大小,但是如果是用另外一个数组给他,可以不用redim大小
比如 Dim byRemain() As Byte
Dim OneMsg() As Byte
byRemain = bWockMsg ' bWockMsg是前边已知的一个数组
lLength= UBound(bWockMsg)
ReDim OneMsg(lLength)
CopyMemory OneMsg(0), byRemain(0), lLength
比如 Dim byRemain() As Byte
Dim OneMsg() As Byte
byRemain = bWockMsg ' bWockMsg是前边已知的一个数组
lLength= UBound(bWockMsg)
ReDim OneMsg(lLength)
CopyMemory OneMsg(0), byRemain(0), lLength
#8
那我前面的程序该怎么改呢?
#9
Dim send_date() As String
Dim number() As Integer
Dim product_name() As String
Dim tuhao() As String 我觉得你没必要都用数组 因为我看你的程序 都是只用到了他们一个元素 比如send_date(k) = CStr(xlsheet.Cells(i, j)) 有必要用数组么 要想用的话 之前给数组设定大小
Dim number() As Integer
Dim product_name() As String
Dim tuhao() As String 我觉得你没必要都用数组 因为我看你的程序 都是只用到了他们一个元素 比如send_date(k) = CStr(xlsheet.Cells(i, j)) 有必要用数组么 要想用的话 之前给数组设定大小