2

我必须阅读一个可以有“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;
}

这是我尝试过的,输出符合预期,但有更好的编码方法吗?最好用较小的变量。

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]);

数组输出:

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)

专家有什么建议吗?

4

1 回答 1

0

您可以以一种非常好的方式将 csv 用作动态数组。这是我的示例代码。我建议根据您的需要重写它并添加一些错误处理。

这是表格数据:

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 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

现在您已将动态数组保存在 mytestR 中。你可以像这样玩它:

 ?mytestR(0)
 2
 ?mytestR(1)
 DEFG
 ?mytestR(2)
 D
 ?mytestR(3)
 2
 ?mytestR(4)
 20
于 2016-06-15T14:02:35.367 回答