ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   How to set variable to cell value in excel macro (https://www.excelbanter.com/excel-discussion-misc-queries/52431-how-set-variable-cell-value-excel-macro.html)

most xlent

How to set variable to cell value in excel macro
 
How do I perform a "do loop" using a cell value as criteria? (i.e.-do while
x < value($c$1). Thanks for your help.

Don Guillett

How to set variable to cell value in excel macro
 
the help files are your friend
While...Wend Statement Example
This example uses the While...Wend statement to increment a counter
variable. The statements in the loop are executed as long as the condition
evaluates to True.

Dim Counter
Counter = 0 ' Initialize variable.
While Counter < 20 ' Test value of Counter.
Counter = Counter + 1 ' Increment Counter.
Wend ' End While loop when Counter 19.
Debug.Print Counter ' Prints 20 in the Immediate window.

--
Don Guillett
SalesAid Software

"most xlent" wrote in message
...
How do I perform a "do loop" using a cell value as criteria? (i.e.-do

while
x < value($c$1). Thanks for your help.




most xlent

How to set variable to cell value in excel macro
 
Hi Don,
This would work if the cell value was constant, however, it is a result of a
formula that changes with variable input in the worksheet. Currently, I
input data into the worksheet and must look at the value of the cell. Then I
need to edit the macro with the new cell value for the do loop. This becomes
time consuming with all the data I am processing. Ideally, I would like the
macro to retrieve the cell value automatically. Thanks in advance for your
next response.

"Don Guillett" wrote:

the help files are your friend
While...Wend Statement Example
This example uses the While...Wend statement to increment a counter
variable. The statements in the loop are executed as long as the condition
evaluates to True.

Dim Counter
Counter = 0 ' Initialize variable.
While Counter < 20 ' Test value of Counter.
Counter = Counter + 1 ' Increment Counter.
Wend ' End While loop when Counter 19.
Debug.Print Counter ' Prints 20 in the Immediate window.

--
Don Guillett
SalesAid Software

"most xlent" wrote in message
...
How do I perform a "do loop" using a cell value as criteria? (i.e.-do

while
x < value($c$1). Thanks for your help.





Don Guillett

How to set variable to cell value in excel macro
 
It could probably be done within a worksheet_change event
right click the sheet tabview codego from there

--
Don Guillett
SalesAid Software

"most xlent" wrote in message
...
Hi Don,
This would work if the cell value was constant, however, it is a result of

a
formula that changes with variable input in the worksheet. Currently, I
input data into the worksheet and must look at the value of the cell.

Then I
need to edit the macro with the new cell value for the do loop. This

becomes
time consuming with all the data I am processing. Ideally, I would like

the
macro to retrieve the cell value automatically. Thanks in advance for

your
next response.

"Don Guillett" wrote:

the help files are your friend
While...Wend Statement Example
This example uses the While...Wend statement to increment a counter
variable. The statements in the loop are executed as long as the

condition
evaluates to True.

Dim Counter
Counter = 0 ' Initialize variable.
While Counter < 20 ' Test value of Counter.
Counter = Counter + 1 ' Increment Counter.
Wend ' End While loop when Counter 19.
Debug.Print Counter ' Prints 20 in the Immediate window.

--
Don Guillett
SalesAid Software

"most xlent" wrote in message
...
How do I perform a "do loop" using a cell value as criteria? (i.e.-do

while
x < value($c$1). Thanks for your help.







Kleev

How to set variable to cell value in excel macro
 
You could do something like this:
Dim ws As Worksheet
Dim i As Integer
Dim x As Integer
Set ws = ThisWorkbook.Worksheets("sheet2")
i = ws.Range("c1").Value
x = 1
Do While x < i
ws.Cells(x, 4).Value = x + i
x = x + 1
Loop

"most xlent" wrote:

How do I perform a "do loop" using a cell value as criteria? (i.e.-do while
x < value($c$1). Thanks for your help.


most xlent

How to set variable to cell value in excel macro
 
Way to go Kleev!
Your suggestion did the trick. Thanks alot!

"Kleev" wrote:

You could do something like this:
Dim ws As Worksheet
Dim i As Integer
Dim x As Integer
Set ws = ThisWorkbook.Worksheets("sheet2")
i = ws.Range("c1").Value
x = 1
Do While x < i
ws.Cells(x, 4).Value = x + i
x = x + 1
Loop

"most xlent" wrote:

How do I perform a "do loop" using a cell value as criteria? (i.e.-do while
x < value($c$1). Thanks for your help.


Kleev

How to set variable to cell value in excel macro
 
You are welcome.

"most xlent" wrote:

Way to go Kleev!
Your suggestion did the trick. Thanks alot!

"Kleev" wrote:

You could do something like this:
Dim ws As Worksheet
Dim i As Integer
Dim x As Integer
Set ws = ThisWorkbook.Worksheets("sheet2")
i = ws.Range("c1").Value
x = 1
Do While x < i
ws.Cells(x, 4).Value = x + i
x = x + 1
Loop

"most xlent" wrote:

How do I perform a "do loop" using a cell value as criteria? (i.e.-do while
x < value($c$1). Thanks for your help.



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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com