2014-10-08 55 views
0

为了清晰起见,我已经删除了原始问题并重新发布。Excel VBA - 在工作表加载时隐藏行

场景:

源工作簿包含多页,书的封面有一个查询/取出功能使用从源书的纸张的一个模板一些预先输入的数据创建一个新的书。

要求:

阶段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个选项, “完整”“不完全”和“非必需”

任务:当一个条目的用户选择和点击,片材需要做以下

  • 取消隐藏的下一行
  • 输入当前的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 =虚线,

试图声明它为一个对象,而不是,认为w我应该允许我以这种方式来规划它,但是没有快乐。

一如既往,任何来自社区的指导将不胜感激。

非常感谢。

Rob。

+0

为了让你开始,你不需要遍历每一行来检查列A中的“HC”。只需使用自动筛选并一次性隐藏行。其次,对于Col E,使用'Worksheet_Change'事件。试一试,如果你仍然坚持,然后张贴你试过的代码,然后我们会从那里拿它 – 2014-10-08 21:21:47

+0

自动过滤器会很好,如果我相信用户手动做,但我没有,这就是在那里获得一些代码的原因 - 有一个进程管理问题正在进行,因此需要强制用户一次执行一个步骤,并在完成时确认每个步骤。 – 2014-10-09 06:58:45

+0

我的意思是自动过滤器的代码... – 2014-10-09 08:03:53

回答

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