Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664

    Selecting all active cells on a spreadsheet

    In the code that was given to me by someone here I take is to select cells in an Excell spreadsheet.I would guess all active cells. It selects cells alright, but only a few. It does not select all active cells. it select just a section of them.

    What is wrong here?

    Any help appreciated. Thanks in advance.


    Respectfully,

    Lou Reed

    The code is:


    Sub selectcells()
    Dim Rng As Range


    Set Rng = Range(Cells(1, 1), ActiveCell)






    'Selecting only hardcoded data
    Rng.SpecialCells(xlCellTypeConstants).Select
    End Sub

  2. #2
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    ActiveCell in Excel is only a single cell. So it is dependent upon whatever cell is the ActiveCell at the time when the code is run.
    I would highly recommend not having your VBA code run against something as subjective as that.
    If you want to run it against an entire selected range, you would use "Selection", not "ActiveCell" ("Selection" can be multiple cells, "ActiveCell" is just a single cell).

    The question is, what do you really want this code to do (specifically, what range do you want it to run against)?

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    try:

    ActiveSheet.UsedRange.Select

  4. #4
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    I want the code to select all cells that have a value in them, preferably a numerical value, but I will take selecting cells that have a text value in them. I do not trust that I (or anyone else) can select all the numerical cells in such a large spreadsheet.

    I would rather the software does it. If I can write the correct VBA code then anyone can easily select all the correct cells by pressing a button on the spreadsheet. I believe that is the best way to insure that all cells are selected.

    So if it can be done then how do I do it?

    Respectfully,

    Lou Reed

  5. #5
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I want the code to select all cells that have a value in them
    Yes, that is very different than the ActiveCell.

    Did you try RanMan's suggestion?

  6. #6
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    I did not use Raman's suggestion - yet. I find now with what I have for VBA code that after a couple of presses of the select
    fields button thta all fileds with values in them are selected - along with some other fields, but so what that is okay.

    Again I have to press the select fields button more than once to get my relevant fields slected, but at least
    they are selected.

    Where do I put Raman code if i want to use it? What line in there now does it replace?

    Does his suggestion allow for only one pressing of the "Select Fields" button.

    Any help appreciated. tahnks in advance.

    Respectfully,

    Lou Reed

  7. #7
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You can combine Ranman's suggestion with your code, to condense it down to a one-line procedure:
    Code:
    Sub selectcells()
        ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants).Select
    End Sub

  8. #8
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    Yes, that works very well.

    Thanks you very much.

    Respectfully,

    Lou Reed

  9. #9
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You are welcome.

  10. #10
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    i just learned that when I use the code in Post #7 that it selects everything, all cells regardless of whether they are occupied or empty. Thus would be okay except when I go to change selected cells to text it
    gets bogged down in the process, the hourglass icon is there, but it never stops like an endless loop. Thus I would like to modify the code from what I am using to something else that can only select the relevant cells, because as I said that it is just to slow when it comes to changing the cells from numbers to text. - I do not know that it will ever stop. It seems to go on continuously.
    So a modification is in order. How do I do it?

    Any help appreciated. Thanks in advance.


    Respectfully,

    Lou Reed

  11. #11
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    How do you define "relevant cells"?
    You need to explain to us the logic of how to determine exactly which cells to include.

  12. #12
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    Relevant cells are as few cells as possible, but all cells with numbers in them are selected. It makes no sense to select every cell in the spreadsheet. If I do when it comes time to add an apostrophe
    the process as I said gets very bogged down. It may be stuck in a infinite loop or it may be just a long process, either way, I have never waited to see the end of the process so I do not know.

    It seems that there must be a better way. That is all that I am asking.

    Respectfully,

    Lou Reed

  13. #13
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    It makes no sense to select every cell in the spreadsheet.
    It doesn't. It only selects the UsedRange. It shouldn't select every cell unless you have used every cell.

    Relevant cells are as few cells as possible, but all cells with numbers in them are selected.
    That's too generic an explanation. We need to more defined rule we can program too.
    Are there any certain columns you are concerned with?
    What exactly is it that you intend to do with the cells you are selecting?

  14. #14
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    The cells that I am concerned with are thee cells with values in them. A blank cells should stay blank. A cells that is occupied should be selected.

    That is all. If it selects all cells now, well I told you what would happen.

    Respectfully,

    Lou Reed

  15. #15
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    This code will only select cells that have values in them:
    Code:
    Sub selectcells()
        ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants).Select
    End Sub
    If you run that code by itself (and NO other code), does it seem to select the correct cells?
    If not, tell me the address of the very first incorrect cell it selects, and tell me what is in that cell.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Not selecting all cells with cntrl-A
    By Lou_Reed in forum Access
    Replies: 2
    Last Post: 12-12-2017, 11:12 AM
  2. Replies: 3
    Last Post: 05-11-2016, 02:07 PM
  3. Replies: 1
    Last Post: 01-11-2014, 12:39 PM
  4. Replies: 4
    Last Post: 06-10-2012, 02:29 PM
  5. Replies: 3
    Last Post: 02-07-2012, 06:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums