2011-09-27 53 views
0

我需要將範圍轉換爲可用字符串的幫助。我不是100%確定我需要什麼,因爲我有小的VBA或VB.NET經驗,但最終我想要一堆像B32J52的單元格,並在C - J(作爲y值)執行linest功能,和B(作爲x值)。將範圍轉換爲VBA中的字符串

我想如果我可以學會彈出一個字符串,說"Bxx:Jyy"然後我可以告訴它做linest(Cxx:Cyy, Bxx:Byy, true, false)

有沒有更簡單的方法來做到這一點?無論如何,我現在所擁有的只是從用戶那裏獲得範圍的一些代碼(剛剛從Google獲得)。如果有人能夠像上面所說的那樣幫助我得到字符串,我想我可以管理其餘的。

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

Range數據類型是否與具有索引的數組類似?如果是這樣,您可以遍歷該範圍並將選定的值附加到一個字符串。 –

+0

你在說Excel嗎? –

+0

是的,這是爲excel – bavman

回答

4

在您需要的用戶選擇的範圍(未預存選擇)這種情況下,所以

MsgBox oRangeSelected.Address 

[更新] 其實,更加緊密地閱讀你的問題,你可以使用的範圍爲與LINEST ,即這個子類獲得X和Y的用戶範圍,然後饋送LINEST。結果存儲在一個數組MyArr中,該數組可以返回給用戶。

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 
+0

啊,謝謝。這工作有一個字符串= oRangeSelected.Address。 – bavman

3

如果您使用Selected.Address將顯示您的範圍作爲字符串。

+0

試過這樣做,但我得到了$ J $ 52 ....所以它只是拿起最後選擇的單元格,而不是整個範圍。 – bavman

相關問題