Private Sub CommandButton1_Click()
Dim year As String
year = TextBox1.Text
'闰年
Dim MerageMatrixLeapYear(11) As String '对应规则
MerageMatrixLeapYear(0) = "01001017"
MerageMatrixLeapYear(1) = "02033049"
MerageMatrixLeapYear(2) = "03065081"
MerageMatrixLeapYear(3) = "04097113"
MerageMatrixLeapYear(4) = "05129145"
MerageMatrixLeapYear(5) = "06145161"
MerageMatrixLeapYear(6) = "07177193"
MerageMatrixLeapYear(7) = "08209225"
MerageMatrixLeapYear(8) = "09241257"
MerageMatrixLeapYear(9) = "10273289"
MerageMatrixLeapYear(10) = "11305321"
MerageMatrixLeapYear(11) = "12337353"
'非闰年
Dim MerageMatrixNonLeapYear(11) As String
MerageMatrixNonLeapYear(0) = "01001017"
MerageMatrixNonLeapYear(1) = "02033049"
MerageMatrixNonLeapYear(2) = "03065081"
MerageMatrixNonLeapYear(3) = "04097113"
MerageMatrixNonLeapYear(4) = "05113129"
MerageMatrixNonLeapYear(5) = "06145161"
MerageMatrixNonLeapYear(6) = "07177193"
MerageMatrixNonLeapYear(7) = "08209225"
MerageMatrixNonLeapYear(8) = "09241257"
MerageMatrixNonLeapYear(9) = "10273289"
MerageMatrixNonLeapYear(10) = "11305321"
MerageMatrixNonLeapYear(11) = "12337353"
'Get the focused map from MapDocument
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Dim pMap As IMap
Set pMap = pMxDoc.FocusMap
'Create a RasterBandCollection from the raster layers in ArcMap
Dim pEnumLayers As IEnumLayer
Dim pLayer As ILayer
Dim pRasLayer As IRasterLayer
Dim pRaster As IRaster
Dim pRBCollTmp As IRasterBandCollection
Dim pRBand As IRasterBand
Dim pRBColl As IRasterBandCollection
Dim month As Integer
Dim dataname As String
Dim data1 As String
Dim data2 As String
'Create a RasterLocalOp operator
Dim pLocalOp As ILocalOp
Set pLocalOp = New RasterLocalOp
'Set output workspace
Dim pEnv As IRasterAnalysisEnvironment
Set pEnv = pLocalOp
Dim pWS As IWorkspace
Dim pWSF As IWorkspaceFactory
Set pWSF = New RasterWorkspaceFactory
Set pWS = pWSF.OpenFromFile("c:\temp", 0) '结果数据的输出目录
Set pEnv.OutWorkspace = pWS
' Dim pRWS As IRasterWorkspace2 ' 用来create rasterdataset
For month = 0 To 11
Set pEnumLayers = pMap.Layers
Set pRBColl = New Raster
dataname = Mid(MerageMatrixLeapYear(month), 1, 2)
data1 = Mid(MerageMatrixLeapYear(month), 3, 3)
data2 = Mid(MerageMatrixLeapYear(month), 6, 3)
'Loop through all layers in ArcMap
Set pLayer = pEnumLayers.Next
Do Until pLayer Is Nothing
If TypeOf pLayer Is IRasterLayer Then
If pLayer.Name = "n" + year + data1 + ".tif" Or pLayer.Name = "n" + year + data2 + ".tif" Then
Set pRasLayer = pLayer
Set pRaster = pRasLayer.Raster
Set pRBCollTmp = pRaster
Set pRBand = pRBCollTmp.Item(0) '保证是单波段的才可以
pRBColl.AppendBand pRBand
End If
End If
Set pLayer = pEnumLayers.Next
Loop
'Compute cell-by-cell (local) statistics
Dim pOutRaster As IRaster
Set pOutRaster = pLocalOp.LocalStatistics(pRBColl, esriGeoAnalysisStatsMaximum)
'Add output into ArcMap as a raster layer
Dim pOutRasLayer As IRasterLayer
Set pOutRasLayer = New RasterLayer
pOutRasLayer.Name = dataname '给layer赋名字
pOutRasLayer.CreateFromRaster pOutRaster
pMap.AddLayer pOutRasLayer
Next month
MsgBox "生成完毕!"
End Sub