Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Программирование, связанное с MS Office > 424 Object Required


Автор: SoulKeeper 14.7.2009, 14:47
Всем привет, есть такой код:

Код

Private Sub Workbook_Open()
    FixAutoFit
End Sub

Private Function FixAutoFit()
    Dim ws As Worksheet
    Dim cell As Range
    
    Application.ScreenUpdating = False
    
    For Each ws In Worksheets
        For Each cell In ws.UsedRange
            If cell.Rows.Count = 1 Then
                If cell.MergeCells And cell.WrapText And IsTopLeftCell(cell) Then
                    FixAutoFitForCell (cell)
                End If
            Else
                MsgBox ("Not Supported:" + cell.Address)
            End If
        Next cell
    Next ws
End Function

Private Function FixAutoFitForCell(cell As Range)
    Dim totalWidth As Single
    Dim oldHeight As Single
    Dim oldWidth As Single
    Dim cellCount As Integer
    Dim ac As Range
    
    cellCount = cell.MergeArea.Columns.Count
    
    For Each ac In cell.MergeArea.Cells
        totalWidth = totalWidth + ac.Width
    Next ac
    
    cell.WrapText = False
    cell.MergeArea.UnMerge
    oldWidth = cell.Width
    cell.Width = totalWidth
    cell.WrapText = True
    cell.Row.AutoFit

    If cell.Height < oldHeight Then
        cell.Height = oldHeight
    End If
    
    cell.Width = oldWidth
End Function

Private Function IsTopLeftCell(cell As Range)
    If cell.Address = cell.MergeArea.Cells(1, 1).Address Then
        IsTopLeftCell = True
    Else
        IsTopLeftCell = False
    End If
End Function


Excel при запуске макроса (а именно при вызове FixAutoFitForCell) выдает ошибку "Run-time Error '424' Object Required'"

Сразу оговорюсь что я Java программист и сюда забрел не от счасливой судьбы smile


По сабжу - не понятно почему вылетает, т.к. IsTopLeftCell принимает аналогичные параметры, но работет.


 smile 

Автор: Naghual 14.7.2009, 15:21
У вас нигде не инициирована oldHeight и ей не задано значение. Возможно что ошибка в этом.

На будущее, указывайте строку в которой происходит ошибка

Автор: SoulKeeper 14.7.2009, 15:27
Ладно, сделаем по другому smile

Код

Private Sub Workbook_Open()
    FixAutoFit
End Sub

Private Function FixAutoFit()
    Dim ws As Worksheet
    Dim cell As Range
    
    Application.ScreenUpdating = False
    
    For Each ws In Worksheets
        For Each cell In ws.UsedRange.Cells
            If cell.Rows.Count = 1 Then
                If cell.MergeCells And cell.WrapText And IsTopLeftCell(cell) Then
                    FixAutoFitForCell (cell)
                End If
            Else
                MsgBox ("Not Supported:" + cell.Address)
            End If
        Next cell
    Next ws
End Function

Private Function FixAutoFitForCell(cell As Range)
'    Dim totalWidth As Single
'    Dim oldHeight As Single
'    Dim oldWidth As Single
'    Dim cellCount As Integer
'    Dim ac As Range
    
'    cellCount = cell.MergeArea.Columns.Count
    
'    For Each ac In cell.MergeArea.Cells
'        totalWidth = totalWidth + ac.Width
'    Next ac
    
'    cell.WrapText = False
'    cell.MergeArea.UnMerge
'    oldWidth = cell.Width
'    cell.Width = totalWidth
'    cell.WrapText = True
'    cell.Row.AutoFit

'    If cell.Height < oldHeight Then
'        cell.Height = oldHeight
'    End If
    
'    cell.Width = oldWidth
End Function

Private Function IsTopLeftCell(cell As Range)
    If cell.Address = cell.MergeArea.Cells(1, 1).Address Then
        IsTopLeftCell = True
    Else
        IsTopLeftCell = False
    End If
End Function


15-я строка, вызов FixAutoFitForCell.

Ошибка та что и была.

Автор: Naghual 14.7.2009, 15:45
А...  Как насчет того, что это Функция? Как насчет Скобок? Что с возвращаемым значением?

Автор: SoulKeeper 14.7.2009, 15:57
Мой опыт VBA = 3 часа, дедлайн завтра smile
Судя по java, php, c++, c# и другим - функция не обязательно должна возвращать значение. Или в VBA должна?


___
Хотя нет, не должна. FixAutoFit ничего не возвращает, но вызывается...
Функция с аналогчной сигнатурой 
Код

Private Function IsTopLeftCell(cell As Range)

вызывется без проблем.

Как-то оно странно, не по человечески smile

Автор: Naghual 15.7.2009, 22:57
Как-то странно, но у меня Ваш код не вызывает ошибок.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)