2015-02-05 9 views
0

매크로 코드에서 도움이 필요합니다. 이 열의 행은 날마다 바뀔 수 있지만 A-E 열에는 데이터가 있습니다. C열의 최소값과 D 열의 최대 값을 계산하는 방법이 필요합니다. 그런 다음 F 열에서 C-E 열의 값과 최소/최대 값과의 관계에 따라 점수를 지정하고 싶습니다.상대 및 절대 참조

예를 들어, 29 행의 데이터와 표제가있는 경우 셀 C31의 열은 D & E. 필드에 비슷한 설정으로 "= MIN (C2 : C30)"이라는 수식을 갖습니다. 그런 다음 F2 셀에 수식 "= 0.25 * (1/(C2/$ C $ 31)) + 0.25 * D2/$ D $ 31 + 0.5 * E2/$ E $ 31"

매크로 코드가 R [] C [] 형식을 사용하고 행 번호가 정적이 아닌 경우 절대 셀 값을 어떻게 처리합니까?

+0

매크로에 대한 상대 참조 사용 알고 있습니까? – pnuts

답변

0

그래서 여기에 내가 함께 뒤섞인 코드가 있습니다. 그것은 예쁘지 않지만 작동하고 그것이 무엇을 해야하는지 않습니다. 커뮤니티의 서식 도움말은 크게 감사하겠습니다.

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 

나는이 문제가 비슷한 사람에게 도움이되기를 바랍니다.