2012-02-24 58 views
1

我想清除行和列中的內容,當找到0時。如何使固定列和變化的行變爲兩者?

我能夠寫出一個代碼來清除它們,但我只能改變行。我的專欄是固定的,它開始變得有點冗長後,我更多的類似的代碼添加到考慮列A到Z

我的代碼如下所示

Sub Macro1() 

Dim columnval As Long, k As Long 

For k = 5 To 6 

    If (Range("A" & k)) = 0 Then 
    Range("A" & k).ClearContents 
    End If 

Next k 

For k = 5 To 6 

    If (Range("B" & k)) = 0 Then 
    Range("B" & k).ClearContents 
    End If 

Next k 

End Sub 

的代碼工作,只是如果您有更好的方法,請與我分享!謝謝!

+0

當你正在尋找一個值,然後通過行和列低效的方式來實現自己的目標循環。爲您更新下面的代碼示例。更多可以在這裏閱讀(http://siddharthrout.wordpress.com/2011/07/14/find-and-findnext-in-excel-vba/) – 2012-02-24 03:41:19

回答

1

這裏是最短並達到你想要的最快方式。

Option Explicit 

Sub Sample() 
    Sheets("Sheet1").Cells.Replace What:="0", Replacement:="", _ 
    LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, _ 
    SearchFormat:=False, ReplaceFormat:=False 
End Sub 

跟進

嗨,如果我只希望在5行6列A-Z以清除內容在什麼?它似乎是清除一切。 - user1204868 14分鐘前

Option Explicit 

Sub Sample() 
    Dim Rng As Range 

    Set Rng = Sheets("Sheet1").Range("A5:" & "Z" & Rows.Count) 

    Rng.Replace What:="0", Replacement:="", _ 
    LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, _ 
    SearchFormat:=False, ReplaceFormat:=False 
End Sub 

希德

+0

嗨sid,謝謝你的回覆! :)我已經嘗試過你的,但它以某種方式刪除第5行下面的所有行。我會用傑裏的答案! :) – user1204868 2012-02-24 07:04:50

+0

是的,因爲我們正在告訴它...您想刪除哪些行?如果只想清除第5行和第6行,則用「Z6」替換{「Z」&Rows.Count};) – 2012-02-24 07:06:35

+0

OH!那也是另一種方式!謝謝! :) – user1204868 2012-02-24 07:21:50

0
option explicit 

sub conditional_clear() 
    dim r as long 
    dim c as long 
    dim ws as worksheet 

    for each ws in worksheets 
     for r = 1 to ws.usedrange.rows.count 
      for c = 1 to ws.usedrange.columns.count 
       with ws.cells(r,c) 
        if .value = 0 then 
         .clearcontents 
        end if 
       end with 
      next c 
     next r 
    next ws 
end sub 

編輯:
如果你想在該地區更多的控制中,你透明細胞內容物,改變這樣的事情

sub conditional_clear(srow as long, erow as long, scol as long, ecol as long) 
    ... 
     for r = srow to erow 
      for c = scol to ecol 
    ... 

,並調用它

call conditional_clear(5, 6, 1, 26) 
+0

嗨,你的代碼似乎有錯誤,在.clearcontents() – user1204868 2012-02-24 03:30:31

+0

嗨,那是一個測試。你通過了。 – bernie 2012-02-24 03:33:31

1

這種方法會將所評估的單元格數量縮減爲您所使用的行和列,而不再多。應該是相當快:

Option Explicit 

Sub ClearZeroValuesInUsedRange() 
Dim LR As Long, LC As Long, r As Long, c As Long 

LR = Cells.Find("*", Cells(Rows.Count, Columns.Count), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 
LC = Cells.Find("*", Cells(Rows.Count, Columns.Count), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column 

Application.ScreenUpdating = False 

    For r = 1 To LR 
     For c = 1 To LC 
      If Cells(r, c) = 0 Then Cells(r, c).ClearContents 
     Next c 
    Next r 

Application.ScreenUpdating = True 
End Sub 

如果你想通過增加一些較大的數字跳到左邊頂部的一些行或列,你可以改變的起始編號。

這個例子是行5 & 6只A-Z:

Sub ClearZeroValuesInUsedRange() 
Dim r As Long, c As Long 

Application.ScreenUpdating = False 

    For r = 5 To 6 
     For c = 1 To 26 
      If Cells(r, c) = 0 Then Cells(r, c).ClearContents 
     Next c 
    Next r 

Application.ScreenUpdating = True 
End Sub 
+0

嗨,如果我只想清除第5行和第6列A-Z列的內容怎麼辦?它似乎是清除一切。 – user1204868 2012-02-24 03:36:31

+0

是的,我添加了上面的第二個版本,只是做那些行/列。 – 2012-02-24 05:17:33

+0

嗨,非常感謝你!:) – user1204868 2012-02-24 07:00:57