ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Range problem (https://www.excelbanter.com/excel-programming/339672-range-problem.html)

Ali Baba

Range problem
 
I am writting a function and the user needs to select a range for the x
values. If the user selected, say, the range "A1:A2" then

x1 = rng(1)
x2 = rng(2)

If the range "B2:B4", then
x1 = rng(1)
x2 = rng(2)
x3 = rng(3)

I wrote this code to carry out the above task

NoOfRowsSelected = rng.Rows.Count

For i = 1 To NoOfRowsSelected
namex = "x" & i
namex = rng(i).value
Next i

The code is only working for i = 3. In other words, it does not give the
right values for x1, x2, x4, .....

Can anybody help?

Bernie Deitrick

Range problem
 
Ali,

Dim rng As Range
Dim i As Integer

Set rng = Selection
For i = 1 To rng.Rows.Count
MsgBox "x" & i & " is equal to " & rng.Cells(i).Value
Next i

You could also use the Application.Inputbox method to get the user to select
the cells.

HTH,
Bernie
MS Excel MVP


"Ali Baba" wrote in message
...
I am writting a function and the user needs to select a range for the x
values. If the user selected, say, the range "A1:A2" then

x1 = rng(1)
x2 = rng(2)

If the range "B2:B4", then
x1 = rng(1)
x2 = rng(2)
x3 = rng(3)

I wrote this code to carry out the above task

NoOfRowsSelected = rng.Rows.Count

For i = 1 To NoOfRowsSelected
namex = "x" & i
namex = rng(i).value
Next i

The code is only working for i = 3. In other words, it does not give the
right values for x1, x2, x4, .....

Can anybody help?





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

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