繁体   English   中英

VBA代码在Excel 2013中工作正常,但在Excel 2016中给出错误-编译错误

[英]VBA code works fine in Excel 2013 but gives error in excel 2016 - Compile error

在寻找解决方案很多之后,我正在与所有人联系。 我有一个可以在Excel 2013中正常工作的代码,但是当我尝试在excel 2016中运行相同的代码时,它给了我编译错误。

这段代码是由其他人开发的,我只是对其进行了少量修改。 下面是代码。 由于Environ $变量而发生错误。 因为那是当我按给定屏幕快照中的“确定”按钮时突出显示的内容。

Function RangetoHTML(rng As Range)
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook

TempFile = Environ("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
    .Cells(1).PasteSpecial Paste:=8
    .Cells(1).PasteSpecial xlPasteValues, , False, False
    .Cells(1).PasteSpecial xlPasteFormats, , False, False
    .Cells(1).Select
    Application.CutCopyMode = False
    On Error Resume Next
    .DrawingObjects.Visible = True
    .DrawingObjects.Delete
    On Error GoTo 0
End With

'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
     SourceType:=xlSourceRange, _
     Filename:=TempFile, _
     Sheet:=TempWB.Sheets(1).Name, _
     Source:=TempWB.Sheets(1).UsedRange.Address, _
     HtmlType:=xlHtmlStatic)
    .Publish (True)
End With

'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.ReadAll
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                      "align=left x:publishsource=")

'Close TempWB
TempWB.Close savechanges:=False

'Delete the htm file we used in this function
Kill TempFile

Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function

我还附有错误屏幕截图。

Environ $变量编译错误

Environ $变量编译错误

任何快速帮助将不胜枚举。

刚从Ron de Bruin rangetohtml(rng as range)得到了相同的错误。

我解决了

如果您在“出现错误时继续执行下一步”之前返回到代码,并注释掉。

运行,您将看到它找不到drawingobjects.visible

修理。

转到file/options/formulas/

确保它不是R1C1-取消选中此框。

此后,我的工作还不错。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM