2016-01-20 301 views
0

我正在使用Access來循環訪問數據表,併爲數據庫表中的每一行生成Excel工作簿(包含三張表)。所有的作品都很好,直到我使用「Range」引入代碼來隱藏一些列和行。 代碼將在第一行成功運行,但會失敗。如果我們再次運行代碼,它也會失敗。如果我們退出Access並重新運行,則第一行再次成功。Access vba excel範圍

NewFileName = "C:\Paul2016Puzzle\TestNewName" + "Project" + Str(iteration) 
      'MsgBox NewFileName 

     Set XL = New Excel.Application 
     Set WB = XL.Workbooks.Open(NewFileName) 
     WB.Activate 

     Set wks = WB.Worksheets(2) 
     XL.ScreenUpdating = False 
     XL.DisplayAlerts = False 


     wks.Select 
     WB.Sheets(2).Activate 


     StrExcel = Chr(65 + WorkingColumns + 1) 

     StrExcel = StrExcel + ":" + StrExcel 
     MsgBox StrExcel 

      WB.Sheets("Sheet 2").Select 
      WB.Sheets("Sheet 2").Range(StrExcel).Select 
      WB.Sheets("Sheet 2").Activate 



       wks.Range(StrExcel).Activate 
       wks.Columns(StrExcel).Select 
       wks.Range(StrExcel).Select 
       ActiveSheet.Range(Selection, Selection.End(xlToRight)).Select 
       Selection.EntireColumn.Hidden = True 

       Rows("12:12").Select 
       ActiveSheet.Range(Selection, Selection.End(xlDown)).Select 
       Selection.EntireRow.Hidden = True 




     wks.Cells(1, 1).ColumnWidth = 30 '(Set column width) 
     For i = 2 To WorkingColumns + 1 
     wks.Cells(1, i).ColumnWidth = 15 
     Next i 

回答

0

與其嘗試創建列字母,只是參考列號。
Chr(65 + WorkingColumns + 1)將失敗 - 如果WorkingColumns是25,它將嘗試並參考列[

參考您的意見。我用這個方法來找到一個表的最後一個單元格:

' Purpose : Finds the last cell containing data or a formula within the given worksheet. 
'    If the Optional Col is passed it finds the last row for a specific column. 
'--------------------------------------------------------------------------------------- 

    Public Function LastCell(wrkSht As Worksheet, Optional Col As Long = 0) As Range 

     Dim lLastCol As Long, lLastRow As Long 

     On Error Resume Next 

     With wrkSht 
      If Col = 0 Then 
       lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column 
       lLastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row 
      Else 
       lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column 
       lLastRow = .Columns(Col).Find("*", , , , xlByColumns, xlPrevious).Row 
      End If 

      If lLastCol = 0 Then lLastCol = 1 
      If lLastRow = 0 Then lLastRow = 1 

      Set LastCell = wrkSht.Cells(lLastRow, lLastCol) 
     End With 
     On Error GoTo 0 

    End Function 

然後,您可以用它來找到最後行/列包含數據和隱藏之後,一切:

Public Sub Main() 

    Dim WB As Workbook 
    Dim wks As Worksheet 
    'Dim WorkingColumns As Long 
    'Dim FirstRow As Long, LastRow As Long 
    'Dim FirstCol As Long, LastCol As Long 

    Set WB = ThisWorkbook 
    Set wks = WB.Worksheets(2) 

    'Not sure how you get the WorkingColumns figure, 
    'so have set it to column 5 (column E). 
    'WorkingColumns = 5 

    'FirstCol = 2 
    'LastCol = 8 

    'FirstRow = 4 
    'LastRow = 10 

    'Find the last cell containing data. 
    Dim rLastCell As Range 
    Set rLastCell = LastCell(wks) 

    With wks 

     'This Offsets by 1 column, so looks at the column after the end of data. 
     .Range(rLastCell.Offset(, 1), .Cells(1, Columns.Count)).EntireColumn.Hidden = True 
     .Range(.Cells(13, 1), .Cells(Rows.Count, 1)).EntireRow.Hidden = True 

     '''''''''''''''''''''''''Second Update''''''''''''''''''''''' 
     'A range is written as Range(FirstCellRef, LastCellRef). 
     'Cells references a single cell using row and column numbers (or letters). 
     'You can use either .Cells(3, 1) or .Cells(3,"A") to reference cell A3. 
     '.Range(.Cells(1, FirstCol), .Cells(1, LastCol)).EntireColumn.Hidden = True 
     '.Range(.Cells(FirstRow, 1), .Cells(LastRow, 1)).EntireRow.Hidden = True 

     'Set width of columns I:L 
     '.Range(.Cells(1, 9), .Cells(1, 12)).ColumnWidth = 30 

     'Set width of column N & P (column O is ignored). 
     'Union(.Cells(1, 14), .Cells(1, 16)).ColumnWidth = 2 
     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 

     '''''''''''''''''''''''''Original Code''''''''''''''''''''''' 
     'Resize the number of columns to 8 wide, including column E. 
     'So E:L. 
     ' .Columns(WorkingColumns).Resize(, 8).Hidden = True 
     ' .Rows(12).Hidden = True 

     ' .Columns(1).ColumnWidth = 30 

     'Resize Column 2 reference by +4. 
     'So B:E 
     ' .Columns(2).Resize(, WorkingColumns - 1).ColumnWidth = 15 
     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
    End With 

End Sub 

編輯:已經更新了代碼,以使用第一/最後一列和行號而不是Resize方法引用列。

+0

我把這段代碼放到了我的程序中,它完美地工作。列數將從一個電子表格到另一個不同,我想隱藏所有列到右側。行數是不變的,我想隱藏第12行下面的所有行。換句話說,只有電子表格的左上角會顯示。謝謝。謝謝。 – RyszardJ

+0

我已經更新了代碼,以顯示如何隱藏給定第一行和最後一行/列引用的行/列。您可以使用'.Cells(1,1).End(xlToRight)'或任何適合您的方法來獲取所需的行/列號。 '.Range(.Cells(1,1),.Cells(1,1).End(xlToRight))。EntireColumn.Hidden = True'也應該有效。 –

+0

謝謝你的幫助。它幾乎可以工作,但我不得不爲最後一列和最後一行輸入常量。但至少我到了那裏。最後一件事 - 你可以推薦好的Access手冊,因爲我只是簡單地涉獵和直觀地工作。感謝RWJ – RyszardJ