Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default How to index a variable in VBA

I am trying to create a loop that will enter data according to a loop but no
success:
I have location1,Location2 Location3 Variables Up to 50 How do i correct
this so it works?

For i= 1 to 50
location(i)=Worksheets(1).cells(i).value
Next i
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,646
Default How to index a variable in VBA

Try this:
location&i=Worksheets(1).cells(i).value

Regards,
Stefi

"Jared" wrote:

I am trying to create a loop that will enter data according to a loop but no
success:
I have location1,Location2 Location3 Variables Up to 50 How do i correct
this so it works?

For i= 1 to 50
location(i)=Worksheets(1).cells(i).value
Next i
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default How to index a variable in VBA

Jared,
This shows you how to use a dynamic array with Redim. If you will always
require 50 elements use can just use
Dim Locations(1 to 50) As Variant
'or if you have string/numeric values declare as such

Not sure if you really want the first 50 cells of the worksheet, but I added
the debug line so you can see where you are getting values from.

Dim Locations() As Variant
Const MIN_ELEMENT As Long = 1
Const MAX_ELEMENT As Long = 50
Dim i As Long

ReDim Locations(MIN_ELEMENT To MAX_ELEMENT)

For i = MIN_ELEMENT To MAX_ELEMENT
Debug.Print Worksheets(1).Cells(i).Address
Locations(i) = Worksheets(1).Cells(i).Value
Next i

NickHK

"Jared" wrote in message
...
I am trying to create a loop that will enter data according to a loop but

no
success:
I have location1,Location2 Location3 Variables Up to 50 How do i correct
this so it works?

For i= 1 to 50
location(i)=Worksheets(1).cells(i).value
Next i
End Sub



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default How to index a variable in VBA

This was great to learn, but i still have a problem. I have a lot of
variables. location1, location2 and so on. doing Location(1) =
worksheets(i).cells(i,1).value does not work. it's having trouble with the
"Location(i)" i do not know how to define that so it doesn't give me an error

thanks

jared


"NickHK" wrote:

Jared,
This shows you how to use a dynamic array with Redim. If you will always
require 50 elements use can just use
Dim Locations(1 to 50) As Variant
'or if you have string/numeric values declare as such

Not sure if you really want the first 50 cells of the worksheet, but I added
the debug line so you can see where you are getting values from.

Dim Locations() As Variant
Const MIN_ELEMENT As Long = 1
Const MAX_ELEMENT As Long = 50
Dim i As Long

ReDim Locations(MIN_ELEMENT To MAX_ELEMENT)

For i = MIN_ELEMENT To MAX_ELEMENT
Debug.Print Worksheets(1).Cells(i).Address
Locations(i) = Worksheets(1).Cells(i).Value
Next i

NickHK

"Jared" wrote in message
...
I am trying to create a loop that will enter data according to a loop but

no
success:
I have location1,Location2 Location3 Variables Up to 50 How do i correct
this so it works?

For i= 1 to 50
location(i)=Worksheets(1).cells(i).value
Next i
End Sub




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default How to index a variable in VBA

I just want to add another comment that might put light on the matter.
I am actually trying to take input from a user form into cells. Now
basically that is when i get an error. I don' know how to define an already
defined variable (text box)

i have 50 boxes which i need to enter into a table.
i can do cells(3,4)=me.location1.value
cells(4,4)=me.location2.value
cells(5,4)=me.location3.value
up to 50
but that sounds stupid and not effeciant
i am sure there's a way around it.
please help



"NickHK" wrote:

Jared,
This shows you how to use a dynamic array with Redim. If you will always
require 50 elements use can just use
Dim Locations(1 to 50) As Variant
'or if you have string/numeric values declare as such

Not sure if you really want the first 50 cells of the worksheet, but I added
the debug line so you can see where you are getting values from.

Dim Locations() As Variant
Const MIN_ELEMENT As Long = 1
Const MAX_ELEMENT As Long = 50
Dim i As Long

ReDim Locations(MIN_ELEMENT To MAX_ELEMENT)

For i = MIN_ELEMENT To MAX_ELEMENT
Debug.Print Worksheets(1).Cells(i).Address
Locations(i) = Worksheets(1).Cells(i).Value
Next i

NickHK

"Jared" wrote in message
...
I am trying to create a loop that will enter data according to a loop but

no
success:
I have location1,Location2 Location3 Variables Up to 50 How do i correct
this so it works?

For i= 1 to 50
location(i)=Worksheets(1).cells(i).value
Next i
End Sub






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default How to index a variable in VBA

Hi Jared,

Try this:

Dim i as Integer
For i = 1 to 50
Cells (i+2,4) = Me.Controls("Location" & i).Value
Next



Jared wrote:
I just want to add another comment that might put light on the matter.
I am actually trying to take input from a user form into cells. Now
basically that is when i get an error. I don' know how to define an already
defined variable (text box)

i have 50 boxes which i need to enter into a table.
i can do cells(3,4)=me.location1.value
cells(4,4)=me.location2.value
cells(5,4)=me.location3.value
up to 50
but that sounds stupid and not effeciant
i am sure there's a way around it.
please help



"NickHK" wrote:

Jared,
This shows you how to use a dynamic array with Redim. If you will always
require 50 elements use can just use
Dim Locations(1 to 50) As Variant
'or if you have string/numeric values declare as such

Not sure if you really want the first 50 cells of the worksheet, but I added
the debug line so you can see where you are getting values from.

Dim Locations() As Variant
Const MIN_ELEMENT As Long = 1
Const MAX_ELEMENT As Long = 50
Dim i As Long

ReDim Locations(MIN_ELEMENT To MAX_ELEMENT)

For i = MIN_ELEMENT To MAX_ELEMENT
Debug.Print Worksheets(1).Cells(i).Address
Locations(i) = Worksheets(1).Cells(i).Value
Next i

NickHK

"Jared" wrote in message
...
I am trying to create a loop that will enter data according to a loop but

no
success:
I have location1,Location2 Location3 Variables Up to 50 How do i correct
this so it works?

For i= 1 to 50
location(i)=Worksheets(1).cells(i).value
Next i
End Sub





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
look up, match index? multi variable formulas Jason Excel Worksheet Functions 1 December 16th 09 03:33 PM
How do I use a variable as a reference to an array in INDEX Steve Haack Excel Worksheet Functions 6 July 29th 08 06:16 PM
Help on index addressing using variable sheets & cell references Greggers Excel Worksheet Functions 3 September 4th 07 02:38 PM
Using INDEX() to Locate Variable Data Shadowboy Excel Discussion (Misc queries) 4 August 21st 06 02:17 PM
Variable values in Index function mlkpied Excel Worksheet Functions 6 December 6th 04 11:38 PM


All times are GMT +1. The time now is 04:22 AM.

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

About Us

"It's about Microsoft Excel"