簡體   English   中英

訪問VBA Excel Range

[英]Access vba excel Range

我正在使用Access遍歷數據表並為數據庫表中的每一行生成一個Excel工作簿(包含三張紙)。 在我使用“ Range”介紹隱藏某些列和行的代碼之前,所有方法都運行良好。 該代碼將在第一行上成功運行,但隨后將失敗。 如果再次運行代碼,它也會失敗。 如果我們退出Access,然后重新運行,則第一行將再次成功。

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

與其嘗試創建列字母,不如參考列號。
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方法來引用列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM