Excel VBA - 隐藏工作表加载中的行

时间:2014-10-08 21:12:02

标签: excel-vba vba excel

为了清楚起见,我已经删除了原来的问题并重新发布。

情景:

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

要求:

阶段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列
  • 在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行,

试图将它声明为一个对象,认为这样可以让我以相反的方式对其进行规范,但不会有任何乐趣。

与以往一样,社区的任何指导都将受到高度赞赏。

非常感谢。

罗布。

1 个答案:

答案 0 :(得分:0)

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