使用vb.net将数据从excel复制到不同的工作簿中

时间:2014-02-10 09:35:56

标签: arrays vb.net excel

我有这个代码,我想将我的数据从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

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

1 个答案:

答案 0 :(得分:0)

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

    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并将其添加到{{ 1}}

相关问题