Autofill a column with month name depending on the date in another column - Excel VBA

我怕爱的太早我们不能终老 提交于 2020-01-25 20:00:15

问题


I'm new in working with Excel VBA and I have this problem.

I would like my userform to autofill dynamic range in a column based on the date picked by the user. When the "create" button is clicked, simultaneously, on one column of the sheet the date is populated and on another column beside it, the Month name is also populated depending on the date encoded. Do you think it'll be possible? Your efforts will be greatly appreciated. Thanks!

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


回答1:


To add the month name in column B, add this to your code:

    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"")"

This will actually insert a formula in the B column that represents the month name of the cell next to it.




回答2:


I'm not sure I understand the Situation correctly, but maybe you can fill the second column just with ".formula = application.worksheetfunction("=month(...)")



来源:https://stackoverflow.com/questions/37809881/autofill-a-column-with-month-name-depending-on-the-date-in-another-column-exce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!