答案 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列中的单元格,这些单元格带有值。 接下来的步骤:
答案 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
有许多方法可以满足您的需求,但是这些代码(包含某些版本)可以在各种应用程序中重复使用,而不仅仅是宏。