VSTO:Excel在vb.net中调整范围

我试图从VBA切换到VSTO,但是当我尝试调整范围时遇到了一些我不熟悉的错误。

我有下面的代码应该最终能够分割一个单元格的行数x的范围。

我已经在VSTO中创build了一个插件。 当我通过按下Excel中的button来运行代码Visual Studo给了我一个“COMExeption是由用户代码未处理”,它highligts最后一行,我试图调整和输出范围。

我究竟做错了什么?

Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click Dim sheet As Excel.Worksheet = Globals.ThisAddIn.Application.ActiveSheet Dim inputrange As Excel.Range 'Dim cell As Excel.Range Dim numberOfOutputRows As Long Dim numberOfOutPutColumns As Long Dim outputRange As Excel.Range Dim inputArray(0, 0) As Long Dim i As Long Dim iRow As Long Dim iCol As Long Dim numberOfCells As Long Dim arrayInputValue As Excel.Range Dim val As Object inputrange = CType(sheet.Application.InputBox("select range",,,,,,, Type:=8), Excel.Range) 'Dim myRange As Excel.Range numberOfOutputRows = InputBox("You have " & inputrange.Count & " cells. Enter the number of rows you want to split up in") numberOfOutPutColumns = inputrange.Cells.Count / numberOfOutputRows outputRange = CType(sheet.Application.InputBox("Output to single cell",,,,,,, Type:=8), Excel.Range) inputrange = inputrange.Columns(1) ReDim inputArray(0 To numberOfOutputRows, 0 To numberOfOutPutColumns + 1) numberOfCells = inputrange.Cells.Count - 1 For i = 0 To numberOfCells arrayInputValue = CType(inputrange.Cells(i + 1), Excel.Range) val = arrayInputValue.value() iRow = i Mod numberOfOutputRows iCol = Int(i / numberOfOutputRows) inputArray(iRow + 1, iCol + 1) = val ' arrayInputValue Next i outputRange.Resize(RowSize:=UBound(inputArray, 1), ColumnSize:=UBound(inputArray, 2)).Value = inputArray 

你将不得不声明你的输出数组为Object而不是Long

 Dim inputArray(0, 0) As Object 

VSTO读取/写入单元格的数据作为对象。