Macro From Excel 2003 Doesn't work in Excel 2007

不羁的心 提交于 2019-12-25 16:40:05

问题


Recently I've upgraded from Excel 2003 to Excel 2007. Nearly all of the macros work, except for one part of one macro. On this sheet for this file, there are roughly 21 slots that have been sized to have pictures placed in them. Because of the nature of the work, sometimes there are a lot more than 21 pictures to input into the document. Before hand it was just a hassle because you would sometimes forget to copy rows over, and then couldn't resize the images properly.

So, upon inserting the images into the photo sheet and running the macro, if there are 21 or less photos it will simply place all of the photos into the slots and resize them. More or less, this works fine, there's a few things I have to tweak, but generally it's working.

The problem is the case for when there are > 21 photos inserted. The code was to find the last available picture cell and copy and paste the needed rows after it. Excel 2007 is not finding any of those cells. The formatting I copied from a recorded macro, which explains the odd styling choices.

The picture cells look like this:

I figured that perhaps something about how the styles of that box had been changed between 2003 and 2007, so I decided to record another macro to get the "new" formatting. But even with Excel's Find dialog and selecting one of the photo cells for its formatting, it gives me an error of "Excel cannot find the data you are looking for." As expected, there were subtle differences between the two Find Formats retrieved by the macro recorder, but neither of them find the cells like they did in Excel 2003. I'm not particularly sure what to do here; can anyone point me in the right direction of getting this to work like it did previously?

The code is this:

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

EDIT

Okay, so I figured out that for some reason, the "choose cell formatting" options was too specific. I went through and just manually chose some of the options who's values I could remember.

The code I currently have is, the error comes at the end of the function, and says Run Time Error '91': Object variable or With block variable not set., and highlights the End Function line.

I have checked to see that find_last_picture_cell is being populated with the correct cell (M102), and it is. But the code still gives me an error and I'm not sure why.

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

来源:https://stackoverflow.com/questions/26068529/macro-from-excel-2003-doesnt-work-in-excel-2007

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!