Hello Okaderli,
Here is a code example that assigns either the weight range or the height range to a range variable named "Rng". The weight range is "A1:A30" and the height range is "B1:B43". Both ranges are located on worksheet "Sheet1".
Examples:
Dim Rng As Range
'Define Rng' to be equal to the weight range
Set Rng = Worksheets("Sheet1").Range("A1:A30")
'Define Rng to be equal to the height range
Set Rng = Worksheets("Sheet1").Range("B1:B43")
These next examples assume there are header labels in row 1 and "A1" is labeled "Weight" and "B1" is labeled "Height". Both Names have been entered with their defined ranges of "A1:A30" and "B1:B43".
Examples with named ranges:
Dim Rng As Range
'The label "Weight" refers to a named range
Set Rng = Worksheets("Sheet1").Range(Weight)
'The label "Height" refers to a named range
Set Rng = Worksheets("Sheet1").Range("B1:B43")
'Note: if the code is placed within the General Declarations code
'or in the Worksheet event code modules of Sheet1 then the code
'can be written as follows:
'Set Rng = Range("A1:A30") or Set Rng = Range(Weight)
Example of finding a Range's length:
Dim LastRowA As Long
Dim LastRowB As Long
Dim RngA As Range
Dim RngB As Range
With Worksheets("Sheet1")
LastRowA = .cells(.Rows.Count, "A").End(xlUp).Row
LastRowB = .Cells(.Rows.Count, "B").End(xlUp).Row
'RngA = Weight
Set RngA = Range("A1:A" & LastRowA)
'RngB = Height
Set RngB = Range("B1:B" & LastRowB)
End With
Sincerely,
Leith Ross
Bookmarks