在VBA中使用月份功能

时间:2017-09-12 15:20:04

标签: excel vba excel-vba

我有两张纸,sheet1是报告,而sheet2是结果。在结果表中,我在A列中表示为“Jan”,“Feb”等的月份,依此类推。在我的报告单中,我有几个月的数字。

我的要求是,检查报告表的W列是否与结果表的A列相同。如果它们相同,那么我必须在结果表中打印计数结果。

我有以下代码返回月份

Sub month()
Dim ws As Worksheet
Dim i As Long
Dim totalrows As Long
Set ws = Sheets("Report")
totalrows = ws.Cells(Rows.count, "A").End(xlUp).Row
MsgBox (totalrows & "   Check")
      For i = 5 To totalrows
       ' get the current week in column 23
         Cells(i, 23).Value = Month(Now)
      Next
End Sub

以下代码,检查两张纸中的当前月份并打印计数值。有人可以提供帮助,我怎么能说VBA包含9的列W与包含9月的A列相同。 ?在A列中输入的月份是手动输入。

Sub result()
 Dim i As Long, j As Long, cntR As Long, CntS As Long, cntT As Long, cntU As Long, sht As Worksheet
    Set sht = Sheets("Result")
    Sheets("Report").Select
    For i = 2 To WorksheetFunction.CountA(sht.Columns(1))
    ' get the current week from column A of the sheet result
        If sht.Range("A" & i) = Val(Month(Now)) Then Exit For
    Next i

    sht.Range("C" & i & ":" & "J" & i).ClearContents
    For j = 5 To WorksheetFunction.CountA(Columns("W"))
    ' if column A and column W are same, then count 1, in column A, R; S, T, U
        If sht.Range("A" & i) = Range("W" & j) Then
            If Range("R" & j) = "1" Then cntR = cntR + 1
            If Range("S" & j) = "1" Then CntS = CntS + 1
            If Range("T" & j) = "1" Then cntT = cntT + 1
            If Range("U" & j) = "1" Then cntU = cntU + 1
        End If
    Next j
   ' print the count values in the respective column
    If cntR <> 0 Then sht.Range("C" & i) = cntR
    If CntS <> 0 Then sht.Range("D" & i) = CntS
    If cntT <> 0 Then sht.Range("E" & i) = cntT
    If cntU <> 0 Then sht.Range("F" & i) = cntU
    If cntR + CntS + cntT + cntU <> 0 Then
        sht.Range("G" & i) = cntR / (cntR + CntS + cntT + cntU)
        sht.Range("H" & i) = CntS / (cntR + CntS + cntT + cntU)
        sht.Range("I" & i) = cntT / (cntR + CntS + cntT + cntU)
        sht.Range("J" & i) = cntU / (cntR + CntS + cntT + cntU)
    End If
End Sub

0 个答案:

没有答案