+ Reply to Thread
Results 1 to 9 of 9

Using a For each loop with a column collection

  1. #1
    Registered User
    Join Date
    12-12-2016
    Location
    Tampa
    MS-Off Ver
    2013
    Posts
    6

    Using a For each loop with a column collection

    I am trying to loop through every cell in column "C" and bold the entire row when the value of the cell in Column C matches the search criteria, but I keep getting the type mismatch error. The code is simple enough but for some probably obvious reason I keep receiving the error message. I know there are other ways to do this, but for learning purposes I want to know why I am getting a type mismatch.
    Please Login or Register  to view this content.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,647

    Re: Using a For each loop with a column collection

    As it is written now, variable cell is assigned whole column at once (not cell by cell). To obtain proper result use:
    Please Login or Register  to view this content.
    Best Regards,

    Kaper

  3. #3
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Using a For each loop with a column collection

    Hi,

    You need to specify the .Cells property, otherwise your collection is actually a collection of columns so you are trying to compare the value of the entire column to "RW" in the first (and only) iteration.
    Please Login or Register  to view this content.
    In the real world you would want to limit the rows, of course.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  4. #4
    Registered User
    Join Date
    12-12-2016
    Location
    Tampa
    MS-Off Ver
    2013
    Posts
    6

    Re: Using a For each loop with a column collection

    Awesome! Thank you!

  5. #5
    Registered User
    Join Date
    08-09-2015
    Location
    CzR
    MS-Off Ver
    MS Office 2013
    Posts
    41

    Re: Using a For each loop with a column collection

    Better:

    Please Login or Register  to view this content.

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Using a For each loop with a column collection

    No, it is not better. Reading the Text property of a cell is considerably slower than reading its Value or Value2 property.

  7. #7
    Registered User
    Join Date
    08-09-2015
    Location
    CzR
    MS-Off Ver
    MS Office 2013
    Posts
    41

    Re: Using a For each loop with a column collection

    Who told you? Learn MS helps on properties of the range object and on working with variants. Howgh.

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Using a For each loop with a column collection

    It's a known fact. See for example MVP Charles Williams' blog here- https://fastexcel.wordpress.com/2011...w-to-avoid-it/

    Or just try it yourself. Instead of espousing half-baked theories, you may want to actually try the suggestions you're making, as well as tempering the way you propose things to people.

  9. #9
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,647

    Re: Using a For each loop with a column collection

    Yeh, It sounds reasonable - if one expects string - why use variant?

    But it only sounds well in theory.

    If you DO measure, it is just opposite.
    Text property looks elegant and appropriate, but it is slow compared to variant value or value2 properties.

    Howgh!

    And as most of VBA users are not faculty members, working on theoretical aspects of programming, but simple users working on "single or limited use" solutions for their small but time intensive problems, I'd say variant use is just fine.

+ 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: 4
    Last Post: 10-30-2015, 01:30 PM
  2. [SOLVED] For loop through Worksheets collection
    By amphinomos in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-31-2014, 08:23 AM
  3. [SOLVED] Performance of iteration through Words collection degrades as collection gets bigger
    By 6StringJazzer in forum Word Programming / VBA / Macros
    Replies: 7
    Last Post: 01-14-2014, 09:44 AM
  4. [SOLVED] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  5. Replies: 2
    Last Post: 12-23-2011, 11:04 AM
  6. Replies: 1
    Last Post: 05-09-2011, 11:34 AM
  7. Problems storing a Collection within a Collection. Is this possible?
    By Tarball in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2007, 10:47 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