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:
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:
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.
Bookmarks