+ Reply to Thread
Results 1 to 8 of 8

VBA Code fails to select the correct number of Columns in a Spreadsheet

  1. #1
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    VBA Code fails to select the correct number of Columns in a Spreadsheet

    While I was performing calculations on a spreadsheet, I realized it didn't select the correct number of columns.. It is supposed to select 10 columns... But it skips the last one. There are 11 columns and my data begin in the Second column. So, I don't need the first Column, only the following 10 ones.

    How can I adjust the code so it performs the calculations on the 10 columns, excluding the FIRST one?

    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: VBA Code fails to select the correct number of Columns in a Spreadsheet

    In your code you state
    'I calculate the number of columns. This works all right
    but it doesn't, it should be giving a count of 11 and not 10 as you need all of the columns.
    You need to remove the -1 at the end of
    Please Login or Register  to view this content.
    What you are saying is I have 11 columns -1 = 10.
    Then you are starting in column 2
    Please Login or Register  to view this content.
    and so you lose another column and so 10-1 = 9.
    So 9 columns selected from(and including) column 2.
    If my solution worked (or not) please let me know. If your question is answered then please remember to mark it solved

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

  3. #3
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: VBA Code fails to select the correct number of Columns in a Spreadsheet

    Wait, but I just need the 10 columns.. not 11... That's why I do -1..

  4. #4
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: VBA Code fails to select the correct number of Columns in a Spreadsheet

    The first one, all I care is to give me 10 columns...

    It's the second one that doesn't select the right amount of columns... it is supposed to select from B2 all the way right and down the next 9 columns after B2.

  5. #5
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: VBA Code fails to select the correct number of Columns in a Spreadsheet

    Then go the other way and change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    But unless you are using "nb_Actions" somewhere else then it seems a bit strange taking off 1 only to put it back on again later.

  6. #6
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: VBA Code fails to select the correct number of Columns in a Spreadsheet

    This worked! Thanks.. And no, I am using nb_Actions in the same way everywhene I just need 10 columns there.. I need nb_Actions not to count the first column..

  7. #7
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: VBA Code fails to select the correct number of Columns in a Spreadsheet

    Please Login or Register  to view this content.
    Seigna, I am afraid you are misunderstanding how the code works. Columns.Count just produces a count of total number of columns (it doesn't decide the starting point), in this case it returns 11. So nb_Actions when you use it in the code would be 11 but you have a minus 1 at the end and so this means it is now the number 10.
    It doesn't decide the Column to start at.
    What decides the Column to start at in your code is the second 2(in red) in the line below.
    Please Login or Register  to view this content.
    So basically it is now saying
    Row2,Column2 to LastRow,Column((11-1)+1)
    if you change that second 2 to a 1 i.e.
    Please Login or Register  to view this content.
    It will only select up to Column J but start at Column 1
    Last edited by WasWodge; 05-06-2013 at 07:47 AM.

  8. #8
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: VBA Code fails to select the correct number of Columns in a Spreadsheet

    Aaaahhhhh I seee.. so it doesn't matter what nb_Actions is in my case, I can take up all of the columns. Thanks!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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