开发者

Convert range to string in VBA

I need help converting a range to a usable string. I'm not 100% sure of what I need, because I have little VBA or VB.NET experience, but ultimately i want to take a bunch of cells like B32 to J52 and perform linest function on C-J (as y values) and B (as x value).

I think if I can learn to pop out a string that says "Bxx:Jyy" Then i'd be able to tell it do linest(Cxx:Cyy, Bxx:Byy, 开发者_JAVA百科true, false).

Is there an easier way to do this? Anyway, all I have right now is some code to get the range from the user (just got it from Google). If someone could just help me get the string like stated above, I think I can manage the rest.

Dim oRangeSelected As Range
Set oRangeSelected = Application.InputBox("Please select a range of cells!", _
                     "SelectARAnge Demo", Selection.Address, , , , , 8)


In this case you want the user selected range (not pre-existing selection), so

MsgBox oRangeSelected.Address

[Update] Actually, reading your question more closely, you can use ranges as is with LINEST, ie this sub gets a user range for X and Y and then feeds LINEST. The results are stored in an array, MyArr, which can be returned to the user.

Sub Sample()
    Dim oRangeSelected1 As Range
    Dim oRangeSelected2 As Range
    Dim myArr()
    Set oRangeSelected1 = Application.InputBox("Please select a X range of cells!", "Select X Demo", Selection.Address, Type:=8)
    Set oRangeSelected2 = Application.InputBox("Please select a  Y range of cells!", "Select Y Demo", , Type:=8)
    If oRangeSelected1.Columns.Count + oRangeSelected2.Columns.Count > 2 Then
        MsgBox "Please select a single column range only"
        Exit Sub
    End If
    If oRangeSelected1.Cells.Count <> oRangeSelected2.Cells.Count Then
        MsgBox "Ranges are of different length"
        Exit Sub
    End If
    myArr = Application.WorksheetFunction.LinEst(oRangeSelected1, oRangeSelected2)
    MsgBox "slope is " & Format(myArr(1), "0.00") & " & intercept is " & Format(myArr(2), "0.00")
End Sub


If you use Selected.Address that will display your range as a string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