Excel宏遍历范围直到找到值,填充范围下面找到的公式单元格

时间:2016-03-15 10:09:39

标签: excel vba macros

我有一张大桌子,有时有几百行 此表由另一个导出到excel的应用程序生成。

一栏的标题为“调整后的价格” 我希望此列中的所有行都包含一个公式(目前它们都是0)。

我想自动执行此过程,因为表会一直重新生成。

此列可能并非始终位于同一位置。 所以我需要一个可以找到此列的宏(“调整价格”)然后用公式填充该列中的所有单元格(当然第一行除外)。

可以这样做吗?

提前致谢:)

1 个答案:

答案 0 :(得分:1)

你的作业是弄清楚如何插上它!

Option Explicit

Sub setAdjustedPrice()

    Dim column As Integer
    Dim adjustedPriceColumn As String
    Dim found As Boolean
    Dim rowCount As Long

    column = 1
    rowCount = 1

    Do While Range(FncAlphaCon(column) & rowCount).Value <> "" And found = False

        If (Range(FncAlphaCon(column) & rowCount).Value = "Adjusted Price") Then

            found = True
            adjustedPriceColumn = FncAlphaCon(column)

        Else

            column = column + 1

        End If

    Loop

    If found = True Then

        Do While rowCount < ActiveSheet.UsedRange.Rows.count

            rowCount = rowCount + 1
            Range(adjustedPriceColumn & rowCount) = "YOUR FORMULA"

        Loop

    Else

        MsgBox ("'Adjusted Price' column not found, cannot continue.")

    End If

End Sub

Private Function FncAlphaCon(aNumber As Integer) As String
' Fixed version 27/10/2011

    Dim letterArray As String
    Dim iterations As Integer

    letterArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

        If aNumber <= 26 Then

            FncAlphaCon = (Mid$(letterArray, aNumber, 1))

        Else

            If aNumber Mod 26 = 0 Then

                iterations = Int(aNumber / 26)
                FncAlphaCon = (Mid$(letterArray, iterations - 1, 1)) & (Mid$(letterArray, 26, 1))

            Else

                'we deliberately round down using 'Int' as anything with decimal places is not a full iteration.
                iterations = Int(aNumber / 26)
                FncAlphaCon = (Mid$(letterArray, iterations, 1)) & (Mid$(letterArray, (aNumber - (26 * iterations)), 1))

            End If

        End If

End Function