2015-04-01 91 views
1

我目前工作的一个报告,对原产于Infoview中(SAP Business Objects公司)所有细胞过去活跃小区的小区找到特定的文本,多次出现,并且在包含特定文本

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

像帖子的瓦片可能表明我想找到一个具有特定文本的单元格。它有多次出现,并且我想要在所有这些实例中通过先前选择的单元格。

我可以按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 

但我不能纳入二合一宏给我,我上面描述的结果。

先前选定的单元格包含一个公式,其中包含索引(匹配)以及对与“特定文本”相同行上的单元格的引用。 在我看来,这种做东西的方式为动态单元格引用等节省了很多麻烦。

我希望你能帮助

回答

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 
+0

它,它几乎是正确的,我相信!谢谢。但是,我在单元格S11 - >(Range(“S11”)中粘贴一个公式,选择 ActiveCell.FormulaR1C1 = _ “= IF(RC [-9] = 1,”“”“,INDEX('source samenwerken '!R1C13:R60000C13,MATCH(RC [-16],'Source samenwerken'!R1C4:R60000C4,0)+ COUNTIF('Source samenwerken'!R1C4:R60000C4,RC [-16]) - 1))“ ) 。我想在每个包含[ö]的单元格中使用该公式,与[ö]所在的行号对应,因为我可以轻松地复制和粘贴单元格S11,但是如何将Cell S11作为“PasteValue”使用? – LauMuaL 2015-04-02 09:25:43

+0

'set rng = cells.find()'将找到一个带有“[ö]”的单元格。 'rng.value = PasteValue'会将“PasteValue”放入该单元格中。由于您在公式中粘贴,因此需要将其更改为'rng.formula = PasteValue'。您需要将“PasteValue”设置为您需要粘贴的任何内容。 – FreeMan 2015-04-02 10:56:34

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

相关问题