【问题标题】:Using vb.net to copy data into different workbooks from excel使用 vb.net 将数据从 excel 复制到不同的工作簿
【发布时间】:2014-03-07 13:29:48
【问题描述】:

我有这段代码,我想将我的数据从 excel 放入多个数组(通过列数据),以便将每个数组粘贴到一个新的工作簿上。但是,我在这一行有错误:“myValue.Add(WSheet.Cells(q, i).value)”

ListView1.Items.Add(myValue) 仅供我仔细检查值,它会返回根据用户输入(文本框)选择的那些列的正确值。

我希望有人可以帮助我,因为我是 vb.net 的初学者

PS:我使用数组是因为我要处理大量数据,但不知道如何从那里着手。我想在粘贴到新工作簿之前实际操作数组(使用 excel 数组公式)。有可能吗?

   'start process
    colNumO = TextBox1.Text
    colNum = TextBox1.Text + 3

    For i = 4 To colNum

        firstcell = WSheet.Cells(1, i)
        lastcell = firstcell.End(Excel.XlDirection.xlDown)
        lastrow = lastcell.Row
        entireColumn = WSheet.Range(firstcell, lastcell)


        columnArray = entireColumn.Value

        'put the entire column values into array
        Dim myArray As Object(,)  '<-- declared as 2D Array  
        myArray = ColumnArray   'store the content of each cell 

        For r As Integer = 1 To myArray.GetUpperBound(0)

            For c As Integer = 1 To myArray.GetUpperBound(1)
                myValue = myArray(r, c)

                ListView1.Items.Add(myValue)

                'put entire column value into one array(myValue)
                If lastrow >= 2 Then

                    For q = 2 To lastrow
                        myValue.Add(WSheet.Cells(q, i).value)

                        'store entire column values into one sheet
                        WBook2.Sheets("sheet1").Range("A1:A" & lastrow).Value = myValue
                    Next q

                End If

            Next c
        Next r

    Next I

编辑后的帖子:

    'start process
    colNumO = TextBox1.Text
    colNum = TextBox1.Text + 3

    For i = 4 To colNum

        lastrow = WSheet.Cells(WSheet.Rows.Count, i).End(Excel.XlDirection.xlUp).Row

        If LastRow >= 2 Then
            Dim ColValuesList As New List(Of String)
            For c = 1 To lastrow
                ColValuesList.Add(WSheet.Cells(c, i).value)
            Next c
            OverallValueList.Add(ColValuesList)
        End If
    Next i

    'copy column data into new worksheet
    Dim colIndex As Integer = 1
    Dim rowIndex As Integer = 1
    Dim j As Integer = 0

    'Get the first worksheet in the book.
    Dim newworksheet As Excel.Worksheet

    With WBook2

        For j = 1 To .Worksheets.Count
            newworksheet = WBook2.Worksheets(j)

            For Each columnValueList In OverallValueList
                For Each value In columnValueList

                    newworksheet.Cells(rowIndex, colIndex).value = value
                    rowIndex += 1

                Next value
                '    colIndex += 1

                j += 1


            Next columnValueList

        Next j


    End With

但现在的问题是每个列表都粘贴到工作簿的同一个工作表上。我想要的是不同工作簿中的不同列表。有可能吗?

【问题讨论】:

  • myValue 是什么对象类型?您收到的异常消息是什么?
  • 您好,我已经编辑了我的代码,所以现在没有问题了。我已经在编辑部分陈述了我的问题。

标签: arrays vb.net excel


【解决方案1】:

根据您编辑的问题,更改以下代码:

    For j = 1 To .Worksheets.Count
        newworksheet = WBook2.Worksheets(j)

        For Each columnValueList In OverallValueList
            For Each value In columnValueList

                newworksheet.Cells(rowIndex, colIndex).value = value
                rowIndex += 1

            Next value
            '    colIndex += 1

            j += 1


        Next columnValueList

    Next j

成为:

    Dim j As Integer = 1
    For Each columnValueList In OverallValueList
        newworksheet = WBook2.Worksheets(j)            
        For Each value In columnValueList

            newworksheet.Cells(rowIndex, colIndex).value = value
            rowIndex += 1

        Next value
        'colIndex += 1

        j += 1
    Next columnValueList

此代码现在将遍历每个 list 并找到下一个 Worksheet(如果存在),然后它将遍历列表中的每个 value 并将其添加到 Worksheet

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 2017-02-26
    相关资源
    最近更新 更多