VBA Excel单词search和复制公式

我正在寻找Excel的VBAmacros,它可以在列A中检测单词“mean”。之后它将复制带有C到J公式的黄色行。公式计算在最后“意味着”到下一个=平均(C1323:C1437)

每六分之一之后还有需要区域和150个复制两行之后的意思,I和J需要更改。 因此,在这种情况下,I和J将引用单元格A1441(= G1439 / C1439 * $ A $ 1441)直到文件结尾。

Excel示例

我不太确定这是否容易,但我完全被挑战。 我会非常感激的帮助。

Sub Makro1() ' ' Makro1 Makro ' ' Tastenkombination: Strg+q strSearchWord = "Mean" i = Application.WorksheetFunction.CountIf(Range("A:A"), strSearchWord) Y = 2 For x = i To 0 i = Application.WorksheetFunction.Match(strSuchWort, Range("A:A"), 0) Range("C" & i).Select Application.CutCopyMode = False ActiveCell.FormulaR1C1 = "=AVERAGE(R[-147]C:R[-1]C)" ' that's still wrong, should be something like iy? Selection.AutoFill Destination:=Range("C" & i:"J" & i), Type:=xlFillDefault Range("CY:JY").Select i = Y 'for each fifth i 'Range("A" & i + 3).Select ' ActiveCell.FormulaR1C1 = "=RC[-2]/RC[-6]*R2159C1" Next x End Sub 

这还是错的,但是我的初稿。

@stucharo的区域更正很难描述我已经添加了更好的图片与公式。 我现在知道这是可以理解的 在这里输入图像说明

如果你的行ActiveCell.FormulaR1C1 = "=AVERAGE(R[-147]C:R[-1]C)"需要改变每行之间的行数,那么你需要添加一个variables, 。 此外,只要将string写入单元格值( ActiveCell.Value )意味着当您单击工作簿中的单元格(它将突出显示范围等)时,您会看到它被写为formaula。 你可以尝试replace它:

 ActiveCell.Value = "=AVERAGE(R[" & i - Y & "]C:R[-1]C)" 

虽然由于我看不到您的工作表的第一行,我不确定每次都会给你正确的行数范围。

如果你的行号很可能会改变,而且你每次都复制相同数量的列,那么把公式直接写入循环中的单元格,而不是明确地复制它就可以。

在每第六个“意思”之后添加文本将要求您保持到目前为止已经通过了多less手段的计数。 这可以通过增加一个计数器variables来完成,使用Mod操作符会告诉你除法后的余数。 因此numberOfMeans Mod 6会给你余数除以6,当它等于零时,你知道你有6的倍数。

我试图捕获所有这些到下面的代码…..

 Sub Test() Application.ScreenUpdating = False Dim startRow As Integer startRow = 2 Dim endrow As Integer endrow = Range("A2").End(xlDown).row Dim lastMeanRow As Integer lastMeanRow = startRow - 1 Dim areaRow as Integer areaRow = lastMeanRow + 3 Dim meanCounter As Integer meanCounter = 0 Dim avgColHeight As Integer Dim col As Integer Dim row As Integer 'Check each row in the sheet For row = startRow To endrow 'Cols i and j in every row need to be modified For col = 9 To 10 Cells(row, col).Value = "=RC[-2]/RC[-6]*R" & areaRow & "C1" Next col 'If column 1 of that row contains "mean" then If Cells(row, 1).Value = "mean" Then 'Calculate the column height to average over.... avgColHeight = row - lastMeanRow - 1 '...and loop through each of the columns.... '(including i and j to add average) For col = 3 To 10 '....inserting the averaging formula. Cells(row, col).Value = "=AVERAGE(R[-" & avgColHeight & "]C:R[-1]C)" Next col 'Then increment the counter to keep track of the number of means meanCounter = meanCounter + 1 'If the number of means is a multiple of 6 then If (meanCounter Mod 6 = 0) Then 'insert the "Area" and "150" strings Cells(row + 2, 1).Value = "Area" Cells(row + 3, 1).Value = "150" areaRow = row + 3 End If 'Finally change the lastMeanRow to the mean row we have just processed. lastMeanRow = row End If 'Do it again until we reach the end of the data Next row Application.ScreenUpdating = True End Sub 

我也注意到你的观点,定期更新区域的价值。 如上所述,通过编程方式编写这些代码将允许您在“区域”的值和更改时添加一些逻辑。

你显然有一个长长的数据列表,并希望自动创build你所描述的行和公式。

有可能写VBA扫描数据和修改公式等,但首先我会质疑,如果这是最好的办法给你你所需要的。

Excel有一个称为“数据透视表”的function,它基本上允许您汇总列表中的数据。

例如,如果列表中的每个城市在世界上都有一行,并且给了城市中的人口,并且列给出了它所在的国家。可以使用数据透视表来创build一个国家城市的平均人口。 我怀疑你正在做这种事情。

如果你不知道数据透视表,你应该了解他们。 看到这里

在你的情况下,你的平均行是summeriseing上面的行中的数据。 要使用数据透视表,您必须有一个列来定义每行所在的组。您的数据透视表会将该列作为行摘要起诉,然后您将为所有其他列创build平均值。

@Nathalie。 不知道更多,很难帮助。 例如,是否已经插入了意味着文本的数据。 它看起来像列A有一个数字代表组内的行号(这可以用公式来创build你需要的数据透视表的“组名”列)。

您可以通过以下方式获取数据透视表来进行区域调整:

  1. 创build一个包含公式的新的一组列,这些公式会导致列C到J中的值被复制,除非它是第六组数据,在这种情况下,您可以相应地调整C到J中的值。

    1. 您可能需要引入以下列:

    2. A.给出“小组名称”B.给出一个它所在的小组的数量,所以每6天可以做一次你需要的调整。

4通过使用数据透视表和基本技巧,你会发现它易于更新刷新数据,如果你需要的话。