Excel縱橫字段查找最大、最小值和統(tǒng)計(jì)個(gè)數(shù)函數(shù)
修法布施得聰明智慧,多分享讓生活更美好。上次分享了《Excel怎樣查找表格縱橫向兩值A(chǔ)、B值相應(yīng)值》,介紹了縱橫字段查表處理方法,現(xiàn)分享縱橫字段查找最大、最小和統(tǒng)計(jì)個(gè)數(shù)自定義函數(shù)處理方法,來提高工作效
修法布施得聰明智慧,多分享讓生活更美好。上次分享了《Excel怎樣查找表格縱橫向兩值A(chǔ)、B值相應(yīng)值》,介紹了縱橫字段查表處理方法,現(xiàn)分享縱橫字段查找最大、最小和統(tǒng)計(jì)個(gè)數(shù)自定義函數(shù)處理方法,來提高工作效率。
步驟一:新建宏模塊
首先打開Microsoft Office Excel 2007,新建文檔并保存文件名《Excel縱橫字段查找最大、最小值和統(tǒng)計(jì)個(gè)數(shù)函數(shù).xlsm》(演示文件,下面代碼復(fù)制到能運(yùn)行宏的工作簿都可以)如圖所示。
步驟二:插入模塊
然后按下快捷鍵ALT F11打開VBE(宏)編輯界面,然后點(diǎn)菜單欄【插入】下拉中列表中點(diǎn)【模塊(M)】如圖所示。
步驟三:插入自定義函數(shù)代碼
然后插入了一個(gè)模塊1,在代碼框中復(fù)制如下代碼:
```vba
Function COUNTAYX(ByVal y As Range, ByVal x As Range, ByVal cb As Range)
Dim r As Long, c As Long, i As Long, k1 As Long, k2 As Long
r
c
k1 0
For i 2 To c
If x.Text cb.Cells(1, i).Text Then
k1 i
Exit For
End If
Next i
If k1 > 0 Then
k2 0
For i 2 To r
If y.Text cb.Cells(i, 1).Text And cb.Cells(i, k1).Text <> "" Then
k2 k2 1
End If
Next i
COUNTAYX k2
Else
COUNTAYX "橫向字段無存!"
End If
End Function
Function MAXYX(ByVal y As Range, ByVal x As Range, ByVal cb As Range)
Dim r As Long, c As Long, i As Long, k1 As Long, k2 As Long
r
c
k1 0
For i 2 To c
If x.Text cb.Cells(1, i).Text Then
k1 i
Exit For
End If
Next i
If k1 > 0 Then
k2 0
For i 2 To r
If y.Text cb.Cells(i, 1).Text And cb.Cells(i, k1).Text <> "" Then
If k2 0 Then
MAXYX cb.Cells(i, k1).Value
Else
If MAXYX < cb.Cells(i, k1).Value Then
MAXYX cb.Cells(i, k1).Value
End If
End If
End If
Next