在包含特定文本的所有单元格中查找具有特定文本,多次出现和粘贴活动单元格的单元格

时间:2015-04-01 15:01:59

标签: excel vba excel-vba find

我目前正在处理一份来自Infoview(SAP业务对象)的报告

这是一份每周提供有价值信息的报告,旨在提高对当前店铺绩效的认识。

就像帖子的图块可能显示我想要找到具有特定文本的单元格。它有多次出现,我希望在所有这些实例中超过先前选择的单元格。

我可以通过Ctrl-F达到相同的结果,"搜索全部" (对于"特定文本")和粘贴(先前选择的单元格)  (http://www.extendoffice.com/documents/excel/816-excel-select-cells-with-specific-text.html) 但我想自动化这个。

我想用:

    Cells.Find(What:="[ö]", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate

      Cells.FindNext(After:=ActiveCell).Activate

但是我不能将这两个结合在一个宏中,它给出了我上面描述的结果。

之前选择的单元格包含一个公式,其中包含索引(匹配)以及与"特定文本"在同一行上的单元格的引用。  在我看来,这种做事方式为动态单元格引用等方面省去了很多麻烦。

我希望你能帮忙

2 个答案:

答案 0 :(得分:0)

你的要求有点模糊,但我相信这会让你开始

Dim PasteValue as string 'this is what you're pasting in
Dim WS as Worksheet
Dim FirstCell as string
Dim rng as range

PasteValue = 'do something here to get your value

set rng = Cells.Find(What:="[ö]", LookIn:=xlFormulas, LookAt:= xlPart, _
    SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
    SearchFormat:=False)
while not rng is nothing    'make sure you found something
  if len(FirstCell) = 0 then
     firstcell = rng.address    'save this spot off so we don't keep looping
  end if
  rng.value = PasteValue
  'now find the next one
  set rng = Cells.Find(What:="[ö]", LookIn:=xlFormulas, LookAt:= xlPart, _
      SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
      SearchFormat:=False)
  if rng.address = FirstCell then  'back at the first cell
    set rng = nothing              'so get out of the loop
  endif
end while

答案 1 :(得分:0)

感谢上面的代码,它帮助了很多。

这是最终的代码,万一有人需要它。

If rng Is Nothing Then Exit Do
Loop While rng.Address <> strFirstAddress

特别有用。

Sub Fill_VCNC()
Dim formula_ö As String 
Dim rng As Range
Dim strFirstAddress As String  

With Range("S:S")
     Set rng = Cells.Find(What:="[ö]", LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False)
     If Not rng Is Nothing Then
         strFirstAddress = rng.Address
         Do
            rng.formula = formula_ö
            .NumberFormat = "0.00"
            Set rng = .FindNext(rng)
            If rng Is Nothing Then Exit Do
         Loop While rng.Address <> strFirstAddress
    End If
 End With
End Sub

(来源:https://social.msdn.microsoft.com/Forums/en-US/958fca4e-b19d-4b50-8235-e05adc7f25d5/loop-through-a-range-until-value-not-found?forum=exceldev