Results 1 to 13 of 13
  1. #1
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664

    Two basic Excel VBA questions

    In the attached zipped file is a Excel spreadsheet with two form control buttons on it.



    The second button, addcolumnremoverows has a three lines of VBA code that should put numbers in that column
    starting with 1 on the second row. Now starts off on the second row but with 2. I would rather it be 1.
    how is this code modified to do it?

    Also, what is the VBA code to select all cells in an Excel worksheet? I read somewhere that
    is as simple as cells.select. That would be great if it were true. Is it?

    cells.select

    Any help appreciated. Thanks in advance.

    Respectfully,

    Lou Reed
    Attached Files Attached Files

  2. #2
    JonSmile is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    11
    For the first bit you could do a simple
    Range ("A" & X).Value = X-1

  3. #3
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    Okay, but what is the VBA code that I that will select all of the Excel spreadsheet's cells?


    Any help appreciated. Thanks in advance.


    Respectfully,


    Lou Reed

  4. #4
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    Okay, the line in the VBA code

    Sheet1.Cells.Select

    will do it. However, when that happens it gets into the Excel VBA get bogged down and seems to get intro an infinite loop. I have to shut own Excel to get things back to normal.

    How do I stop this?

    Any help appreciated. Thanks in advance.

    Respectfully,

    Lou Reed

  5. #5
    JonSmile is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    11
    When you say "all cells in a spreadsheet".
    Do you really mean ALL cells (a1 - zzz999)
    or the cells in a range where you have entered data (so a1 - position of last active cell)?

    Sub SelectAll()
    cells.select in VBA
    End Sub
    does the 1st.

    Sub SelectYourRange()
    Range(Cells(1, 1), ActiveCell).Select
    End Sub

    does the 2nd

  6. #6
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    I mean all cells that have a value in them. Blank cells are ignored.

    Respectfully,

    Lou Reed

  7. #7
    JonSmile is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    11
    Try this

    Sub jon01()


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


    'Selecting only hardcoded data
    Rng.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
    I think that manually selecting all cells in a big spreadsheet based on whether they have a number in them can be reckless. Someone may not get all of them. Then we get errors.

    If I can I write a VBA program that will only select cells that actually have a value in them, that would be much better. Can your VBA code posted immediately above (post # 7) do this?

    Respectfully,

    Lou Reed

  9. #9
    JonSmile is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    11
    Hi,

    The code below is the quickest and as far as I know the easiest way to automatically select all cells with data in them.

    There is no manual selection.

    Did you even try it before posting the above comment?


    Code:
    Sub jon01()
    
    
    Set Rng =Range(Cells(1, 1), ActiveCell)
    
    
    'Selecting only hardcoded data
    Rng.SpecialCells(xlCellTypeConstants).Select
    End Sub

  10. #10
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    No, I am ashamed to admit that i never did try it before posting. I am not sure that I would have known if it worked. The spreadsheet is huge and inspecting all cells is not a good idea - errors will occur.

    Respectfully,

    Lou Reed

  11. #11
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    In the Post # 9 if I compile that it will say that Rng is not defined.

    I must define it as something. Please what do I define it as? I know that it is an easy VBA question,but not for me.

    Any help appreciated. Thanks in advance.

    Respectfully,

    Lou Reed

  12. #12
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Lou,

    Looking at the code below, what object type do you think "Rng" should be declared as???
    Code:
    Sub jon01()
       Dim Rng As ????
    
       Set Rng = Range(Cells(1, 1), ActiveCell)
    
       'Selecting only hardcoded data
       Rng.SpecialCells(xlCellTypeConstants).Select
    
       Set Rng = Nothing
    End Sub

  13. #13
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    I must apologize for my carelessness (stupidity). Rng should be declared as a Range. It is just not what i amused to. It seems I usually declare integers, real,and things like that. I have only come across declaring range in VBA for Excel.

    Respectfully,

    Lou Reed

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Basic Questions - Beginner to MS access
    By speedyrabbit in forum Access
    Replies: 2
    Last Post: 01-20-2017, 05:22 PM
  2. Replies: 2
    Last Post: 01-03-2016, 09:19 PM
  3. Replies: 26
    Last Post: 05-28-2014, 12:23 PM
  4. Basic Questions
    By jlclark4 in forum Access
    Replies: 6
    Last Post: 12-13-2010, 12:09 PM
  5. Basic questions from a newbie! :(
    By Michael_ in forum Access
    Replies: 6
    Last Post: 05-07-2010, 02:41 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