Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Dynamically declare and populate variables

I have a worksheet that lists database fields, corresponding excel variables
and the data type for each. I'd like to run through the list of variables
and declare each one with the appropriate data type. I've pasted the code
snippet below:

'Load the Variables into an array
ReDim DataVars(HowManyCols) As String
ReDim DataOffset(HowManyCols) As String

Dim Counter As Integer
Counter = 1

'Dimension data and column offset variables
For Each V In VarRange
DataVars(Counter) = V.Value
Select Case V.Offset(ColumnOffset:=1)
Case Is = "Long"
Dim DataVars(Counter) As Long
Case Is = "String"
Dim DataVars(Counter) As String
Case Is = "Date"
Dim DataVars(Counter) As Date
Else
Dim DataVars(Counter) As Variant
End Select

DataOffset(Counter) = V.Value & "_OFF"
Dim DataOffset(Counter) As Integer
Counter = Counter + 1
Next V

When it hits the Dim statement, the Complie error is: Constant expression
required.

Any help you can offer would be greatly appreciated.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Dynamically declare and populate variables

I think you have missed the point of arrays. Arrays are a collection of like
things. In this case the best you could do would be an array of variants. You
need to declare the array up front and then you can redim it as you need to
resize it.
--
HTH...

Jim Thomlinson


"Gary@Dwight" wrote:

I have a worksheet that lists database fields, corresponding excel variables
and the data type for each. I'd like to run through the list of variables
and declare each one with the appropriate data type. I've pasted the code
snippet below:

'Load the Variables into an array
ReDim DataVars(HowManyCols) As String
ReDim DataOffset(HowManyCols) As String

Dim Counter As Integer
Counter = 1

'Dimension data and column offset variables
For Each V In VarRange
DataVars(Counter) = V.Value
Select Case V.Offset(ColumnOffset:=1)
Case Is = "Long"
Dim DataVars(Counter) As Long
Case Is = "String"
Dim DataVars(Counter) As String
Case Is = "Date"
Dim DataVars(Counter) As Date
Else
Dim DataVars(Counter) As Variant
End Select

DataOffset(Counter) = V.Value & "_OFF"
Dim DataOffset(Counter) As Integer
Counter = Counter + 1
Next V

When it hits the Dim statement, the Complie error is: Constant expression
required.

Any help you can offer would be greatly appreciated.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Dynamically declare and populate variables

Thanks Jim:

All of the elements in the array are the same type: strings. What I'm
trying to accomplish is to run through a list of variable names (call that
Column A), and declare each variable according to the type in Column B.

Can you think of another way to accomplish this?

Regards,
Gary@Dwight



"Jim Thomlinson" wrote:

I think you have missed the point of arrays. Arrays are a collection of like
things. In this case the best you could do would be an array of variants. You
need to declare the array up front and then you can redim it as you need to
resize it.
--
HTH...

Jim Thomlinson


"Gary@Dwight" wrote:

I have a worksheet that lists database fields, corresponding excel variables
and the data type for each. I'd like to run through the list of variables
and declare each one with the appropriate data type. I've pasted the code
snippet below:

'Load the Variables into an array
ReDim DataVars(HowManyCols) As String
ReDim DataOffset(HowManyCols) As String

Dim Counter As Integer
Counter = 1

'Dimension data and column offset variables
For Each V In VarRange
DataVars(Counter) = V.Value
Select Case V.Offset(ColumnOffset:=1)
Case Is = "Long"
Dim DataVars(Counter) As Long
Case Is = "String"
Dim DataVars(Counter) As String
Case Is = "Date"
Dim DataVars(Counter) As Date
Else
Dim DataVars(Counter) As Variant
End Select

DataOffset(Counter) = V.Value & "_OFF"
Dim DataOffset(Counter) As Integer
Counter = Counter + 1
Next V

When it hits the Dim statement, the Complie error is: Constant expression
required.

Any help you can offer would be greatly appreciated.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Dynamically declare and populate variables

Dim DataVars() as String
ReDim DataVars(HowManyCols)
Declares an array of strings.

Then later on you try to re-declare using
Dim DataVars(Counter) As Long
You are trying to redeclare elements of the array with different data types
and that is not allowed. Your array DataVars can only be 1 data type.

