嗨,如果我想回傳它在 VBA 中找到的單元格的值,并在其下方另外 3 行清除它的所有內容,但我目前被卡住了,因為它可以找到單元格的形成位置但已洗掉整個范圍而不是特定范圍(我知道范圍(“A7:H20”)是錯誤的,謝謝大家,如果你能幫忙
Sub Find_and_remove()
For Each cell In Range("A7:H20")
If cell.Value = Range("Q5") Then
'return the range of that cell and deleted it with 3 row below it'
Range("A7:H20").Clear
End If
Next cell
End Sub
uj5u.com熱心網友回復:
Sub Find_and_remove()
Dim rng As Range
For Each rng In Range("A7:H20")
If rng.Value = Range("Q5") Then Range(rng, rng.Offset(3, 0)).Clear
Next cell
End Sub
uj5u.com熱心網友回復:
另一種解決方案:我正在使用您將引數傳遞給的 sub:
- 找到的價值
- 范圍查看和清除內容
- 低于要清除的找到值的行數。
此外,我正在從范圍的底部到頂部查看-否則可以清除包含要找到的字串的單元格-然后以下值將不會被清除:
Option Explicit
'>>> example of how to call the findAndRemove-Sub <<<
Public Sub test_FindAndRemove()
With ActiveSheet ' adjust this to your needs
findAndRemove .Range("Q5"), .Range("A7:H20"), 3
End With
End Sub
'>>>> this is the sub that is doing the work <<<<<
Public Sub findAndRemove(strFind As String, _
rgLookup As Range, _
cntDeleteRowsBelow As Long)
Dim i As Long, c As Range
'start from the bottom and go up
'otherwise you could delete further strFind-cells unintentionally
For i = rgLookup.Rows.Count To 1 Step -1
For Each c In rgLookup.Rows(i).Cells
If c.Value = strFind Then
'ATTENTION:
'at this point there is no check if below row contains the strFind-value!
c.Resize(cntDeleteRowsBelow 1).Clear
End If
Next
Next
End Sub
uj5u.com熱心網友回復:
您可以只使用cell.Clear,或者如果您希望清除單元格并且下面的 3 個使用類似這樣的東西
For i = 0 To 3
cell.Offset(i, 0).Clear
Next
uj5u.com熱心網友回復:
我認為您的意思是“回傳該單元格的地址”,不是嗎?Debug.Print(cell.Address)會給你這個資訊。但你在這里實際上并不需要它。而不是Range("A7:H20").Clear用= number of rows 你想連同自己一起清除cell.Resize(1 i, 1).Clear(不需要回圈)。icell
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/465702.html
