繁体   English   中英

Excel VBA-在工作表加载时隐藏行

[英]Excel VBA - hide rows on sheet load

为了清楚起见,我删除了原始问题,然后重新发布。

场景:

源工作簿有多个页面,书的首页具有查询/提取功能,可以使用源书中一个工作表中的模板使用一些预先输入的数据来创建新书。

要求:

阶段1:提取功能需要将第6行以外的所有行设置为隐藏,其中A列中的数据= HC。

该代码的初稿(到目前为止有效)如下:

Sub Extract()

    Dim wbkOriginal As Workbook
    Set wbkOriginal = ActiveWorkbook

    'sets site name and site ID into the estate page to be extracted
    Worksheets(Sheet11.CmbSheet.Value).Range("B3").Value = Worksheets("front page").Range("E6")
    Worksheets(Sheet11.CmbSheet.Value).Range("D3").Value = Worksheets("front page").Range("N6")
    Worksheets(Sheet11.CmbSheet.Value).Range("F3").Value = Worksheets("front page").Range("K6")

    'hiding all rows that being with HC apart from row 6 which is the starting row
    'code to be added to the individual estate sheets to unhide each row after status column filled
    'on a row by row basis - as the hiding is for HC rows only, the section headers will remain visible
    'may have to code around that on the sheet itself
    BeginRow = 7
    EndRow = 300
    ChkCol = 1

    For RowCnt = BeginRow To EndRow
        If Worksheets(Sheet11.CmbSheet.Value).Cells(RowCnt, ChkCol).Value Like "HC" Then
            Worksheets(Sheet11.CmbSheet.Value).Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        End If
    Next RowCnt

    ' copies sheet name from combo box into new document, saves it with site name,
    ' site id and current date into user profile desktop folder for ease of access
    ' with new HEAT, worth investigating if sheet can be saved directly to a call ID folder?
        With ActiveWorkbook.Sheets(Sheet11.CmbSheet.Value)
        .Copy
                ActiveWorkbook.SaveAs _
                "C:\temp\" _
                & .Cells(3, 2).Text _
                & " " _
                & Format(Now(), "DD-MM-YY") _
                & ".xlsm", _
                xlOpenXMLWorkbookMacroEnabled, , , , False
        End With

    'code to close the original workbook to prevent accidental changes etc
    Application.DisplayAlerts = False
    wbkOriginal.Close
    Application.DisplayAlerts = True
    End Sub

阶段2:以HC开头的每一行在E列中都有一个下拉菜单。该下拉菜单包含3个选项:“完成”,“不完整”和“不需要”

任务:当用户选择并单击一个条目时,工作表需要执行以下操作

  • 取消隐藏下一行
  • 在第I栏中输入当前的Windows用户名
  • 在J栏中输入当前时间

原型代码:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim ChangedCell As Object
Dim lRow As Long

       For Each ChangedCell In Target
       If ChangedCell.Column = 5 And ChangedCell <> "" Then
            lRow = ChangedCell.Row + 1
                lRow.Hidden = False
                Cells(lRow, 8) = Environ("USERNAME")
                Cells(lRow, 9) = "HH:MM"
            End If
       Next
End Sub

问题:

编译错误:无效的限定符,引用lRow.Hidden = False行,

试图将其声明为一个对象,以为可以让我以这种方式来指定它,但没有乐趣。

与以往一样,社区的任何指导将不胜感激。

非常感谢。

抢。

Sub Extract()

    Dim wbkOriginal As Workbook
    Set wbkOriginal = ActiveWorkbook

    'sets site name and site ID into the estate page to be extracted
    Worksheets(Sheet11.CmbSheet.Value).Range("B3").Value = Worksheets("front page").Range("E6")
    Worksheets(Sheet11.CmbSheet.Value).Range("D3").Value = Worksheets("front page").Range("N6")
    Worksheets(Sheet11.CmbSheet.Value).Range("F3").Value = Worksheets("front page").Range("K6")

    'hiding all rows that being with HC apart from row 6 which is the starting row
    'code to be added to the individual estate sheets to unhide each row after status column filled
    'on a row by row basis - as the hiding is for HC rows only, the section headers will remain visible
    'may have to code around that on the sheet itself
    BeginRow = 7
    EndRow = 300
    ChkCol = 1

    For RowCnt = BeginRow To EndRow
        If Worksheets(Sheet11.CmbSheet.Value).Cells(RowCnt, ChkCol).Value <> "" Then
            Worksheets(Sheet11.CmbSheet.Value).Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        End If
    Next RowCnt

    ' copies sheet name from combo box into new document, saves it with site name,
    ' site id and current date into user profile desktop folder for ease of access
    ' with new HEAT, worth investigating if sheet can be saved directly to a call ID folder?
        With ActiveWorkbook.Sheets(Sheet11.CmbSheet.Value)
        .Copy
                ActiveWorkbook.SaveAs _
                "C:\temp\" _
                & .Cells(3, 2).Text _
                & " " _
                & Format(Now(), "DD-MM-YY") _
                & ".xlsm", _
                xlOpenXMLWorkbookMacroEnabled, , , , False
        End With

    'code to close the original workbook to prevent accidental changes etc
    Application.DisplayAlerts = False
    wbkOriginal.Close
    Application.DisplayAlerts = True
    End Sub

暂无
暂无

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

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