子程序中的功能未完全执行
Function is not fully executed within Sub Routine
我正在尝试使用 html 发布程序(在函数内)附加电子邮件正文(Excel 范围)。这工作正常,只要我不尝试根据创建的 html 文件格式化基础 Sheet。
“###”标记代码中的重要行。
在此代码片段中,函数从底层子程序启动 (RangetoHTNL(rng))。
With OutMail
.To = rec
.cc = cc
.BCC = ""
.Subject = "Convertibles - Execution " & cover.Range("D4").Value
.HTMLBody = StrBody & RangetoHTML(rng) '#### Here the function get launched #####
.Attachments.Add (filename)
.Display 'or use .Send
End With
现在奇怪的是,在我想更改列宽之前,该函数可以正常工作。在此行之后,算法 "jumps" 退出函数并执行底层子程序,而无需先完成整个函数。
在函数的代码片段下方找到,我在 Excel Sheet 中进行了一些格式化,之后应该发布为 HTML。
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
row = .Range("A" & Rows.Count).End(xlUp).row
lastcolumn = .Cells(6, Columns.Count).End(xlToLeft).Column
default = .Range(.Cells(6, 1), .Cells(6, lastcolumn)).Width '-> Default width of whole table Range("A:?")
ReDim myArray(row)
For i = 1 To row
myCell = .Cells(i, 1).text
mysize = getLabelPixel(myCell)
myArray(x) = mysize
Next i
Max = WorksheetFunction.Max(myArray)
max_width = Max / con
default_width = default / con
If max_width > default_width Then
prop = max_width / lastcolumn
'####### until here the function is executed. Then it jumps out #######
.Columns("1:" & lastcolumn).Width = prop
.Columns("2:" & lastcolumn).Columns.AutoFit
'###### without these two lines the function is executed properly #####
End If
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
有没有人经历过这样的事情?我已经尝试过其他版本的编码(例如,不使用 "With" - 语句方法) - 没有任何帮助。
我的猜测是您将 on error resume next
放置在调用该函数的子例程中,然后在调整列大小时发生错误(如果其中一个变量无效,可能会溢出)。
确保将 on error goto 0
放在函数调用之前,然后调试函数并检查变量是否有效。
/编辑:
既然我们有一个错误,让我们看看它。我知道错误发生在这里:
.Columns("1:" & lastcolumn).Width = prop
看起来不对,试试这个(确保 lastcolumn
和 prop
有效):
.Cells(1, lastcolumn).ColumnWidth = prop
我正在尝试使用 html 发布程序(在函数内)附加电子邮件正文(Excel 范围)。这工作正常,只要我不尝试根据创建的 html 文件格式化基础 Sheet。
“###”标记代码中的重要行。
在此代码片段中,函数从底层子程序启动 (RangetoHTNL(rng))。
With OutMail
.To = rec
.cc = cc
.BCC = ""
.Subject = "Convertibles - Execution " & cover.Range("D4").Value
.HTMLBody = StrBody & RangetoHTML(rng) '#### Here the function get launched #####
.Attachments.Add (filename)
.Display 'or use .Send
End With
现在奇怪的是,在我想更改列宽之前,该函数可以正常工作。在此行之后,算法 "jumps" 退出函数并执行底层子程序,而无需先完成整个函数。
在函数的代码片段下方找到,我在 Excel Sheet 中进行了一些格式化,之后应该发布为 HTML。
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
row = .Range("A" & Rows.Count).End(xlUp).row
lastcolumn = .Cells(6, Columns.Count).End(xlToLeft).Column
default = .Range(.Cells(6, 1), .Cells(6, lastcolumn)).Width '-> Default width of whole table Range("A:?")
ReDim myArray(row)
For i = 1 To row
myCell = .Cells(i, 1).text
mysize = getLabelPixel(myCell)
myArray(x) = mysize
Next i
Max = WorksheetFunction.Max(myArray)
max_width = Max / con
default_width = default / con
If max_width > default_width Then
prop = max_width / lastcolumn
'####### until here the function is executed. Then it jumps out #######
.Columns("1:" & lastcolumn).Width = prop
.Columns("2:" & lastcolumn).Columns.AutoFit
'###### without these two lines the function is executed properly #####
End If
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
有没有人经历过这样的事情?我已经尝试过其他版本的编码(例如,不使用 "With" - 语句方法) - 没有任何帮助。
我的猜测是您将 on error resume next
放置在调用该函数的子例程中,然后在调整列大小时发生错误(如果其中一个变量无效,可能会溢出)。
确保将 on error goto 0
放在函数调用之前,然后调试函数并检查变量是否有效。
/编辑: 既然我们有一个错误,让我们看看它。我知道错误发生在这里:
.Columns("1:" & lastcolumn).Width = prop
看起来不对,试试这个(确保 lastcolumn
和 prop
有效):
.Cells(1, lastcolumn).ColumnWidth = prop