2014-02-10 4 views
0

이 코드는 Excel에서 내 데이터를 여러 배열 (열 데이터를 통해)에 저장하여 각 배열을 새 통합 문서에 붙여 넣을 때 사용합니다. 그러나이 줄에 오류가 있습니다 : "myValue.Add (WSheet.Cells (q, i) .value)"vb.net을 사용하여 Excel에서 다른 통합 문서로 데이터 복사

ListView1.Items.Add (myValue)는 값을 두 번 확인하기 만하면됩니다. 사용자 입력 (텍스트 상자)에 따라 선택된 열의 올바른 값 나는 많은 양의 데이터를 처리하고 있기 때문에 내가 배열을 사용하지만, 거기에서 진행하는 방법을 잘되지 않습니다 :

는 제가 vb.net에서

PS를 초보자 나처럼 누군가가 여기 좀 도와 수 있기를 바랍니다. 새로운 통합 문서에 붙여 넣기 전에 실제로 배열을 조작 (배열 수식 사용)하고 싶습니다. 가능한가?

'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 

그러나 문제는 이제 통합 문서의 같은 워크 시트에 붙여 각각의 목록입니다. 내가 원하는 것은 다른 목록을 다른 통합 문서로 만드는 것입니다. 가능한가? 편집 된 질문을 바탕으로

+0

에 추가합니다. 어떤 객체 유형이 'myValue'입니까? 그리고 당신이 얻는 예외 메시지는 무엇입니까? – Nunners

+0

안녕하세요, 이제는 문제가없는 코드를 편집했습니다. 나는 편집 된 부분에 내 문제를 진술했다. – user3277702

답변

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 

이되고 :이 코드는 현재 각 list 반복하고 (있는 경우) 다음 Worksheet를 찾을 수

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 

을, 목록의 각 value을 반복하여 Worksheet

관련 문제