我是使用Excel VBA的新手,但遇到此问题。
我希望我的用户表单根据用户选择的日期自动在列中填充动态范围。同时单击“创建”按钮时,将在工作表的一列上填充日期,并在其旁边的另一列上填充月份名称,具体取决于所编码的日期。您认为有可能吗?您的努力将不胜感激。谢谢!
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
要在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列中插入一个公式,该公式表示该单元格旁边的单元格的月份名称。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句