+ Reply to Thread
Results 1 to 9 of 9

Focusing on cells without "selecting" them

Hybrid View

  1. #1
    Registered User
    Join Date
    09-05-2013
    Location
    West Plains, Missouri
    MS-Off Ver
    Access 2003, Excel 2007
    Posts
    18

    Focusing on cells without "selecting" them

    Hi all,

    I am still fairly new to VBA and have in issue I can not solve. Is there a way to set the focus on a range of cells and have a fuction, like sort, happen without actually selecting that range? I want my macro to run in the background without disturbing what I am doing on my other open spreadsheets. Currently if the wookbook is not open, I get an error, also I want the sort to happen without the screen flashing at me. Here is my code:

    Sub Auto_Sort()
    '
    ' Auto_Sort Macro
    '
    
    
        
        
        Range("A50:C83").Select
        ThisWorkbook.Worksheets("Dashboard").Sort.SortFields.Clear
        ThisWorkbook.Worksheets("Dashboard").Sort.SortFields.Add Key:=Range( _
            "C50:C83"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
            xlSortNormal
        With ThisWorkbook.Worksheets("Dashboard").Sort
            .SetRange Range("A50:C83")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
    'Reset Auto_Sort counter
        Application.OnTime Now + TimeValue("00:00:30"), "Auto_sort"
        
        
    End Sub
    Hope I did that right!

    All help is appreciated!
    ToddHarmon

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Focusing on cells without "selecting" them

    Sub Auto_Sort()
    Worksheets("Dashboard").Range("A50:C83").Sort key1:=Range("C50"), order1:=xlDescending, Header:=xlNo, DataOption1:=xlSortNormal
    End Sub
    If solved remember to mark Thread as solved

  3. #3
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Focusing on cells without "selecting" them

    Sub Auto_Sort()
    ThisWorkbook.Worksheets("Dashboard").Range("A50:C83").Sort key1:=Range("C50"), order1:=xlDescending, Header:=xlNo, DataOption1:=xlSortNormal
    End Sub

  4. #4
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Focusing on cells without "selecting" them

    Sub Auto_Sort()
    ThisWorkbook.Worksheets("Dashboard").Range("A50:C83").Sort key1:=Range("C50"), order1:=xlDescending, Header:=xlNo, DataOption1:=xlSortNormal
    End Sub

  5. #5
    Registered User
    Join Date
    09-05-2013
    Location
    West Plains, Missouri
    MS-Off Ver
    Access 2003, Excel 2007
    Posts
    18

    Re: Focusing on cells without "selecting" them

    That helped out some, taught me a cleaner way to code. I am still getting a "run time error-'9' Script out of range" message when another spreadsheet is open and in use. Any way to fix this problem?

    Rep for the cleaner code!

  6. #6
    Registered User
    Join Date
    09-05-2013
    Location
    West Plains, Missouri
    MS-Off Ver
    Access 2003, Excel 2007
    Posts
    18

    Re: Focusing on cells without "selecting" them

    Thank you, I will see if that works for me. I may have been using that command improperly.

  7. #7
    Registered User
    Join Date
    09-05-2013
    Location
    West Plains, Missouri
    MS-Off Ver
    Access 2003, Excel 2007
    Posts
    18

    Re: Focusing on cells without "selecting" them

    Still doesn't seem to work for me. I am showing all of my code for this project to see if it is something else that is causing the problem. The Update_Link module works like it should, and so does the sort module as long as this spreadsheet is the open one. As soon as I open any other spreadsheet it now gives a stop 400 error.

    Sub Update_Link()
    '
    'Update_Link Macro
    'Dim r as starting row, cc as cell row, str1 as file location, str2-11 as cell location, nc as cell location
    
    nc = 6
    nc2 = 7
    r = 50
    str1 = Range("C3") + "Audit Form'!"
    str2 = "$G$5"
    str3 = "$N$5"
    str4 = "$A$" & cc
    str5 = "$B$" & cc
    str12 = "$CQ$"
    str14 = "$J$"
    
    'Establish links to external spreadsheet
    
        Range("C8") = "=" + str1 + str2
        Range("C9") = "=" + str1 + str3
            
        
            For cc = 10 To 43
             Range("A" & r).Select
             Range("A" & r) = "=" + str1 + str4 & cc
             Range("C" & r) = "=" + str1 + str5 & cc
             r = r + 1
            Next cc
            
            
             Range("F13") = "=" + str1 + str12 & nc
             Range("F15") = "=" + str1 + str12 & nc2
             
            
            
    'Initiate Sorting
    
        Application.OnTime Now + TimeValue("00:00:01"), "ThisWorkbook.Auto_sort"
    
    'Set focus on primary sheet
        Range("A1").Activate
        
    End Sub
    
    Sub Auto_Sort()
    '
    ' Auto_Sort Macro
    '
    
    
    ThisWorkbook.Worksheets("Dashboard").Range("A50:C83").Sort key1:=Range("C50"), order1:=xlDescending, Header:=xlNo, DataOption1:=xlSortNormal
    
        
    'Reset Auto_Sort counter
        Application.OnTime Now + TimeValue("00:00:05"), "ThisWorkbook.Auto_sort"
        
     Range("A1").Select
    End Sub
    I'm completely confused as to why it won't work. All of the code is in the "ThisWorkbook" section in VBA. Do the modules need to be seperated maybe?

  8. #8
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Focusing on cells without "selecting" them

    Usually I put all macros in Modules, not in ThisWorkbook area.
    I put only some event macros in ThisWorkbook area.

  9. #9
    Registered User
    Join Date
    09-05-2013
    Location
    West Plains, Missouri
    MS-Off Ver
    Access 2003, Excel 2007
    Posts
    18

    Re: Focusing on cells without "selecting" them

    Going to mark this one as solved. Since this program is primarily used as a stand alone, I have added a workaround by disabling the macro by user input. Thanks for the help, it is appreciated!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 5
    Last Post: 01-23-2014, 11:02 AM
  2. [SOLVED] selecting cells accordnig to date, "countif"
    By rodgersmg in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 11-12-2013, 01:55 AM
  3. [SOLVED] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM
  4. Replies: 3
    Last Post: 01-25-2011, 09:31 PM
  5. pasting problem after selecting "go to" cells
    By Santana20 in forum Excel General
    Replies: 2
    Last Post: 03-09-2010, 03:08 PM

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