Vba Excel 2003中的宏在Excel 2007中不起作用
最近我已经从Excel2003升级到Excel2007。除了一个宏的一部分外,几乎所有宏都可以工作。在该文件的这张纸上,大约有21个插槽的大小已调整为可以放置图片。由于工作的性质,有时需要输入超过21张图片到文档中。在此之前,这只是一个麻烦,因为您有时会忘记复制行,然后无法正确调整图像的大小 因此,在将图像插入照片表并运行宏时,如果有21张或更少的照片,它将简单地将所有照片放入插槽并调整大小。或多或少,这是很好的,有一些事情我必须调整,但通常它的工作 当插入的照片超过21张时,问题就出现了。代码是找到最后一个可用的图片单元格,然后复制并粘贴所需的行。Excel 2007找不到任何这些单元格。我从录制的宏复制的格式,这解释了奇怪的样式选择 图片单元格如下所示: 我想这可能是关于2003年到2007年间这个盒子的样式是如何改变的,所以我决定录制另一个宏来获得新的格式。但是,即使使用Excel的“查找”对话框并选择其中一个照片单元格进行格式化,也会给我一个错误,即Excel无法找到您要查找的数据。正如所料,宏记录器检索到的两种查找格式之间存在细微差异,但它们都没有像在Excel2003中那样查找单元格。我不太确定在这里该做什么;有没有人能给我指出正确的方向,让它像以前一样工作 代码如下: 单元格。查找代码,2007 细胞。查找代码,2003年 编辑 好吧,我发现出于某种原因,选择单元格格式选项太具体了。我浏览了一遍,只是手动选择了一些我能记住的选项 我当前的代码是,错误出现在函数的末尾,并显示运行时错误“91”:未设置对象变量或With block变量,并突出显示end函数行 我已经检查了find_last_picture_单元格是否填充了正确的单元格M102,并且是。但是代码仍然给了我一个错误,我不知道为什么Vba Excel 2003中的宏在Excel 2007中不起作用,vba,excel,excel-2007,Vba,Excel,Excel 2007,最近我已经从Excel2003升级到Excel2007。除了一个宏的一部分外,几乎所有宏都可以工作。在该文件的这张纸上,大约有21个插槽的大小已调整为可以放置图片。由于工作的性质,有时需要输入超过21张图片到文档中。在此之前,这只是一个麻烦,因为您有时会忘记复制行,然后无法正确调整图像的大小 因此,在将图像插入照片表并运行宏时,如果有21张或更少的照片,它将简单地将所有照片放入插槽并调整大小。或多或少,这是很好的,有一些事情我必须调整,但通常它的工作 当插入的照片超过21张时,问题就出现了。代码
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
哪一行给出了错误?设置find_last_picture_cell=…?啊,忘了说,是的,这是给我错误的那一行。通过查看我用宏创建的其他一些文件,似乎所有对单元格的调用。find都给出了某种错误。对文本进行了编辑,@davidzemensw这个函数调用了什么?函数是返回范围。尝试设置find\u last\u picture\u cell=一个虚拟单元格,以查看函数是否工作并确保单元格。find是导致错误的原因。
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
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