【发布时间】:2015-02-11 07:32:38
【问题描述】:
我需要一些指导。我有一组数据。在 A 列中有从 2015 年 1 月 1 日到 2015 年 12 月 31 日的日期。我想要实现的是创建一个新列并用基于日期的值填充它。
例如,如果列 A 第 1 行的日期为 01/16/2015,则新列的值将是 "Jan-15" 。基本上,如果日期介于 2015 年 1 月 16 日和 2015 年 2 月 15 日之间,则其值为“Jan-15”。如果它介于 2015 年 2 月 16 日和 2015 年 3 月 15 日之间,则其值为“Feb-15”。
这就是我现在所拥有的。问题是;新列中没有值输入。只是一个带有标题的空白列。
Sub Month()
Dim Found As Range
Dim LR As Long
Dim ws As Worksheet
Dim cell As Range
Dim a As Variant, v As Variant, num
Set ws = Sheets("PAYABLES - OUTFLOWS")
Set Found = ws.Rows(1).Find(What:="Due Date", _
LookIn:=xlValues, lookat:=xlWhole)
If Found Is Nothing Then Exit Sub
a = [{"#01/18/2015", Jan; "#01/19/2015", Jan; "#01/20/2015", Jan}]
LR = ws.Cells(ws.Rows.Count, Found.Column).End(xlUp).Row
Found.Offset(0, 2).EntireColumn.Insert
ws.Cells(1, Found.Column + 2).Value = "Month"
For Each cell In ws.Range(ws.Range("A2"), ws.Cells(LR, 3))
v = Application.VLookup(cell.Value, a, 2, False)
cell.EntireRow.Cells(Found.Column + 2).Value = IIf(IsError(v), "", v)
Next cell
End Sub
【问题讨论】: