使用月份名称根据另一列中的date自动填充列 – Excel VBA

我是新的Excel VBA工作,我有这个问题。

我希望我的用户表单根据用户select的date自动填充列中的dynamic范围。 当单击“创build”button时,同时在工作表的一列填充date,在其旁边的另一列上,还根据编码的date填充“月份”名称。 你认为这可能吗? 您的努力将不胜感激。 谢谢!

Private Sub CommandButton1_Click() If Week <> "" Then Dim ws As Worksheet Dim LR1 As Long Dim LR3 As Long Dim LR4 As Long Set ws = ThisWorkbook.Sheets("Sheet1") ' get the last row from columns that has a value LR1 = ws.range("A" & ws.Rows.Count).End(xlUp).Row LR3 = ws.range("C" & ws.Rows.Count).End(xlUp).Row + 1 LR4 = ws.range("D" & ws.Rows.Count).End(xlUp).Row + 1 ' use the last row to determine how far down to extend the formula ws.range("D" & LR4 & ":D" & LR1).Value = Me.Week.Value ws.range("C" & LR3 & ":C" & LR1).Value = Me.DTPicker1.Value Else MsgBox "Please fill all fields!" End If Unload Me End Sub 

创建模板GUI

要在列B中添加月份名称,请将此添加到您的代码中:

  Dim LR2 As Long ' ... LR2 = ws.Range("B" & ws.Rows.Count).End(xlUp).Row + 1 ' ... ws.Range("B" & LR2 & ":B" & LR1).FormulaR1C1 = "=TEXT(R[0]C[1], ""mmm"")" 

这实际上会在B列中插入一个表示它旁边单元格的月份名称的公式。

我不确定我是否正确理解情况,但也许可以用“.formula = application.worksheetfunction(”= month(…)“)来填充第二列。