2013-03-25 107 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