簡體   English   中英

使用宏更改單元格值

[英]Changing cell value using macro

我正在嘗試使用Excel宏更改單元格值。

例如,更改此組:

Codes:
CareBears
Catpaws
CareBears
Catpaws
CareBears
Doghound
Catpaws
Doghound

變成:

Codes:
Bear
Cat
Bear
Cat
Bear
Dog
Cat
Dog

我使用宏記錄成功地自動轉換了它們,但是當數據切換位置或有另一行數據時,該數據將不會被讀取。

我正在尋求您的協助。

在此處輸入圖片說明

如果您不需要替換值,並且每個人都只需要處理熊,貓和狗,那么您可以在B列中添加一個公式,前提是您的代碼在A列中。它雖然不漂亮,但是可以工作。

將其放入B2,然后復制,並將您的代碼放在A列中。

= IF(NOT(ISERROR(SEARCH(“ Bear”,A2,1))),“ Bear”,IF(NOT(ISERROR(SEARCH(“ Dog”,A2,1))),“ Dog”,IF(NOT (ISERROR(SEARCH(“ Cat”,A2,1))),“ Cat”,“?”)))

我贊賞這不能回答“宏觀”問題。 如果您有DogBear之類的值,請不要嘗試使用它;-)

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM