0
我需要一些指導。我有一組數據。在A列中,有從2015年1月1日至2015年12月31日的日期。我想要實現的是創建一個新列,並根據日期填充值。基於另一列日期的新列值
例如,如果列A行1的日期爲01/16/2015,則新列的值將爲「Jan-15」。基本上,如果日期在2015年1月16日至2015年2月15日之間,則將具有「1月15日」的值。如果在2015年2月16日至2015年3月15日之間,價格將爲「2月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
嘿!非常感謝。它像一個魅力。但是,如果我希望它是「2015年2月15日」而不是2015年2月15日? – 2015-02-11 08:21:50
可以使用格式化單元格>自定義> mmm-yy格式化整列或使用VBA中的格式化功能。 – 2015-02-11 10:38:16
更新後的代碼 - 看看現在是否適合您 – 2015-02-11 11:00:16