工作簿转换为HTML后,如何使隐藏的列保持隐藏状态?

时间:2019-06-25 20:31:13

标签: excel vba

我正在编写一个脚本,该脚本通过excel工作簿,隐藏某些行和列,然后将工作簿发布为HTML。由于某种原因,一旦转换,隐藏的行将保持隐藏状态,但隐藏的列则不会。它们隐藏在工作簿的excel版本中,但是一旦转换为HTML,它们实际上就会扩展并破坏其他所有格式。

我已经尝试过使用SaveAs命令(另存为xlHtml)以及publish命令。两者都成功地将文档转换为HTML,并隐藏了行,但是都没有隐藏列。

'Sets which sheets to search through and update
sheetList = Array("CH01", "CH02", "CH03", "CH04", "CH05", "CH06", "CH07", 
"CH08", "CH09")

For sheetNum = LBound(sheetList) To UBound(sheetList)
    'sets sh to each sheet in sheetList
    Set sh = Sheets(sheetList(sheetNum))

    'Hides first four rows of each sheet in sheetList
    sh.Columns("A:D").Hidden = True
Next

'Save Method one
ActiveWorkbook.SaveAs _
Filename:=ActiveWorkbook.Path & "\test.html", _
FileFormat:=xlHtml  

'Save Method Two
With ActiveWorkbook
    With .PublishObjects(1)
    .Filename = "publishtest.htm"
    .Publish
    End With
End With

转换过程通常要花费大约两到四分钟的时间,并且输出文件中隐藏了所有预期的行,我用相同的方法将它们隐藏在另一段代码中。

一旦我运行了上面的代码,我将始终检查工作簿的excel版本,并且它隐藏了正确的行和列,但是转换后的HTML版本仅隐藏了正确的行。有任何想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

使用@PeterT的建议,并将文件移动到本地文件夹,我弄清楚了。我忘记了我使用的是远程驱动器,并且在vba转换为html之前,下载过程必须花很长时间才能对vba进行编辑。有趣的是,隐藏列仍然无效,我不得不删除它们。这是我的代码

'Sets which sheets to search through and update
sheetList = Array("CH01", "CH02", "CH03", "CH04", "CH05", "CH06", "CH07", "CH08", "CH09")

'Saves and opens new workbook to process and convert to html. 
ActiveWorkbook.SaveCopyAs _
Filename:=ActiveWorkbook.Path & "\" & test & ".xlsm"
Workbooks.Open (ActiveWorkbook.Path & "\" & test & ".xlsm")

For sheetNum = LBound(sheetList) To UBound(sheetList)
    'sets sh to each sheet in sheetList
    Set sh = ActiveWorkbook.Sheets(sheetList(sheetNum))

    'Deletes first four rows of each sheet in sheetList (only way to hide columns once converted to html)
    sh.Columns(1).EntireColumn.Delete
    sh.Columns(1).EntireColumn.Delete
    sh.Columns(1).EntireColumn.Delete
    sh.Columns(1).EntireColumn.Delete
Next