如何冻结输出工作表的窗格

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何冻结输出工作表的窗格相关的知识,希望对你有一定的参考价值。

我目前有一些用Access编写的VBA代码来输出Excel文件。目前,没有为电子表格指定格式,只有使用我创建的查询提取的原始数据。我的问题是,如何冻结输出电子表格的顶行?请参阅下面的代码。

    Option Compare Database
    Public TimeStamp As String
    Public TimeStamp2 As String

    Function DailyMTDMail()

    If Weekday(Date) = 7 Or Weekday(Date) = 1 Then
    'do nothing
    Else

    TimeStamp = Month(Date) & "." & Day(Date) & "." & Year(Date)


    DoCmd.OutputTo acOutputQuery, "001 Extract Sales in Period", 
    acFormatXLSX, "\xxxxxxxxxMTD Sales @ " & TimeStamp & ".xlsx", False

    Dim filename As String

    filename = "\xxxxxxxxxMTD Sales @ " & TimeStamp & ".xlsx"

    Dim xl As Excel.Application
    Dim wb As Excel.Workbook
    Dim ws As Excel.Worksheet
    Set xl = CreateObject("Excel.Application")
    Set wb = xl.Workbooks.Open(filename)
    Set ws = wb.Sheets("001 Extract Sales in Period")         ' change to 
    the name of your sheet

    wb.Application.ActiveWindow.FreezePanes = False
    ws.Range("a2").Select                ' change to the range you want to 
    freeze

    wb.Application.ActiveWindow.FreezePanes = True
    wb.Save
    Set objMessage = CreateObject("CDO.Message")

    objMessage.Subject = " Inc - MTD Sales @ " & TimeStamp

    objMessage.From = "email@email.com"

    'objMessage.To = "email@email.com"
    objMessage.To = "email@email.com"    'test


    objMessage.Textbody = "Please find attached MTD sales @ " & TimeStamp & 
    vbCr & vbCr & "Regards" & vbCr & vbCr & "Name"

    objMessage.AddAttachment filename

    'This section provides the configuration information for the remote SMTP 
    server.
    'Normally you will only change the server name or IP.
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

    'Name or IP of Remote SMTP Server
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = 
    "word"

    'Server port (typically 25)
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

    objMessage.Configuration.Fields.Update

    'End remote SMTP server configuration section==

    objMessage.Send



    End If
答案

在docmd.output之后你必须打开excelfile,冻结你想要的单元格,保存并关闭文件,例如:

Function FreezeMe(strfile As String)

Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Set xl = CreateObject("Excel.Application")
Set wb = xl.Workbooks.Open(strfile)
Set ws = wb.Sheets("sheet1")         ' change to the name of your sheet

wb.Application.ActiveWindow.FreezePanes = False
ws.Range("a2").Select                ' change to the range you want to freeze 

wb.Application.ActiveWindow.FreezePanes = True
wb.Save
wb.Close


End Function

以上是关于如何冻结输出工作表的窗格的主要内容,如果未能解决你的问题,请参考以下文章

表格怎么冻结前两行

冻结 HTML 表格中的窗格

如何在 HTML 表格上复制“冻结窗格”功能?

怎么让excel表格前三行每页都显 显示 意思就是让前三行 在用表的时候一直显示,只有下面的格再动,详细点

如何在Excel数据中间插入数据而不覆盖原有数据?

excel如何冻结首行?