Why not delcare it as an array of Variants and then you can put whatever
data you want into each of the elements. While geneally it is true that you
want to avoid using varaints as they have more overhead there are times when
variants are the best option.

Finally if we had some idea what you were trying to accomplish (big picture)
then we could possibly give you better suggestions.

--
HTH...

Jim Thomlinson


"Gary@Dwight" wrote:

Thanks Jim:

All of the elements in the array are the same type: strings. What I'm
trying to accomplish is to run through a list of variable names (call that
Column A), and declare each variable according to the type in Column B.

Can you think of another way to accomplish this?

Regards,
Gary@Dwight



"Jim Thomlinson" wrote:

I think you have missed the point of arrays. Arrays are a collection of like
things. In this case the best you could do would be an array of variants. You
need to declare the array up front and then you can redim it as you need to
resize it.
--
HTH...

Jim Thomlinson


"Gary@Dwight" wrote:

I have a worksheet that lists database fields, corresponding excel variables
and the data type for each. I'd like to run through the list of variables
and declare each one with the appropriate data type. I've pasted the code
snippet below:

'Load the Variables into an array
ReDim DataVars(HowManyCols) As String
ReDim DataOffset(HowManyCols) As String

Dim Counter As Integer
Counter = 1

'Dimension data and column offset variables
For Each V In VarRange
DataVars(Counter) = V.Value
Select Case V.Offset(ColumnOffset:=1)
Case Is = "Long"
Dim DataVars(Counter) As Long
Case Is = "String"
Dim DataVars(Counter) As String
Case Is = "Date"
Dim DataVars(Counter) As Date
Else
Dim DataVars(Counter) As Variant
End Select

DataOffset(Counter) = V.Value & "_OFF"
Dim DataOffset(Counter) As Integer
Counter = Counter + 1
Next V

When it hits the Dim statement, the Complie error is: Constant expression
required.

Any help you can offer would be greatly appreciated.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Dynamically declare and populate variables

The big picture is:
Users will import data from a CSV file with column headings into a page of
the spreadsheet. I don't necessarily know which fields they will import each
time, but I do have a finite list of fields. This data will be reviewed and
uploaded to an Oracle database.

I'd like to compare the column heading of the CSV file to the available
columns on my "Data Map" sheet to determine where it will be stored in Oracle
and also to do some validation of the data.

I wanted to avoid a hard coding as much as possible as I intend to use this
same logic in many other sheets. The first problem I encountered was simply
being able to Dim a variable and provide a name:

Dim {the variable name in C15} as {data type in C16}

Thanks for looking at this. Any direction would be appreciated...


"Jim Thomlinson" wrote:

Dim DataVars() as String
ReDim DataVars(HowManyCols)
Declares an array of strings.

Then later on you try to re-declare using
Dim DataVars(Counter) As Long
You are trying to redeclare elements of the array with different data types
and that is not allowed. Your array DataVars can only be 1 data type.

Why not delcare it as an array of Variants and then you can put whatever
data you want into each of the elements. While geneally it is true that you
want to avoid using varaints as they have more overhead there are times when
variants are the best option.

Finally if we had some idea what you were trying to accomplish (big picture)
then we could possibly give you better suggestions.

--
HTH...

Jim Thomlinson


"Gary@Dwight" wrote:

Thanks Jim:

All of the elements in the array are the same type: strings. What I'm
trying to accomplish is to run through a list of variable names (call that
Column A), and declare each variable according to the type in Column B.

Can you think of another way to accomplish this?

Regards,
Gary@Dwight



"Jim Thomlinson" wrote:

I think you have missed the point of arrays. Arrays are a collection of like
things. In this case the best you could do would be an array of variants. You
need to declare the array up front and then you can redim it as you need to
resize it.
--
HTH...

Jim Thomlinson


"Gary@Dwight" wrote:

I have a worksheet that lists database fields, corresponding excel variables
and the data type for each. I'd like to run through the list of variables
and declare each one with the appropriate data type. I've pasted the code
snippet below:

'Load the Variables into an array
ReDim DataVars(HowManyCols) As String
ReDim DataOffset(HowManyCols) As String

Dim Counter As Integer
Counter = 1

