|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
代码如下。。。。
Public csdate
Sub 指标查询统计()
'年度累加函数
Application.ScreenUpdating = False
Range("C3:i13").ClearContents
Dim i As Long, j As Long, kk As Long
Set ws = ThisWorkbook.Sheets("基础表")
Set ws1 = ThisWorkbook.Sheets("统计表")
arr1 = ws.Cells(1, 1).CurrentRegion.Value
rq = Format(ws1.Range("d1").Value, "yyyy年m月")
x = Application.Match(CStr(ws1.Range("d1")), Application.Index(arr1, , 1), 0)
If IsError(x) Then MsgBox "没有此月份": Exit Sub
dyzb = Get_ydzh(arr1, rq)
syzb = Get_ydzh(arr1, rq, 1)
tqdyzh = Get_ydzh(arr1, rq, 2)
dylj = Get_ndzb(rq)
qilj = Get_ndzb(rq, 1)
dybj = Get_jdzb(rq)
qibj = Get_jdzb(rq, 1)
For i = 2 To UBound(arr1, 2)
ws1.Cells(i + 1, 3) = dyzb(i)
ws1.Cells(i + 1, 4) = syzb(i)
ws1.Cells(i + 1, 5) = tqdyzh(i)
ws1.Cells(i + 1, 6) = dybj(i)
ws1.Cells(i + 1, 7) = qibj(i)
ws1.Cells(i + 1, 8) = dylj(i) 'Get_ndzb(i)
ws1.Cells(i + 1, 9) = qilj(i) 'Get_ndzb(i)
Next i
Application.ScreenUpdating = True
End Sub
Public Function Get_ydzh(arr1, ByVal indate As Variant, Optional inint As Integer = 0)
'按日期查询取得当月、上月、上年当月数据函数
'On Error Resume Next
indate = Format(indate, "yyyy年m月")
If inint = 1 Then
indate = Format(DateAdd("m", -1, indate), "yyyy年m月")
ElseIf inint = 2 Then
indate = Format(DateAdd("yyyy", -1, indate), "yyyy年m月")
Else
indate = Format(indate, "yyyy年m月")
End If
kk = UBound(arr1, 2)
For i = 1 To UBound(arr1)
If Format(arr1(i, 1), "yyyy年m月") = indate Then
ReDim arrtemp(1 To kk)
For s = 1 To kk
arrtemp(s) = arr1(i, s) 'ws.Cells(i, s).Value
Next
Exit For
End If
Next
Get_ydzh = arrtemp
End Function
Public Function Get_ndzb(ByVal indate As Variant, Optional inint As Integer = 0)
indate1 = Format(indate, "yyyy年") & "1月"
indate = Format(indate, "yyyy年m月")
t = DateDiff("m", indate1, indate)
If inint = 1 Then
indate2 = Format(DateAdd("yyyy", -1, indate), "yyyy年m月")
indate3 = Format(DateAdd("m", -t, indate2), "yyyy年m月")
Else
indate3 = Format(indate, "yyyy年") & "1月"
End If
Set ws = ThisWorkbook.Sheets("基础表")
arr1 = ws.Cells(1, 1).CurrentRegion.Value
kk = ws.UsedRange.Columns.Count
For i = 1 To UBound(arr1)
If Format(arr1(i, 1), "yyyy年m月") = indate3 Then
data = ws.Range(ws.Cells(i, 1), ws.Cells(i + t, kk))
Exit For
End If
Next i
ReDim sum1(LBound(data, 2) To UBound(data, 2))
' 遍历每一列并累加
For j = LBound(data, 2) To UBound(data, 2)
For i = LBound(data, 1) To UBound(data, 1)
sum1(j) = (sum1(j)) + (data(i, j))
Next i
Next j
Get_ndzb = sum1
End Function
Public Function Get_jdzb(ByVal indate As Variant, Optional inint As Integer = 0)
m = Format(indate, "m")
m = Application.Match(1 * m, [{1,4,7,10}])
indate1 = Format(indate, "yyyy年") & Application.Index([{1,4,7,10}], m) & "月"
indate = Format(indate, "yyyy年m月")
t = DateDiff("m", indate1, indate)
If inint = 1 Then
indate2 = Format(DateAdd("m", -3, indate), "yyyy年m月")
m = Format(indate2, "m")
m = Application.Match(1 * m, [{1,4,7,10}])
indate3 = Format(indate2, "yyyy年") & Application.Index([{1,4,7,10}], m) & "月"
t = 2
Else
indate3 = Format(indate, "yyyy年") & Application.Index([{1,4,7,10}], m) & "月"
End If
Set ws = ThisWorkbook.Sheets("基础表")
arr1 = ws.Cells(1, 1).CurrentRegion.Value
kk = ws.UsedRange.Columns.Count
For i = 1 To UBound(arr1)
If Format(arr1(i, 1), "yyyy年m月") = indate3 Then
data = ws.Range(ws.Cells(i, 1), ws.Cells(i + t, kk))
Exit For
End If
Next i
ReDim sum1(LBound(data, 2) To UBound(data, 2))
' 遍历每一列并累加
For j = LBound(data, 2) To UBound(data, 2)
For i = LBound(data, 1) To UBound(data, 1)
sum1(j) = (sum1(j)) + (data(i, j))
Next i
Next j
Get_jdzb = sum1
End Function
|
评分
-
2
查看全部评分
-
|