VB は初めてで、ワークシートをコピーしようとするとイライラするようなクラッシュが発生します。
呼び出しworksheet.Copy()
はほとんどの場合機能しますが、ユーザーがセルにテキストを入力し、そのセルの選択を解除したり Enter キーを押したりしていない場合、「ワークシート クラスのメッセージのコピーに失敗しました」というメッセージとエラー コード -2146827284 でクラッシュします。なぜこれが起こっているのか、誰かが私に教えてもらえますか?
これがサブ全体です(ユーザーがExcelリボンのボタンをクリックすると呼び出されます)
Friend Sub CheckSheet(ByRef worksheet As Microsoft.Office.Interop.Excel.Worksheet, ByVal testType As List(Of String), ByRef guide As List(Of String), ByRef display As Dictionary(Of String, Boolean), ByRef URL As String)
'Read the labels and units from the current worksheet
Dim readings = loadReadings(worksheet, 3, 3, 4)
If readings Is Nothing Then 'If the readings are not successfully loaded
Exit Sub
End If
'Create a copy of the workbook to be processed to put the results in
Dim originalFilename As String = Application.ActiveWorkbook.FullName
Dim using1904system As Boolean = Application.ActiveWorkbook.Date1904
worksheet.Copy() 'FIXME Crash here if selected cell has newly entered text
worksheet = Application.ActiveWorkbook.ActiveSheet
Application.ActiveWorkbook.Date1904 = using1904system 'Make sure the new worksheet uses the same date option
'Get requests from worksheet
Dim cellMap = New Dictionary(Of String, Range)
Dim allRequests = getRequests(worksheet, readings, cellMap, 5, testType, guidelines, 1, 2, 3)
'Gets the results from the server and outputs them to a new Excel workbook
Dim success = processResults(allRequests, URL, worksheet, display, cellMap, 5, originalFilename, testType, guidelines)
If Not success Then
Exit Sub
End If
'Set focus to the checked worksheet
worksheet.Activate()
End Sub
編集:
別のセルを選択して、プログラムでセルの選択を解除しようとしました。
worksheet.Range("A1").Activate()
しかし、それは何の効果もないようです (クラッシュはまだ発生します)。私のアドインは、ユーザーから明示的に指示された場合にのみ copy メソッドを呼び出します。そのため、ユーザーがセルへのテキストの入力を停止し、ボタンをクリックしてアドインを実行すると、ボックスにテキストを入力しました。