VB 将数组的数据保存到Excel中

2025-02-27 13:37:39
推荐回答(4个)
回答1:

给你个高速两维数组的参考一下
你改改就是
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

回答2:

直接用赋值语句就可以啦。
[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)

回答3:

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

回答4:

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