0

我有疑问如何改进我的宏以通过 A 列,当它找到空白单元格时,它会在那里输入月份名称?我在下面尝试了类似的东西,但它只能用于Range("A1").value而不是Cells(Rows.Count, "A").End(xlUp).Row

 Sub actual_month()
'fill month col

Dim arrMonths() As String
ReDim arrMonths(1 To 12)

Application.DisplayAlerts = False
arrMonths(1) = "JAN"
arrMonths(2) = "FEB"
arrMonths(3) = "MAR"
arrMonths(4) = "APR"
arrMonths(5) = "MAY"
arrMonths(6) = "JUNE"
arrMonths(7) = "JULY"
arrMonths(8) = "AUG"
arrMonths(9) = "SEP"
arrMonths(10) = "OCT"
arrMonths(11) = "NOV"
arrMonths(12) = "DEC"

Workbooks("UAC_report_p.xlsb").Activate

Sheets("SLA Calculation").Select


For Each Cell In ActiveSheet.UsedRange.Cells
      'do some stuff
   Next



For i = 1 To 12



Cells(Rows.Count, "A").End(xlUp).Row = Month(Date)
  If Cells(Rows.Count, "A").End(xlUp).Row.Value = Month(Date) Then _
     Cells(Rows.Count, "A").End(xlUp).Row.Value = arrMonths(Cells(Rows.Count, "A").End(xlUp).Row.Value)
   Next i




Application.DisplayAlerts = True
End Sub
4

1 回答 1

1

假设您使用的是当月,您可以这样做

Public Sub test()
    Dim endrow As Long
    Dim ws As Worksheet
    Dim Col As Long
    Dim arrMonths(1 To 12)

    arrMonths(1) = "JAN"
    arrMonths(2) = "FEB"
    arrMonths(3) = "MAR"
    arrMonths(4) = "APR"
    arrMonths(5) = "MAY"
    arrMonths(6) = "JUNE"
    arrMonths(7) = "JULY"
    arrMonths(8) = "AUG"
    arrMonths(9) = "SEP"
    arrMonths(10) = "OCT"
    arrMonths(11) = "NOV"
    arrMonths(12) = "DEC"

    Set ws = Sheet4

    'set column to "A"
    Col = 1


    endrow = ws.Cells(ws.Rows.Count, Col).End(xlUp).Row + 1

    If ws.Cells(endrow, Col).Value = "" Then _
    ws.Cells(endrow, Col).Value = arrMonths(Month(Now))
End Sub

编辑

更好的方法是根本不使用数组并使用LanguageID来获取特定语言中月份名称的等价物。有关 LanguageID 的完整列表,请参阅THIS The LanguageIDfor English_United_Statesis409

试试这个代码

Public Sub test()
    Dim ws As Worksheet
    Dim Lrow As Long, Col As Long
    Dim sMonth As String

    Set ws = Sheet4

    '~~> Not sure if Czech has same names as `Text` / `Today`. 
    '~~> If not then replace `Text` and `Today` with their respective equivalents.
    sMonth = Application.Evaluate("=TEXT(TODAY(),""[$-409]MMM"")")

    With ws
        '~~> Set column to "A"
        Col = 1

        Lrow = .Cells(.Rows.Count, Col).End(xlUp).Row + 1

        If .Cells(Lrow, Col).Value = "" Then _
        .Cells(Lrow, Col).Value = sMonth
    End With
End Sub
于 2013-11-08T08:14:38.723 回答