Sub正在影响错误的Excel工作簿

时间:2016-03-16 13:57:45

标签: excel vba excel-vba access-vba

我编写了这个VBA代码,用于从Access表中的数据生成报告,并使用用户友好格式将其转储到Excel中。

该代码第一次运行良好。但是,如果我在第一个生成的Excel工作表打开时再次运行代码,我的一个子例程会影响第一个工作簿而不是新生成的工作簿。

为什么呢?我怎样才能解决这个问题?

我认为问题在于我将工作表和记录集传递给打印列的名为GetHeaders的子例程,但我不确定。

Sub testROWReport()

DoCmd.Hourglass True

'local declarations
Dim strSQL As String
Dim rs1 As Recordset
'excel assests
Dim xlapp As excel.Application
Dim wb1 As Workbook
Dim ws1 As Worksheet
Dim tempWS As Worksheet
'report workbook dimentions
Dim intColumnCounter As Integer
Dim lngRowCounter As Long

'initialize SQL container
strSQL = ""
'BEGIN: construct SQL statement {
--this is a bunch of code that makes the SQL Statement
'END: SQL construction}

'Debug.Print (strSQL) '***DEBUG***
Set rs1 = CurrentDb.OpenRecordset(strSQL)

'BEGIN: excel export {
    Set xlapp = CreateObject("Excel.Application")
    xlapp.Visible = False
    xlapp.ScreenUpdating = False
    xlapp.DisplayAlerts = False

    'xlapp.Visible = True '***DEBUG***
    'xlapp.ScreenUpdating = True '***DEBUG***
    'xlapp.DisplayAlerts = True '***DEBUG***

    Set wb1 = xlapp.Workbooks.Add
    wb1.Activate
    Set ws1 = wb1.Sheets(1)
    xlapp.Calculation = xlCalculationManual
    'xlapp.Calculation = xlCalculationAutomatic '***DEBUG***

    'BEGIN: Construct Report
    ws1.Cells.Borders.Color = vbWhite
    Call GetHeaders(ws1, rs1) 'Pastes and formats headers
    ws1.Range("A2").CopyFromRecordset rs1 'Inserts query data
    Call FreezePaneFormatting(xlapp, ws1, 1) 'autofit formatting, freezing 1 row,0 columns
    ws1.Name = "ROW Extract"
        'Special Formating
        'Add borders
        'Header background to LaSenza Pink
        'Fix Comment column width
        'Wrap Comment text

        'grey out blank columns
    'END: Report Construction

    'release assets
    xlapp.ScreenUpdating = True
    xlapp.DisplayAlerts = True
    xlapp.Calculation = xlCalculationAutomatic
    xlapp.Visible = True

    Set wb1 = Nothing
    Set ws1 = Nothing
    Set xlapp = Nothing
    DoCmd.Hourglass False
'END: excel export}

 End Sub

    Sub GetHeaders(ws As Worksheet, rs As Recordset, Optional startCell As Range)

    ws.Activate 'this is to ensure selection can occur w/o error

    If startCell Is Nothing Then
    Set startCell = ws.Range("A1")
    End If

    'Paste column headers into columns starting at the startCell
    For i = 0 To rs.Fields.Count - 1
    startCell.Offset(0, i).Select
    Selection.Value = rs.Fields(i).Name
    Next

    'Format Bold Text
    ws.Range(startCell, startCell.Offset(0, rs.Fields.Count)).Font.Bold = True

End Sub
Sub FreezePaneFormatting(xlapp As excel.Application, ws As Worksheet, Optional lngRowFreeze As Long = 0, Optional lngColumnFreeze As Long = 0)


  Cells.WrapText = False
  Columns.AutoFit

    ws.Activate
    With xlapp.ActiveWindow
        .SplitColumn = lngColumnFreeze
        .SplitRow = lngRowFreeze
    End With
    xlapp.ActiveWindow.FreezePanes = True

End Sub

2 个答案:

答案 0 :(得分:4)

单独使用单元格和列时,它们引用ActiveSheet.Cells和ActiveSheet.Columns。 尝试使用目标表单作为前缀:

Sub FreezePaneFormatting(xlapp As Excel.Application, ws As Worksheet, Optional lngRowFreeze As Long = 0, Optional lngColumnFreeze As Long = 0)

    ws.Cells.WrapText = False
    ws.Columns.AutoFit
    ...

End Sub

答案 1 :(得分:2)

好的,我在这里找出了问题。我想我不能使用"。选择"或"选择。"当我使用不可见的非更新工作簿时。我发现当我将一些代码从自动选择更改为简单地直接更改单元格的值时,就可以解决这个问题。

OLD:

    startCell.Offset(0, i).Select
    Selection.Value = rs.Fields(i).Name

NEW:

    ws.Cells(startCell.Row, startCell.Column).Offset(0, i).Value = rs.Fields(i).Name