+ Reply to Thread
Results 1 to 9 of 9

Screen Updating Not Completely Disabled in Excel Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    01-07-2012
    Location
    Pleasanton, CA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Screen Updating Not Completely Disabled in Excel Macro

    Hi - I have an Excel macro that pulls data from Access queries, populating several different worksheets in the Excel file. I've disabled screen updating in the VBA code and that works fine...none of the operations performed in the worksheets are shown on screen. However, when I activate different worksheets in the code, the screen does show all the switching between worksheets, which is annoying and I don't want the users to see it. I thought turning off screen updating would take care of that, but I guess not. Does anyone know how to just stay on the worksheet where the user clicks the button to start the macro?

    Thanks,

    Jeff
    Last edited by jstroope; 01-10-2012 at 11:03 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Screen Updating Not Completely Disabled in Excel Macro

    You shouldn't ned to select or activate sheets
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    01-07-2012
    Location
    Pleasanton, CA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Screen Updating Not Completely Disabled in Excel Macro

    Thanks Roy,

    The problem is, if I do something like
    With worksheet A
        Set rFound = .Range("B1:D10").Find etc
             If Not rFound Is Nothing Then
                rFound.Select
    I get an error if I don't first select or activate the worksheet.

    Thanks,

    Jeff
    Last edited by royUK; 01-08-2012 at 12:54 PM.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Screen Updating Not Completely Disabled in Excel Macro

    What error? Post the full code or attach a workbook example

  5. #5
    Registered User
    Join Date
    01-07-2012
    Location
    Pleasanton, CA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Screen Updating Not Completely Disabled in Excel Macro

    Thanks Roy,

    The error message I get is, "1004 Application-defined or object-defined error". This is the code that throws the error:

    Sub risks_Issues()
    
    On Error GoTo HandleErr
    
    ' Set file path, database name and query name for ado connection and command
    strPath = "\\10.233.9.110\share\RevenueCycle\KPIT NATL PMO\RC PMO Planning\PMOS\IssueRiskDatabase\Template"
    strPath = strPath & "\IssueRisk_v2007.accdb"
    strMessage = "No Risks and Issues records returned."
    nrRisksIssues = 0
    ' Pick appropriate query based on region selection
    Select Case Properties.Range("D2").Value
        Case "GLOBAL"
            strQry = "qry_S_IssueRiskDashboard_Global"
        Case "ROCS"
            strQry = "qry_S_IssueRiskDashboard_ROCS"
        Case Else
            strQry = "qry_S_IssueRiskDashboard_Regional"
    End Select
    ' Call the parameters function
    Call parameters(strQry)
    ' Call the connection function.
    If conn(strPath, strQry) <> 1 Then
        MsgBox "There was an error connecting to the database.", vbCritical, "Risks & Issues"
        Exit Sub
    End If
    
    Application.ScreenUpdating = False
    
    ' Refresh the details data
    'RisksIssuesDetails.Activate
    With RisksIssuesDetails
        ' Find the last populated column and row
        lColRi = .Range("A3").End(xlToRight).Column
        lRowRi = .Range("A65536").End(xlUp).Row
        ' If no records returned - set nr flag, inform, clear out old data and exit
        If rs.BOF = True And rs.EOF = True Then
            nrRisksIssues = 1
            Call noRecords(strMessage)
            ' Clean up
            cn.Close
            Set cn = Nothing
            Set rs = Nothing
            Set cmd = Nothing
            Application.ScreenUpdating = True
            Exit Sub
        End If
        ' Clear out the old details data and paste out new data
        If lRowRi < 4 Then ' If there's no data, paste out the new data from the Access query
            .Range("A4").CopyFromRecordset rs
        Else
            .Range(Cells(4, 1), Cells(lRowRi + 1, lColRi)).Clear
            .Range("A4").CopyFromRecordset rs   ' Paste out the new data from the Access query
        End If
        ' Find the new last populated column and row
        lColRi = .Range("A3").End(xlToRight).Column
        lRowRi = RisksIssuesDetails.Range("A65536").End(xlUp).Row
        ' Clear text-to-column errors in ProjectID and IssueRiskId cols F & I
        .Range("I4:I" & lRowRi).TextToColumns
        ' Add cell borders
        With .Range(Cells(4, 1), Cells(lRowRi, lColRi)).borders
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        ' Clean up display
        .Range("A1").Select
        ActiveWindow.ScrollColumn = 1
        ActiveWindow.ScrollRow = 1
    End With
    
    Application.ScreenUpdating = True
    Application.CutCopyMode = False
    
    ExitHere:
        ' Clean up object variables
        cn.Close
        Set cn = Nothing
        Set rs = Nothing
        Set cmd = Nothing
        Exit Sub
        
    HandleErr:
        MsgBox Err.Description, , Err.Number
        Resume ExitHere
        Resume
        
    End Sub
    I commented out the line where I activate the worksheet so it would error out. The number of rows varies depending on the parameters selected. Is there any other way to clear cells, add borders, etc without selecting or activating the worksheet?

    Thanks,

    Jeff

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Screen Updating Not Completely Disabled in Excel Macro

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    Added this time

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Screen Updating Not Completely Disabled in Excel Macro

    You don't appear to declare any variables.

    This is wrong
    .Range(Cells(4, 1), Cells(lRowRi + 1, lColRi)).Clear
    It should be
    .Range(.Cells(4, 1), .Cells(lRowRi + 1, lColRi)).Clear
    Notice .Cells, not just Cells. You've used a With...End With Statement.

  8. #8
    Registered User
    Join Date
    01-07-2012
    Location
    Pleasanton, CA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Screen Updating Not Completely Disabled in Excel Macro

    Thanks Roy - I'll give it a try. The variables are declared as Public in a separate module.

  9. #9
    Registered User
    Join Date
    01-07-2012
    Location
    Pleasanton, CA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Screen Updating Not Completely Disabled in Excel Macro

    Thanks Roy - it worked and it makes perfect sense.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1