具有n行和列的dynamic数组

我必须读一个可以有'n'行和'n'列的excel,并将数据存储在数组中进行处理。

例如下面的例子:

Author Book No Value ABC A 1 100 DEF D 2 20 

我想要在一个单独的数组中的信息头和另一个信息行。

这是我到目前为止:

假设数据从Excel工作表的第13行开始 –

 var i, j, k ,l,m ,n; i=13; j=1, k=0; m=0; n=0; // i represents the row and j represents excel column(A) while(EApp.ActiveSheet.Cells(12,j).Value!="") { j=j+1; // Counting the Number of Columns (Header) } j=j-1; //decrementing the counter j by 1 k=j; while(j!=0) { ArrField[j]=EApp.ActiveSheet.Cells(12,j).Value; j=j-1; } 

这是我的尝试,输出是预期的,但有没有更好的方式来编码? 优选具有较小的variables。

 while(EApp.ActiveSheet.Cells(i,1).Value!="undefined") { m=k; j=0; ArrData[i]=new Array(m); while(m!=0) { ArrData[n][j]=EApp.ActiveSheet.Cells(i,m).Value; m=m-1; j=j+1; } n=n+1; i=+1; } 

另外我需要读取数组并将其存储到另一个系统的相应字段中。 我在这里有点迷路了

示例代码:(类似这样的)

 SetFieldValue(ArrField[0],ArrData[0][0]); 

arrays输出:

 Value,No,Book,Author (Header got in reverse order) 100,1,A,ABC,20,2,D,DEF (2 rows of Data also got in reverse order) 

任何build议专家?

你可以使用csv作为一个相当不错的方式dynamic数组。 这是我的示例代码。 我build议将其重写为您的需要,并添加一些error handling。

这里是表格数据:

 ID AUTHOR BOOK NO VALUE 1 ABC A 1 100 2 DEFG D 2 20 3 XYZ Q 3 55 

这里是代码:

 Sub test1() Dim arrayname As String Dim mytestW As Variant Dim mytestR As Variant '-->your array you can work with 'give your array a arrayname = authorbook (for example) arrayname = "authorbook" mytestW = dataintoarray(arrayname) mytestR = AsArray(arrayname, 2) '-->get the second row End Sub 

这里是function:

  Function dataintoarray(myarray As String) As Variant Dim res As Variant Dim wb As Workbook, ws As Worksheet Dim LastRow As Long, LastCol As Long Dim iRow As Integer, jCol As Integer Dim OutputFileNum As Integer Dim PathName As String Dim Line As String Line = "" Set wb = ThisWorkbook Set ws = wb.Worksheets("Sheet1") PathName = Application.ActiveWorkbook.Path OutputFileNum = FreeFile Open PathName & "\" & myarray & ".csv" For Output Lock Write As #OutputFileNum LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row LastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column For iRow = 1 To LastRow For jCol = 1 To LastCol Line = Line & ws.Cells(iRow, jCol).Value & "," Next jCol Print #OutputFileNum, Line Line = "" Next iRow Close OutputFileNum End Function Function AsArray(myarray As String, index As Integer) As Variant Dim res As Variant Dim wb As Workbook, ws As Worksheet Dim objFSO As Object, objFile As Object Dim sLine As String Set wb = ThisWorkbook Set ws = wb.Worksheets("Sheet1") Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(Application.ActiveWorkbook.Path & "\" & myarray & ".csv", 1) Do Until objFile.AtEndOfStream sLine = objFile.ReadLine 'the col of the ID = 0 If Split(sLine, ",")(0) = index Then AsArray = Split(sLine, ",") End If Loop objFile.Close End Function 

现在你的dynamic数组保存在mytestR中。 你可以像这样玩:

  ?mytestR(0) 2 ?mytestR(1) DEFG ?mytestR(2) D ?mytestR(3) 2 ?mytestR(4) 20