[英]Changing cell value using macro
我正在嘗試使用Excel宏更改單元格值。
例如,更改此組:
Codes:
CareBears
Catpaws
CareBears
Catpaws
CareBears
Doghound
Catpaws
Doghound
變成:
Codes:
Bear
Cat
Bear
Cat
Bear
Dog
Cat
Dog
我使用宏記錄成功地自動轉換了它們,但是當數據切換位置或有另一行數據時,該數據將不會被讀取。
我正在尋求您的協助。
Sub FindAndExecute()
'got the answer working with this help http://stackoverflow.com/questions/19504858/find-all-matches-in-workbook-using-excel-vba
Dim Sh As Worksheet
Dim Loc As Range
Dim Loc1 As Range
Dim Loc2 As Range
For Each Sh In ThisWorkbook.Worksheets
With Sh.UsedRange
Set Loc = .Cells.Find(What:="Carebears")
If Not Loc Is Nothing Then
Do Until Loc Is Nothing
Loc.Value = "Bear"
Set Loc = .FindNext(Loc)
Loop
End If
Set Loc1 = .Cells.Find(What:="Catpaws")
If Not Loc1 Is Nothing Then
Do Until Loc1 Is Nothing
Loc1.Value = "Cat"
Set Loc1 = .FindNext(Loc1)
Loop
End If
Set Loc2 = .Cells.Find(What:="Doghound")
If Not Loc2 Is Nothing Then
Do Until Loc2 Is Nothing
Loc2.Value = "Dog"
Set Loc2 = .FindNext(Loc2)
Loop
End If
End With
Set Loc = Nothing
Set Loc1 = Nothing
Set Loc2 = Nothing
Next
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.