VB 将数组的数据保存到Excel中
有6个一维数组a1(20)b1(20)....f1(20)都已经赋值,想把它们用Excel表格输出6列20行...
有6个一维数组 a1(20) b1(20) ....f1(20) 都已经赋值,想把它们用Excel表格输出 6列 20行
展开
4个回答
展开全部
给你个高速两维数组的参考一下
你改改就是
Dim aa(300, 200) As Double
Private Sub Command1_Click()
Dim XlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
XlApp.Visible = True
Set xlBook = XlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
xlSheet.Range(xlSheet.Cells(LBound(aa, 1) + 1, LBound(aa, 2) + 1), xlSheet.Cells(UBound(aa, 1) + 1, UBound(aa, 2) + 1)).Value = aa
Erase aa '要释放数组,免得占用内存
End Sub
Private Sub Form_Load()
For i = LBound(aa, 1) To UBound(aa, 1)
For j = LBound(aa, 2) To UBound(aa, 2)
aa(i, j) = i + j
Next
Next i
End Sub
你改改就是
Dim aa(300, 200) As Double
Private Sub Command1_Click()
Dim XlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
XlApp.Visible = True
Set xlBook = XlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
xlSheet.Range(xlSheet.Cells(LBound(aa, 1) + 1, LBound(aa, 2) + 1), xlSheet.Cells(UBound(aa, 1) + 1, UBound(aa, 2) + 1)).Value = aa
Erase aa '要释放数组,免得占用内存
End Sub
Private Sub Form_Load()
For i = LBound(aa, 1) To UBound(aa, 1)
For j = LBound(aa, 2) To UBound(aa, 2)
aa(i, j) = i + j
Next
Next i
End Sub
展开全部
直接用赋值语句就可以啦。
[a1:a20] = Application.WorksheetFunction.Transpose(a1)
[b1:b20] = Application.WorksheetFunction.Transpose(b1)
[c1:c20] = Application.WorksheetFunction.Transpose(c1)
[d1:d20] = Application.WorksheetFunction.Transpose(d1)
[e1:e20] = Application.WorksheetFunction.Transpose(e1)
[f1:f20] = Application.WorksheetFunction.Transpose(f1)
[a1:a20] = Application.WorksheetFunction.Transpose(a1)
[b1:b20] = Application.WorksheetFunction.Transpose(b1)
[c1:c20] = Application.WorksheetFunction.Transpose(c1)
[d1:d20] = Application.WorksheetFunction.Transpose(d1)
[e1:e20] = Application.WorksheetFunction.Transpose(e1)
[f1:f20] = Application.WorksheetFunction.Transpose(f1)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Command1_Click()
Dim XlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
XlApp.Visible = True
Set xlBook = XlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
For i=1 to 20 '你这数组是21个数不是20个
xlSheet.Range(xlSheet.Cells(1,i).Value=a1(i)
xlSheet.Range(xlSheet.Cells(2,i).Value=b1(i)
xlSheet.Range(xlSheet.Cells(3,i).Value=c1(i)
xlSheet.Range(xlSheet.Cells(4,i).Value=d1(i)
xlSheet.Range(xlSheet.Cells(5,i).Value=e1(i)
xlSheet.Range(xlSheet.Cells(6,i).Value=f1(i)
Next i
End Sub
Dim XlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
XlApp.Visible = True
Set xlBook = XlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
For i=1 to 20 '你这数组是21个数不是20个
xlSheet.Range(xlSheet.Cells(1,i).Value=a1(i)
xlSheet.Range(xlSheet.Cells(2,i).Value=b1(i)
xlSheet.Range(xlSheet.Cells(3,i).Value=c1(i)
xlSheet.Range(xlSheet.Cells(4,i).Value=d1(i)
xlSheet.Range(xlSheet.Cells(5,i).Value=e1(i)
xlSheet.Range(xlSheet.Cells(6,i).Value=f1(i)
Next i
End Sub
更多追问追答
追问
对象Range的方法_WorkSheet失败
追答
xlSheet.Range(xlSheet.Cells(1,i).Value=a1(i)
xlSheet.Range(xlSheet.Cells(2,i).Value=b1(i)
xlSheet.Range(xlSheet.Cells(3,i).Value=c1(i)
xlSheet.Range(xlSheet.Cells(4,i).Value=d1(i)
xlSheet.Range(xlSheet.Cells(5,i).Value=e1(i)
xlSheet.Range(xlSheet.Cells(6,i).Value=f1(i)
这一段,公式的括号不完全,你试着完全,再试试
xlSheet.Range(xlSheet.Cells(1,i)).Value=a1(i)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Sub dada()
Dim arr(1 To 12) As Integer
Dim i As Integer
For i = 1 To 12
arr(i) = Int(100 * Rnd) + 1 '对arr(1) 到arr(12)存入1到100的随机整数
Next
For i = 1 To 12
Cells(i, 1) = arr(i) '单元格第一列的第1到12行分别赋值为arr(1) 到arr(100)
Next
End Sub
Dim arr(1 To 12) As Integer
Dim i As Integer
For i = 1 To 12
arr(i) = Int(100 * Rnd) + 1 '对arr(1) 到arr(12)存入1到100的随机整数
Next
For i = 1 To 12
Cells(i, 1) = arr(i) '单元格第一列的第1到12行分别赋值为arr(1) 到arr(100)
Next
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询