【问题标题】:Hide excel columns doesn't work in outlook vba隐藏excel列在outlook vba中不起作用
【发布时间】:2017-08-10 07:00:38
【问题描述】:

我根本找不到在 Outlook vba 中隐藏特定列的方法。我什么都试过了。我当前的代码如下:

Sub ExportToExcel()
    Dim xlApp As Object
    Dim xlWB As Object
    Dim xlSheet As Object
    Dim enviro As String
    Dim strPath As String
    Dim ns As NameSpace
    Dim item As Object
    Dim inbox As MAPIFolder
    Dim i As Long
    Dim j As Long

    ' Get Excel set up
enviro = CStr(Environ("USERPROFILE"))
'the path of the workbook
 strPath = enviro & "\Documents\test.xlsx"
     On Error Resume Next
     Set xlApp = GetObject(, "Excel.Application")
     If Err <> 0 Then
         Application.StatusBar = "Please wait while Excel source is opened ... "
         Set xlApp = CreateObject("Excel.Application")
         bXStarted = True
     End If
     On Error GoTo 0
     'Open the workbook to input the data
     Set xlWB = xlApp.Workbooks.Open(strPath)
     Set xlSheet = xlWB.Sheets("Sheet1")
    ' Process the message record
    `On Error Resume Next
    For j = 2 To 367
        If xlSheet.cells(1, j).Value <> Date And xlSheet.cells(1, j).Interior.ColorIndex = 4 Then
            xlSheet.Columns(j).Interior.ColorIndex = 0
        End If
        If xlSheet.cells(1, j).Value = Date Then
            xlSheet.Columns(j).Interior.ColorIndex = 4
            For i = 2 To j - 1
                xlSheet.Columns(i).EntireColumn.Hidden = True
                Debug.Print xlSheet.cells(1, i).Value
            Next i
        Exit For
        End If
    Next j
     xlWB.Worksheets("Sheet1").Columns("A:NB").EntireColumn.AutoFit
     xlWB.Close 1
     If bXStarted Then
         xlApp.Quit
     End If
End Sub

第一行,从我的 Excel 表的第二列开始,填充了从 01.01.2017 到 31.12.2017 的日期。

我希望宏隐藏当天之前的所有日期。 如您所见,隐藏测试中的 debug.print 按预期工作,并打印从 01.01.2017 到当前 date-1 的所有日期。

附带说明,xlSheet.Columns(i).Color = 5287936 也不起作用。

通过在 cmets 中的 for 之前放置 On Error Resume Next,我收到“应用程序定义的或对象定义的错误”错误。

如果我删除所有错误测试,我会收到“ActiveX 组件无法创建对象”错误。

新发现,如果我 debug.print xlsheet.columns(i).hidden 我在即时窗口中收到“真实”消息。显然代码完全按照它应该做的,但它只是没有生效。

【问题讨论】:

  • 评论不适用于扩展讨论或调试会话。 不要在 cmets 中发布代码。当有人要求您提供代码或说明时,您需要在您的问题中 edit 它。之前的对话是moved to chat
  • 你在哪个办公室工作?
  • 我用的是office 2010。

标签: excel vba outlook


【解决方案1】:

手动尝试自动调整。您应该会发现它取消隐藏隐藏的列。

将自动调整移动到隐藏列之前或更精确地了解要自动调整的列。

Set xlWB = xlApp.Workbooks.Open(strPath)
Set xlSheet = xlWB.Sheets("Sheet1")

xlWB.Worksheets("Sheet1").Columns("A:NB").EntireColumn.AutoFit

' Process the message record
For j = 2 To 367
    If xlSheet.Cells(1, j).Value <> Date And xlSheet.Cells(1, j).Interior.ColorIndex = 4 Then
        xlSheet.Columns(j).Interior.ColorIndex = 0
    End If

    If xlSheet.Cells(1, j).Value <> Date And xlSheet.Cells(1, j).Interior.ColorIndex <> 0 Then
        xlSheet.Columns(j).Interior.ColorIndex = 4
    End If

    If xlSheet.Cells(1, j).Value = Date Then
        xlSheet.Columns(j).Interior.ColorIndex = 4
        For i = 2 To j - 1
            xlSheet.Columns(i).EntireColumn.Hidden = True
            Debug.Print xlSheet.Cells(1, i).Value
        Next i
        Exit For
    End If

Next j

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多