2015-05-29 83 views
0

我在下面的Excel代碼中收到上述錯誤。此代碼用於重新排列合併單元格的行高度。該代碼直接從微軟的支持網站複製,並且如果僅使用一次就可以正常工作。Excel 2013 VBA:錯誤-2147417848對象'範圍'的方法'選擇'失敗

在我的循環下面,它實際上在第一個六個合併的單元格上工作得很好。在執行「for」循環的24次迭代

NewWorksheet.Range(NewWorksheet.Cells(RowCounter, 5), NewWorksheet.Cells(RowCounter, 6)).Select 

錯誤只發生就行了。 ErrorFile_LastRow的值是43.第一個合併的單元格在第18行。第24行有未合併的單元格。我從https://support.microsoft.com/en-us/kb/319832的微軟發現了一個有點相關的文章,並在下面的代碼中添加了oXL引用。基於同一篇文章,我在違規行中添加了NewWorksheet,但沒有任何幫助。

在完全相同的迭代regregless任何上述更改或什麼和多少應用程序正在運行的同一行上發生相同的錯誤。我甚至試圖重新啓動我的筆記本電腦,並確保只有宏運行沒有其他辦公應用程序,但即使這似乎沒有幫助。事實上,如果有人可以告訴我如何調整合並單元格的行高而不使用'Select','ActiveCell'等,這將是最好的,因爲我試圖不使用這些類型的命令以增加代碼的健壯性&速度,並且確保宏不停止處理,因爲我正在處理另一個應用程序。

培訓相關的代碼段(宏本身是非常複雜的): -

Dim oXL As Excel.Application 
Dim NewWorkbook As Workbook 
Dim NewWorksheet As Worksheet 
Dim ErrorFile_LastRow As Long 
Dim MergedHeight As Single 
Dim MergedWidth As Single 
Dim PossNewRowHeight As Single 
Dim lngRowCount As Long 
Dim lngColCount As Long 
Dim i As Long 
Dim RowCounter As Long 
Dim ActiveCellWidth As Single 

Set oXL = Excel.Application 

    oXL.Workbooks.Add 
    '------------------------------------------------------------------------------------------------- 
    ' Create a workbook handle for the new workbook 
    '------------------------------------------------------------------------------------------------- 
    Set NewWorkbook = oXL.ActiveWorkbook 
    '---------------------------------------------------------------------------------------------------------- 
    ' Use the new workbook handle. 
    '---------------------------------------------------------------------------------------------------------- 
    With NewWorkbook 
     '------------------------------------------------------------------------------------------------- 
     ' Create a new worksheet handle for the new workbook. 
     '------------------------------------------------------------------------------------------------- 
     Set NewWorksheet = .Sheets(1) 
    End With 

    '---------------------------------------------------------------------------------------------------------- 
    ' Use the new worksheet handle. 
    '---------------------------------------------------------------------------------------------------------- 
    With NewWorksheet 
     '------------------------------------------------------------------------------------------------- 
     ' Capture the last row of data to process. 
     '------------------------------------------------------------------------------------------------- 
     ErrorFile_LastRow = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row 
     .Range(Cells(1, 1), Cells(ErrorFile_LastRow, 6)).Select 
    End With 

    NewWorksheet.Activate 
    Application.PrintCommunication = True 
    NewWorksheet.PageSetup.PrintArea = Selection.Address 
    '------------------------------------------------------------------------------------------------- 
    ' Adjust the row height to fit the data. 
    '------------------------------------------------------------------------------------------------- 
    For RowCounter = 2 To ErrorFile_LastRow 
     If RowCounter <> ErrorFile_LastRow Then 
      NewWorksheet.Range(NewWorksheet.Cells(RowCounter, 5), NewWorksheet.Cells(RowCounter, 6)).Select 
     Else 
      NewWorksheet.Range(Cells(RowCounter, 1), Cells(RowCounter, 6)).Select 
     End If 
     If ActiveCell.MergeCells Then 
      With ActiveCell.MergeArea 
       If .WrapText = True Then 
        lngRowCount = .Rows.Count 
        lngColCount = .Columns.Count 
        MergedHeight = Selection.Height 
        For i = 1 To lngColCount 
         MergedWidth = .Cells(1, i).ColumnWidth + 1 + MergedWidth 
        Next i 
        If MergedHeight > 409.5 Then 
         MergedHeight = 409.5 
        End If 
        If MergedWidth > 409.5 Then 
         MergedHeight = 409.5 
        End If 
        ActiveCellWidth = ActiveCell.ColumnWidth 
        .MergeCells = False 
        .Cells(1).RowHeight = MergedHeight 
        .Cells(1).ColumnWidth = MergedWidth 
        .EntireRow.AutoFit 
        PossNewRowHeight = .Cells(1).RowHeight 
        .MergeCells = True 
        .Cells(1).ColumnWidth = ActiveCellWidth 
        For i = 1 To lngRowCount 
         .Cells(i, 1).RowHeight = PossNewRowHeight/lngRowCount 
        Next i 
       End If 
      End With 
     End If 
    Next RowCounter 
+0

我沒有看到該特定行代碼失敗的明顯原因。您得到的錯誤幾乎總是與嘗試從不是「ActiveSheet」的「工作表」中的「選擇」單元格有關。在錯誤行之前添加一行以確認您確實位於正確的表單上:「Debug.Print ActiveSheet.Name = NewWorksheet.Name」。既然這是一個「複雜的宏」,你是否有任何「Worksheet_Change」或「Worksheet_SelectionChange」事件?這些可能會對所有正在進行的選擇造成嚴重破壞。 –

回答

0

化妝起訴ErrorFile_LastRow某處初始化。

+0

對不起,我錯過了在這裏複製代碼。我將它添加到上面的代碼片段中。 ErrorFile_LastRow的值是43。 – user4954633

相關問題