【问题标题】:Relative and absolute references相对和绝对引用
【发布时间】:2015-02-10 04:26:02
【问题描述】:

我正在寻求宏代码方面的帮助。我在 A-E 列中有数据,尽管这些列中的行可能每天都在变化。我需要一种方法来计算 C 和 E 列中的最小值,以及 D 列中的最大值。然后在 F 列中,我想根据 C-E 列中的值及其与最小值/最大值的关系来分配分数。

例如,如果有 29 行数据和一个标题,则单元格 C31 将具有公式“=MIN(C2:C30)”,在 D 和 E 列中具有类似的设置。然后单元格 F2 将具有公式"=0.25*(1/(C2/$C$31))+0.25*D2/$D$31+0.5*E2/$E$31"。

当宏代码使用 R[]C[] 格式且行号不是静态时,如何处理绝对单元格值?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    这是我拼凑的代码。它并不漂亮,但它可以工作并且做它应该做的事情。来自社区的任何格式提示将不胜感激。

    Sub WeightedScore()
    '
    ' WeightedScore Macro
    '
    
    ' This will allow me to use a dynamic range of rows when sorting the table toward the end of the macro.
    
    Dim LastRow As Integer
    
    ' This part is just some asthetic cleanup from the report that is generated
    
    Rows("4:4").Select
    Selection.Delete Shift:=xlUp
    Columns("D:F").Select
    Selection.Delete Shift:=xlToLeft
    
    ' These are the weights to be applied to each factor
    
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "0.25"
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "0.25"
    Range("C1").Select
    Selection.FormulaR1C1 = "0.5"
    
    ' This part essentially counts the rows to be sorted in the table toward the end of the macro
    
    LastRow = Range("E3").CurrentRegion.Cells(Range("E3").CurrentRegion.Cells.Count).Row
    
    ' This code allows for the minimum and maximum values in the data column regardless of number of rows
    
    Range("C4").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    Selection.FormulaR1C1 = "=MIN(R4C3:R[-1]C)"
    Range("D4").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    Selection.FormulaR1C1 = "=MAX(R4C4:R[-1]C)"
    Range("E4").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    Selection.FormulaR1C1 = "=MIN(R4C5:R[-1]C)"
    
    ' This part is essentially admitting defeat, copying the min/max values below a variable number
    ' of rows, and then pasting them into static cells at the top of the sheet.
    
    Range("C3").Select
    Selection.End(xlDown).Select
    Selection.Copy
    Range("C2").PasteSpecial xlPasteValues
    Range("D3").Select
    Selection.End(xlDown).Select
    Selection.Copy
    Range("D2").PasteSpecial xlPasteValues
    Range("E3").Select
    Selection.End(xlDown).Select
    Selection.Copy
    Range("E2").PasteSpecial xlPasteValues
    
    ' This part names the "Score" column and applies the absolute weights and absolute min/max values
    ' to the relative cell values.
    
    Range("F3").Select
    Selection.FormulaR1C1 = "Score"
    Range("F4").Select
    Selection.FormulaR1C1 = _
        "=1/(RC[-3]/R2C3)*R1C1+RC[-2]/R2C4*R1C2+RC[-1]/R2C5*R1C3"
    Selection.NumberFormat = "#,##0.00"
    Selection.Copy
    ActiveCell.Offset(0, -1).Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(-1, 1).Range("A1").Select
    Range(Selection, Selection.End(xlUp)).Select
    ActiveSheet.Paste
    
    ' This is where the data is selected and sorted based on the "Score" value above. The LastRow
    ' function as described earlier allows for a dynamic range of rows.
    
    Range("A3:F" & LastRow).Select
    ActiveWorkbook.Worksheets("Reports").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Reports").Sort.SortFields.Add Key:=Range("F4:F" & LastRow _
        ), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Reports").Sort
        .SetRange Range("A3:F" & LastRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    ' This last part ends the macro with the highest "Score" selected
    
    Range("F4").Select
    End Sub
    

    我希望这对遇到类似问题的人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-06
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      • 2017-08-20
      相关资源
      最近更新 更多