2014-10-08 76 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