0
我有這個代碼,我想將我的數據從excel放入多個數組(通過列數據),以便每個數組粘貼到新的工作簿上。然而,我有這條線上的錯誤:「myValue.Add(WSheet.Cells(q,i).value)」使用vb.net將數據複製到excel中的不同工作簿
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'?什麼是你收到的例外消息? – Nunners
你好,我編輯了我的代碼,所以現在沒有問題。我已經在編輯過的部分說明了我的問題。 – user3277702