2012-04-24 110 views
0

我需要用下面的幫助,運行時錯誤1004,有時溢出錯誤

我有下面的代碼,以尋找在A列中的一些然後再看看如果從1到6,我將不得不轉換它將test1 ... test6,如果有任何1到6可用。

我的代碼出於某種原因顯示運行時錯誤和某個時間溢出錯誤。想知道是否有人能幫我解決這個錯誤。先謝謝你!

Sub macro() 

    Dim rownum As Long, colnum As Long, currcell As Range 

    rownum = ActiveCell.Row 
    colnum = ActiveCell.Column 

    Sheets("Sheet1").Select 

    'Converting 1-6 into Test1-Test6 
    Do 
     Set currcell = ActiveSheet.Cells(rownum, 1) 
     If IsNumeric(currcell.Value) Then 
      If currcell.Value = 1 Then 
       currcell = "test1" 
      ElseIf currcell.Value = 2 Then 
       currcell = "test2" 
      ElseIf currcell.Value = 3 Then 
       currcell = "test3" 
      ElseIf currcell.Value = 4 Then 
       currcell = "test4" 
      ElseIf currcell.Value = 5 Then 
       currcell = "test5" 
      ElseIf currcell.Value = 6 Then 
       currcell = "test6" 
       Exit Do 
      End If 
     End If 
     rownum = rownum + 1 
    Loop 

End Sub 
+0

好了,溢出錯誤可能_maybe_因爲你使用的是無限循環......嘗試添加一些條件,停止在那裏。 – phg 2012-04-24 09:27:46

+0

你有沒有試過在調試模式下通過你的代碼?錯誤發生在哪條線上? – 2012-04-24 10:11:39

+0

它聲明錯誤是在'設置currcell = ActiveSheet.Cells(rownum,1) '...不知道爲什麼它發生在那裏...至於溢出錯誤,它表示在'rownum = rownum + 1' – user1204868 2012-04-24 10:24:37

回答

2

@SiddharthRout,我實現了DIM,它是一個錯字...我不認爲我可以使用.find,因爲我需要修復的變化,只是列A. - user1204868 1分鐘前

這是你想什麼呢?

Option Explicit 

Sub Sample() 
    Dim i As Long 

    With Sheets("Sheet1").Columns(1) '<~~ Col A 
     For i = 1 To 6 
      .Replace What:=i, Replacement:="test" & i, LookAt:=xlWhole, _ 
      SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ 
      ReplaceFormat:=False 

      DoEvents 
     Next 
    End With 
End Sub 
+0

哇..謝謝!這比我能做的更短更快 – user1204868 2012-04-24 10:40:24