'Dimension data and column offset variables
For Each V In VarRange
DataVars(Counter) = V.Value
Select Case V.Offset(ColumnOffset:=1)
Case Is = "Long"
Dim DataVars(Counter) As Long
Case Is = "String"
Dim DataVars(Counter) As String
Case Is = "Date"
Dim DataVars(Counter) As Date
Else
Dim DataVars(Counter) As Variant
End Select

DataOffset(Counter) = V.Value & "_OFF"
Dim DataOffset(Counter) As Integer
Counter = Counter + 1
Next V

When it hits the Dim statement, the Complie error is: Constant expression
required.

Any help you can offer would be greatly appreciated.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Dynamically declare and populate variables

What do you intend to do with these variables once they are declared? The
reason that I ask is that you can't declare variables as you describe.
Declaring variables is an exercise in hard coding. You can not define your
variable names or data types at run time, only at design time.

I have done similar things to what you ask but I have never needed to
declare variables for each column of data. I traverse the cells in the column
using range objects, allowing me to validate the data. I write back the
records to the database using ODBC connections. The only issue there is that
your SQL statement must be able to handle variables to define the field
names. Finally when writing data you may have to do some coercion to get the
XL data into a format that Oracle will like.
--
HTH...

Jim Thomlinson


"Gary@Dwight" wrote:

The big picture is:
Users will import data from a CSV file with column headings into a page of
the spreadsheet. I don't necessarily know which fields they will import each
time, but I do have a finite list of fields. This data will be reviewed and
uploaded to an Oracle database.

I'd like to compare the column heading of the CSV file to the available
columns on my "Data Map" sheet to determine where it will be stored in Oracle
and also to do some validation of the data.

I wanted to avoid a hard coding as much as possible as I intend to use this
same logic in many other sheets. The first problem I encountered was simply
being able to Dim a variable and provide a name:

Dim {the variable name in C15} as {data type in C16}

Thanks for looking at this. Any direction would be appreciated...


"Jim Thomlinson" wrote:

Dim DataVars() as String
ReDim DataVars(HowManyCols)
Declares an array of strings.

Then later on you try to re-declare using
Dim DataVars(Counter) As Long
You are trying to redeclare elements of the array with different data types
and that is not allowed. Your array DataVars can only be 1 data type.

Why not delcare it as an array of Variants and then you can put whatever
data you want into each of the elements. While geneally it is true that you
want to avoid using varaints as they have more overhead there are times when
variants are the best option.

Finally if we had some idea what you were trying to accomplish (big picture)
then we could possibly give you better suggestions.

--
HTH...

Jim Thomlinson


"Gary@Dwight" wrote:

Thanks Jim:

All of the elements in the array are the same type: strings. What I'm
trying to accomplish is to run through a list of variable names (call that
Column A), and declare each variable according to the type in Column B.

Can you think of another way to accomplish this?

Regards,
Gary@Dwight



"Jim Thomlinson" wrote:

I think you have missed the point of arrays. Arrays are a collection of like
things. In this case the best you could do would be an array of variants. You
need to declare the array up front and then you can redim it as you need to
resize it.
--
HTH...

Jim Thomlinson


"Gary@Dwight" wrote:

I have a worksheet that lists database fields, corresponding excel variables
and the data type for each. I'd like to run through the list of variables
and declare each one with the appropriate data type. I've pasted the code
snippet below:

'Load the Variables into an array
ReDim DataVars(HowManyCols) As String
ReDim DataOffset(HowManyCols) As String

Dim Counter As Integer
Counter = 1

'Dimension data and column offset variables
For Each V In VarRange
DataVars(Counter) = V.Value
Select Case V.Offset(ColumnOffset:=1)
Case Is = "Long"
Dim DataVars(Counter) As Long
Case Is = "String"
Dim DataVars(Counter) As String
Case Is = "Date"
Dim DataVars(Counter) As Date
Else
Dim DataVars(Counter) As Variant
End Select

DataOffset(Counter) = V.Value & "_OFF"
Dim DataOffset(Counter) As Integer
Counter = Counter + 1
Next V

When it hits the Dim statement, the Complie error is: Constant expression
required.

Any help you can offer would be greatly appreciated.

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Dynamically declare and populate variables

Thanks Jim...

