访问vba excel范围

时间:2016-01-20 10:46:04

标签: excel ms-access

我使用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

1 个答案:

答案 0 :(得分:1)

不要尝试创建列字母,只需引用列号 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方法更新代码以引用列。