|
新买了本VBA实战技巧精粹(修订版)在第一章VBA基础2-4专题卡住了,求大神帮助
'声明全局变量llastrow
Dim llastrow As Long
'主进程
Sub mymain()
Dim rng As Range
llastrow = Range("A" & Cells.Rows.Count).End(xlUp).Row
Range("D3:E" & llastrow).Clear
'调用“输入加班系数”子进程
Call inputextra
'调用“计算加班费”子进程
Call calculateextra
'调用“标记最大加班费”子进程
Call SetFormat(rng)
End Sub
'加班系数计算公式
Function getextra(num As Single) As Single
getextra = Range("H" & (num + 2)).Value
End Function
'输入加班系数
Sub inputextra()
Dim i As Single
For i = 3 To llastrow
Range("D" & i) = getextra(Cells(i, 3).Value)
Next i
End Sub
'计算加班费
Sub calculateextra()
Dim j As Single
For j = 3 To llastrow
Cells(j, 5) = Cells(j, 3) * Cells(j, 4) * Cells(j, 2)
Next j
End Sub
'标记最大加班费
Sub SetFormat(rnga As Range)
With rnga
.NumberFormat = "0.00" '对象变量或With块变量未设置
.HorizontalAlignment = xlCenter '对象变量或With块变量未设置
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=RC5=max(C5)"
.FormatConditions(1).Interior.ColorIndex = 4
End With
End Sub
|
|