Excel 2003中的宏不适用于Excel 2007

时间:2014-09-26 21:22:19

标签: vba excel-vba excel-2007 excel

最近我已经从Excel 2003升级到Excel 2007.除了一个宏的一部分外,几乎所有的宏都可以工作。在此文件的此工作表上,大约有21个插槽已调整大小以放置图片。由于作品的性质,有时会有超过21张图片输入到文档中。在此之前它只是一个麻烦,因为你有时会忘记复制行,然后无法正确调整图像大小。

因此,在将图像插入照片纸并运行宏时,如果有21张或更少的照片,它只会将所有照片放入插槽并调整大小。或多或少,这很好,有一些我需要调整的东西,但一般它都在工作。

当存在>时出现问题。插入21张照片。代码是找到最后一个可用的图片单元格,然后复制并粘贴所需的行。 Excel 2007找不到任何这些单元格。我从录制的宏中复制的格式,解释了奇怪的样式选择。

图片单元格如下所示:Please See Image Above

我认为这可能是关于2003年到2007年之间该盒子的样式是如何改变的,所以我决定录制另一个宏来获得#34;新的"格式。但是,即使使用Excel的“查找”对话框并选择其中一个照片单元格进行格式化,它也会出现“#34; Excel无法找到您要查找的数据”的错误。"正如预期的那样,宏录制器检索到的两种查找格式之间存在细微差别,但它们都没有像在Excel 2003中那样找到单元格。我不太清楚这里要做什么;任何人都可以指出我正确的方向让它像以前一样工作吗?

代码是这样的:

Cells.Find Code,2007

Dim rng As Range
    Application.FindFormat.Clear
    Application.FindFormat.NumberFormat = "General"
    With Application.FindFormat
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .MergeCells = True
    End With
    With Application.FindFormat.Font
        .Name = "Calibri"
        .FontStyle = "Regular"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = 2
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    With Application.FindFormat.Borders(xlLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 49
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Application.FindFormat.Borders(xlRight)
        .LineStyle = xlContinuous
        .ColorIndex = 49
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Application.FindFormat.Borders(xlTop)
        .ColorIndex = 49
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Application.FindFormat.Borders(xlBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 49
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Application.FindFormat.Borders(xlDiagonalDown).LineStyle = xlNone
    Application.FindFormat.Borders(xlDiagonalUp).LineStyle = xlNone
    With Application.FindFormat.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Application.FindFormat.Locked = True
    Application.FindFormat.FormulaHidden = False

    Set rng = Sheet2.Cells.Find(What:="", After:=Sheet2.Range("A6"), SearchDirection:=xlPrevious, SearchFormat:=True)
    If rng Is Nothing Then
        Debug.Print "Nothing"
    End If

Cells.Find Code,2003

Function find_last_picture_cell(Optional start_cell As String = "A6") As Range
    Dim r As Range
    Set r = Range(start_cell)
    Application.FindFormat.Clear
    Application.FindFormat.NumberFormat = "General"
    With Application.FindFormat
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .MergeCells = True
    End With
    With Application.FindFormat.Font
        .Name = "Calibri"
        .FontStyle = "Regular"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = 1
    End With
    With Application.FindFormat.Borders(xlLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = 49
    End With
    With Application.FindFormat.Borders(xlRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = 49
    End With
    With Application.FindFormat.Borders(xlTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = 49
    End With
    With Application.FindFormat.Borders(xlBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = 49
    End With

    Set find_last_picture_cell = Cells.Find(What:="", After:=r, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False _
        , SearchFormat:=True)
End Function

修改

好的,所以我想出了由于某种原因,"选择单元格格式化"选项太具体了。我经历过,只需手动选择一些我能记住的价值观。

我目前拥有的代码是,错误出现在函数的末尾,并显示Run Time Error '91': Object variable or With block variable not set.,并突出显示End Function行。

我已经检查过find_last_picture_cell是否填充了正确的单元格(M102),它是。但代码仍然给我一个错误,我不知道为什么。

Function find_last_picture_cell(Optional start_cell As String = "A6") As Range
    Dim r As Range
    Set r = Range(start_cell)
    Application.FindFormat.Clear
    With Application.FindFormat
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .MergeCells = True
    End With
    With Application.FindFormat.Font
        .Subscript = False
        .TintAndShade = 0
    End With
    With Application.FindFormat.Interior
        .PatternColorIndex = xlAutomatic
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Application.FindFormat.Locked = True

    Set find_last_picture_cell = Cells.Find(What:="", After:=r, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False _
        , SearchFormat:=True)
End Function

0 个答案:

没有答案