The connection and upload process is not an issue. I've done that plenty of
times.
Your comments made me think of another solution to this, thanks for your
help...


"Jim Thomlinson" wrote:

What do you intend to do with these variables once they are declared? The
reason that I ask is that you can't declare variables as you describe.
Declaring variables is an exercise in hard coding. You can not define your
variable names or data types at run time, only at design time.

I have done similar things to what you ask but I have never needed to
declare variables for each column of data. I traverse the cells in the column
using range objects, allowing me to validate the data. I write back the
records to the database using ODBC connections. The only issue there is that
your SQL statement must be able to handle variables to define the field
names. Finally when writing data you may have to do some coercion to get the
XL data into a format that Oracle will like.
--
HTH...

Jim Thomlinson


"Gary@Dwight" wrote:

The big picture is:
Users will import data from a CSV file with column headings into a page of
the spreadsheet. I don't necessarily know which fields they will import each
time, but I do have a finite list of fields. This data will be reviewed and
uploaded to an Oracle database.

I'd like to compare the column heading of the CSV file to the available
columns on my "Data Map" sheet to determine where it will be stored in Oracle
and also to do some validation of the data.

I wanted to avoid a hard coding as much as possible as I intend to use this
same logic in many other sheets. The first problem I encountered was simply
being able to Dim a variable and provide a name:

Dim {the variable name in C15} as {data type in C16}

Thanks for looking at this. Any direction would be appreciated...


"Jim Thomlinson" wrote:

Dim DataVars() as String
ReDim DataVars(HowManyCols)
Declares an array of strings.

Then later on you try to re-declare using
Dim DataVars(Counter) As Long
You are trying to redeclare elements of the array with different data types
and that is not allowed. Your array DataVars can only be 1 data type.

Why not delcare it as an array of Variants and then you can put whatever
data you want into each of the elements. While geneally it is true that you
want to avoid using varaints as they have more overhead there are times when
variants are the best option.

Finally if we had some idea what you were trying to accomplish (big picture)
then we could possibly give you better suggestions.

--
HTH...

Jim Thomlinson


"Gary@Dwight" wrote:

Thanks Jim:

All of the elements in the array are the same type: strings. What I'm
trying to accomplish is to run through a list of variable names (call that
Column A), and declare each variable according to the type in Column B.

Can you think of another way to accomplish this?

Regards,
Gary@Dwight



"Jim Thomlinson" wrote:

I think you have missed the point of arrays. Arrays are a collection of like
things. In this case the best you could do would be an array of variants. You
need to declare the array up front and then you can redim it as you need to
resize it.
--
HTH...

Jim Thomlinson


"Gary@Dwight" wrote:

I have a worksheet that lists database fields, corresponding excel variables
and the data type for each. I'd like to run through the list of variables
and declare each one with the appropriate data type. I've pasted the code
snippet below:

'Load the Variables into an array
ReDim DataVars(HowManyCols) As String
ReDim DataOffset(HowManyCols) As String

Dim Counter As Integer
Counter = 1

'Dimension data and column offset variables
For Each V In VarRange
DataVars(Counter) = V.Value
Select Case V.Offset(ColumnOffset:=1)
Case Is = "Long"
Dim DataVars(Counter) As Long
Case Is = "String"
Dim DataVars(Counter) As String
Case Is = "Date"
Dim DataVars(Counter) As Date
Else
Dim DataVars(Counter) As Variant
End Select

DataOffset(Counter) = V.Value & "_OFF"
Dim DataOffset(Counter) As Integer
Counter = Counter + 1
Next V

When it hits the Dim statement, the Complie error is: Constant expression
required.

Any help you can offer would be greatly appreciated.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declare variables to a code? Pas Excel Discussion (Misc queries) 6 April 10th 10 01:14 PM
Declare Variables in Array Mike H. Excel Discussion (Misc queries) 2 March 11th 09 12:33 PM
Declare and Set Public variables jlclyde Excel Discussion (Misc queries) 2 January 28th 09 02:16 PM
How to efficiently declare variables Jeff Excel Discussion (Misc queries) 2 June 29th 06 01:56 PM
Best place to declare variables Taras Excel Programming 7 November 27th 03 04:34 PM


All times are GMT +1. The time now is 11:33 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"