2013-03-25 72 views
0

當我將「lFundcolumn」中的「Activesheet.cells」替換爲「rnMonths」時,出現「運行時錯誤13」。如果有人能夠解釋我在這裏做錯了,我會很感激。設置區域內的查找/查找

簡而言之 - 我想在行內找到一個值,並將該列和該列的右側複製。以下是查找錯誤的第一列的代碼。

Sub Roll_period() 

Dim sMonth As String 
Dim rnMonths As Range 
Dim lFundcolumn As Long 
Dim rnRngtocopy As Range 
    sMonth = ActiveSheet.Cells(3, 1).Value 
    Set rnMonths = ActiveSheet.Rows(4) 


lFundcolumn = rnMonths.Find(What:=sMonth, after:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, searchorder:=xlByColumns).Column 
+1

John的回答將解決您的問題,但我也建議在檢查'.Column'屬性前檢查'.Find()'的返回值,否則當找不到該值時會出錯因爲Find()將返回Nothing而不是Range對象) – 2013-03-25 16:11:52

回答

4

...你做的一切都是正確的,你的問題是after:=ActiveCellFind聲明,這可能會或可能不會指向看正確的地方...

它改成這樣:

after:=rnMonths.Cells(1, 1) 

這將是這樣的:

lFundcolumn = rnMonths.Find(What:=sMonth, after:=rnMonths.Cells(1, 1), LookIn:=xlValues, LookAt:=xlWhole, searchorder:=xlByColumns).Column 

希望塔噸做的伎倆!

+0

感謝您的輸入John。 Apreciate幫助。一旦你指出錯誤,它似乎很直接。 – user1624926 2013-03-25 14:25:34