+ Reply to Thread
Results 1 to 8 of 8

Why Would Range() Apply Different Data Types to My Data?

  1. #1
    Registered User
    Join Date
    01-17-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    25

    Why Would Range() Apply Different Data Types to My Data?

    Hello VBAmasters,

    Recently I posted a problem having to do with looping through two different columns at the same time. (As a newbie forum member, I'm forbidden to include URLs in my post, sorry.) Another forum member helped me out, and posted some code; here's the relevant snippet:

    Please Login or Register  to view this content.
    This code ran on a spreadsheet where columns B and C were populated with text strings:


    And the code worked great. When I turn on the debugger and set a watch to the "Girls" and "Tasks" variables, I see what I guess is an array of strings; exactly what I want:


    The example code works perfectly.

    Now to be honest, I am working on a very large spreadsheet with a lot of VBA code. The "Loop through two columns" issue I posted about was smaller problem in that code. For posting purposes, I deliberately created a little toy spreadsheet that illustrated the looping problem. I thought that if someone would post a solution, I could adapt that solution to my "real" spreadsheet. Here's the adaptation:

    Please Login or Register  to view this content.
    There's not much that is different. I changed the variable names and the two columns I need to loop through are on separate worksheets. I didn't think that would pose any problems. The columns look like this; here's "Peers", and then "ProtoCmds":

    Like the toy version, these columns are just strings, nothing more. I want Excel and VBA to see them as text strings. And when the Range() command runs in my VBA code, I want the function to return an array of strings.

    So when I run my adapted version of the code, VBA throws a "Run-time error '13' :: Type mismatch" error, which basically means I'm feeding Ubound() the wrong data types. I set a debug watch on renamed variables "Peers" and "ProtoCmds" and sure enough, they are NOT arrays of strings:


    So, "Peers" seems to be an array of... empty variants? And worse, "ProtoCmds" appears to be a single string. Not an array. No wonder Ubound() choked when it was fed this variable. I wish I knew why Range() thought it was looking at one cell, when there are at least a hundred cells of strings in that column. Perhaps the data in the first cell ("!========...") fools it or something???

    So I'm just guessing here, but I wonder if when Range() is called, it looks at the data and makes an educated guess about the data type it is looking at. In my toy spreadsheet, it correctly guessed that I wanted an array of strings. In my "real" spreadsheet, it incorrectly guessed and gave me the wrong data types.

    Is this what is going on here? Why might Range() be misreading my data? Is there was way to force Range() to return the data types I want? Thanks so much for reading.
    Attached Images Attached Images

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Why Would Range() Apply Different Data Types to My Data?

    There is one problem and one possible problem.

    1. What if the number of rows in each column is different?
    2. The 2nd part of the range also needs a worksheet object qualifier. If not set, it uses the activesheet.
    e.g.
    Please Login or Register  to view this content.
    Do that for the other as well.

  3. #3
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,004

    Re: Why Would Range() Apply Different Data Types to My Data?

    Please Login or Register  to view this content.
    2 question:
    1. Cells(Rows.count, 2) means last row in the sheet in col B not D (2 is col B), is that what you want?
    2. Cells(Rows.count, 2), because you don't qualify the cell and the row then it mean the code will use the cell & rows in the active sheet not in sheet Worksheets("Variables").

    If what you want is to get the range in Worksheets("Variables").Range("D26:D" to last row with data), then:

    Please Login or Register  to view this content.
    And the same problem with:
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    01-17-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: Why Would Range() Apply Different Data Types to My Data?

    Thanks Kenneth,

    Good catch of the lack of a Worksheet qualifier on the "Peers" variable. I'd completely missed that.

    The number of rows in the "Peers" and "ProtoCmds" are not related and will almost certainly not be the same. I'll need the VBA to dynamically work with both columns, regardless of how long each are. (I think its also safe to assume neither column will be longer than 100 rows.)

    Its late where I am (New Jersey), so I'll add the qualifiers first thing in the AM and retest. Thanks so much!

  5. #5
    Registered User
    Join Date
    01-17-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: Why Would Range() Apply Different Data Types to My Data?

    Wow, Akuini, you're good!

    1) No, I definitely mean to define "Peers" as the rows D26, D27, D28... D-something. Never straying into Column B.

    2) You're correct that my code is working off of another active worksheet when it runs the lines I quoted in my original post. I hadn't thought that I was grounding the beginning of my range on Worksheet "Variables" but not grounding the end.

    It is late where I am and I need to get to bed. I will test your fixes in the morning and post responses. Thank you for your help!

  6. #6
    Registered User
    Join Date
    01-17-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: Why Would Range() Apply Different Data Types to My Data?

    Hi Kenneth,

    Yep, you were right, I wasn't properly referencing the end of the range. Plugging that did the trick. Thanks!

  7. #7
    Registered User
    Join Date
    01-17-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: Why Would Range() Apply Different Data Types to My Data?

    Thanks Akuini,

    Yep, you spotted the problem. Thanks, I'm good to go! Appreciate the help...!

  8. #8
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,004

    Re: Why Would Range() Apply Different Data Types to My Data?

    You're welcome, glad to help, & thanks for the feedback.

+ 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: 1
    Last Post: 10-01-2015, 03:02 PM
  2. [SOLVED] Apply formula through a range of data
    By meghs918 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-22-2015, 06:59 PM
  3. Replies: 4
    Last Post: 10-02-2013, 09:39 AM
  4. [SOLVED] Macro to select a data range then apply a custom sort
    By dvsbyknight in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-20-2013, 01:50 AM
  5. How do I only apply labels to the first data in a range?
    By spaceknight in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-04-2013, 08:26 PM
  6. COUNTING 3 DIFFERENT TYPES OF DATA FROM A RANGE
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-01-2006, 07:10 PM
  7. How do I apply the ROUNDUP formula to a range of calculated data
    By IslandGreenHouse in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-28-2005, 06:05 PM

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