Assign a Range of values to variable to be used in same macro

jwp

New Member
Joined
Aug 31, 2011
Messages
33
Am trying to assign a value in a range from (A1:A100) to a variable I want to use in the same macro a 100 times.

I want a Loop to take the activecell assign it to a variable use that in a macro then move to the next row and take the next cell value.

Can someone help me pass this variable to another macro?

Code:
For Count = 0 To 100
Var = ActiveCell.Offset(Count, 0)

Call Macro 'to use the Variable Var

Next Count


End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
dim myRange as Range

range("A1:A100").select


For each MyRange in Selection

' your bit .....call macro with myRange.value

next myRange

' a cell is the default item of a selection
 
Upvote 0
You should get the idea.

Code:
Sub loopthroughrange()

Dim var As Variant
var = Range("A1:A100")
For Each i In var
MsgBox i

Next i

End Sub
 
Upvote 0
make your variable public so that all macro can use that variable.
E.g. Dim myVar as long
but put it outside the procedure
 
Upvote 0
Am trying to assign a value in a range from (A1:A100) to a variable I want to use in the same macro a 100 times.

I want a Loop to take the activecell assign it to a variable use that in a macro then move to the next row and take the next cell value.

Can someone help me pass this variable to another macro?

Code:
For Count = 0 To 100
Var = ActiveCell.Offset(Count, 0)

Call Macro 'to use the Variable Var

Next Count


End Sub



You could assign the Range to an array
Sub something()
MyArr=Range("A1:A100").value
End sub

The first element would be
MyArr(1,1)
second element
MyArr(2,1)
and so on..
 
Upvote 0
Sorry , Villy is correct, I misread the post.

Code:
Public public_var As Variant
Sub firstsub()

var = Range("A1:A100")

For Each i In var
  public_var = i
  MsgBox public_var

  calledsub
  Next i
  
End Sub
Code:
Sub calledsub()
  
MsgBox "Public Variable " + CStr(public_var)

End Sub
 
Upvote 0
I tried making code work and I believe few important decalartions were missed out

Code:
Dim public_var As Variant
Sub firstsub()
Dim Var As Range, i As Range

Set Var = Range("A1:A100")
For Each i In Var
  public_var = i
  MsgBox public_var
  calledsub
  Next i
  
End Sub

Sub calledsub()
  
MsgBox "Public Variable " + CStr(public_var)
End Sub

Biz
 
Upvote 0
Thanks Biz

It wasn't an exact answer, the user will have to tweak it a bit.
It just gives the general idea.

Additionally it might be savy to declare
Code:
Option Explicit
before the Sub Routine and the Public variable declaration (above everything else).

It forces all Variables to be declared and might be advantageous if your sub routines get large and you are passing a lot variables between two or more sub routines.

Cheers
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

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