2012-08-02 61 views
0

我想String轉換的細胞位置爲範圍VBA字符串轉換爲區域

Dim closeAfcc As Integer 
Dim restCases As Integer 
Dim AFCCRange As String 
Dim rng As Range 

i = 2 
closeAfcc = 0 
restCases = 0 
Do 
If Sheet1.Cells(i, 6) = "Closed-AFCC" Then 
     closeAfcc = closeAfcc + 1 
     AFCCRange = AFCCRange + "sheet1!K" & i & "," 
     Else 
     restCases = restCases + 1 

     End If 
i = i + 1 
Loop Until Sheet1.Cells(i, 6) = "" 
AFCCRange = Left(AFCCRange, Len(AFCCRange) - 1) 
Set rng = Range(AFCCRange) 

Error 1004

我的字符串結果是
AFCCRange= sheet1!K2,sheet1!K3,sheet1!K4,sheet1!K6,sheet1!K7,sheet1!K8,sheet1!K9,sheet1!K10,sheet1!K11,sheet1!K12

感謝

+1

此代碼硬編碼 「工作表Sheet1」 作爲標籤名稱代碼名稱爲「Sheet1」的表單 - 此名稱是否正確? – brettdj 2012-08-02 06:49:37

+0

是的,這是正確的名稱。 – cfircoo 2012-08-04 09:38:33

回答

0

變化此代碼:

AFCCRange = AFCCRange + "sheet1!K" & i & "," 

閱讀:

AFCCRange = AFCCRange & "'sheet1'!$K$" & i & ", " 

而且改變:

AFCCRange = Left(AFCCRange, Len(AFCCRange) - 1) 

閱讀:

AFCCRange = Left(AFCCRange, Len(AFCCRange) - 2)