在单元格中查找内容然后替换同一行中其他单元格中的内容

时间:2018-01-09 16:49:34

标签: excel vba excel-vba

基本上我正按照标题中的说法进行操作。

我需要在

中找到NameB

示例:

enter image description here

然后将“NOT YET”改为“DONE”

3 个答案:

答案 0 :(得分:0)

试试这件:

Sub TestMe()    
    Dim myCell  As Range        
    For Each myCell In Columns("C").SpecialCells(2).Cells
        If myCell <> Range("C1") Then myCell = Range("C1")
    Next myCell    
End Sub

这是它的工作原理:

  • SpecialCells(2).Cells仅获取C列中的单元格,这些单元格带有值。
  • 然后它循环遍历它们并更改它们,这与C1不同。

接下来的步骤:

  • 确保为StackOverflow中的下一个问题显示到目前为止您尝试过的内容。

答案 1 :(得分:0)

ReplaceContent是被调用的主要子元素,这就是它被标记为Public的原因。其他的是Private,因为它们只需要在模块中调用。

Public Sub ReplaceContent()
    UpdateContent "NameB", 2
End Sub

UpdateContent提供了查找username的名称,以及您希望将信息填充到updateColumnOffset中的列数,以及完成消息{{1}使用默认值设置。

completedMessage

Private Sub UpdateContent(ByVal username As String, ByVal updateColumnOffset As Long, Optional ByVal completedMessage = "DONE") Dim users As Range Set users = GetCellsPopulatedWith(username) Dim user As Range For Each user In users If user.Value2 = username Then user.Offset(0, updateColumnOffset).Value2 = completedMessage End If Next End Sub 提供了要查找的名称并搜索该名称,直到找到找到的第一个单元格并停止查看GetCellsPopulatedWith。结果与Do While ... Loop

一起添加
Union

Private Function GetCellsPopulatedWith(ByVal username As String) As Range Dim foundCell As Range Set foundCell = FindUserName(username, Sheet1.Range("A1")) If Not foundCell Is Nothing Then Set GetCellsPopulatedWith = foundCell Dim startAddress As String startAddress = foundCell.Address Dim previousCell As Range Set previousCell = foundCell Set foundCell = FindUserName(username, previousCell) Do While foundCell.Address <> startAddress Set GetCellsPopulatedWith = Union(GetCellsPopulatedWith, foundCell) Set previousCell = foundCell Set foundCell = FindUserName(username, previousCell) Debug.Print previousCell.Address, foundCell.Address Loop End If End Function 是找到具有该名称的下一个单元格的位置。在此处进行更改将使结果保持一致。

FindUserName

答案 2 :(得分:0)

如果您使用的是Macros,则此代码可能会对您有所帮助:

Dim value As String
Dim i As Integer
value = Range("A1").value
i = 1
Do While value <> ""

    If value = "NAMEB" Then
        Range("C" & i).value = "DONE"
    End If
    i = i + 1
    value = Range("A" & i).value
Loop

您可以通过将其转换为函数并实现参数来增强它,例如:

Function SetResult(ByVal nameValue As String, ByVal resultValue As String)
    Dim value As String
    Dim i As Integer
    value = Range("A1").value
    i = 1
    Do While value <> ""
        If value = nameValue Then
            Range("C" & i).value = resultValue
        End If
        i = i + 1
        value = Range("A" & i).value
    Loop
    SetResult = "Done"
End Sub

如果您在电子表格中添加了一个按钮,则可以这样使用:

Private Sub CommandButton1_Click()
    Dim nameVal, resultVal, result As String

    nameVal = InputBox("Please insert the value you want to look for in Column A")
    resultVal = InputBox("Please insert the value you want to set in Column C")

    result = SetResult(nameVal, resultVal)

End Sub

Function SetResult(ByVal nameValue As String, ByVal resultValue As String) As String
     Dim value As String
     Dim i As Integer
     value = Range("A1").value
     i = 1
     Do While value <> ""

     If value = nameValue Then
         Range("C" & i).value = resultValue
     End If
     i = i + 1
     value = Range("A" & i).value
     Loop
     SetResult = "Done"
End Function

许多方法可以满足您的需求,但是这些代码(包含某些版本)可以在各种应用程序中重复使用,而不仅仅是宏。

部分截图Set search for NAMEC in Column A Set value to "DONE BETTER" in Column C if criteria is matched in Column A Value changed