Vba: Error type mismatch on referenced worksheet

tatendatiffany

Board Regular
Joined
Mar 27, 2011
Messages
103
i keep getting error mismatch but i have ensure the table names and columns are the same could someone please help me: here is a piece of the code

Dim Index As Sheets
Set Index = Sheets
Sheet1.Activate
Dim Element As Worksheet
Set Element = ActiveSheet
Element.Activate
SQL1 = "SELECT FROM " & Sheets("Index").range("TABLES_S").Value & Sheets("Index").range("TableClmn").Value & Sheets("Element").range("GESTATUS").Value
Debug.Print SQL1

on the higlighted line that is when i get the error
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
My first guess is one or more of the 3 named ranges are NOT single cell ranges.
 
Upvote 0
no ranges are the ranges in the worksheet e.g. TABLES_S it is C2:C39 and i named that range, so its not a single cell..
what does it mean if it is like this should i change it to something else.
 
Upvote 0
Well, which of the 38 values in the range TABLES_S do you want used in the string you are building? The one in C2, or C3, or C4...etc.. ?
 
Upvote 0
the range c2,c3 etc each of the cell contains a different table name and i want string to look at each of them individually and loop through same as with TableClmn....Do you think I should change my approach to looping through each of the cells.


Thank you again
 
Upvote 0
You haven't shown any method of looping at all....

Perhaps something like this..

Rich (BB code):
Dim i As Long
With Sheets("Index")
    For i = 1 to .range("TABLES_S").Rows.Count
        SQL1 = "SELECT FROM " & .range("TABLES_S")(i,1) & .range("TableClmn")(i,1) & Sheets("Element").range("GESTATUS")(i,1)
        Debug.Print SQL1
    Next i
End With
 
Last edited:
Upvote 0
Hi,


Thank you for your help it seems to kind of be working but now i am getting subscript out of range with the line

SQL1 = "SELECT FROM " & .range("TABLES_S")(i, 1) & .range("TableClmn")(i, 1) & Sheets("Element").range("GESTATUS")(i, 1)

and when i hover or it SQL is empty "".

I know what the error mean but i am checking all the spelling and referenced sheets and range match up.
 
Upvote 0
I checked again and realised and had missed something out. nevermind

can i ask another question with this loop i am trying to run through columns the column name and table name in the ranges i mention.
So how to i concate it so it continually loops?? do i add an Cstr + 1

I am new to VBA

Thank you
 
Upvote 0

Forum statistics

Threads
1,215,569
Messages
6,125,600
Members
449,238
Latest member
wcbyers

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top