简体   繁体   English

自动化 activecell 值和偏移量 function

[英]automate activecell values and offset function

'Please help me to automate the below code using input function, so that i can just type the number in the input box to get desired rows to be filled automatically.' “请帮助我使用输入 function 自动执行以下代码,以便我只需在输入框中键入数字即可自动填充所需的行。”

sub DAY()
GENERAL Macro


ActiveCell.Value = "DAY"

Select Case ActiveCell.Value

Case "DAY"
    ActiveCell.Resize(, 1).Value = "DAY"
    ActiveCell.Offset(, 1).Value = "OFF"
    ActiveCell.Offset(, 2).Value = "DAY"
    ActiveCell.Offset(, 3).Value = "OFF"
    ActiveCell.Offset(, 4).Value = "DAY"
    ActiveCell.Offset(, 5).Value = "OFF"
    ActiveCell.Offset(, 6).Value = "DAY"
    ActiveCell.Offset(, 7).Value = "OFF"
    ActiveCell.Offset(, 8).Value = "DAY"
    ActiveCell.Offset(, 9).Value = "OFF"
    ActiveCell.Offset(, 10).Value = "DAY"
    ActiveCell.Offset(, 11).Value = "OFF"
    ActiveCell.Offset(, 12).Value = "DAY"
    ActiveCell.Offset(, 13).Value = "OFF"


 End Select

 End Sub

'Please help me to automate the below code using input function, so that i can just type the number in the input box to get desired rows to be filled automatically.' “请帮助我使用输入 function 自动执行以下代码,以便我只需在输入框中键入数字即可自动填充所需的行。”

I think you might want something like this?我想你可能想要这样的东西?

Sub PopulateListByRows()
    Dim n As Long, a As Long, ArrTxt
    n = InputBox("How many days?")
    ArrTxt = Array("Day", "Off")
    For a = 1 To n
        ActiveCell.Offset(a - 1).Value = ArrTxt((a + 1) Mod 2)
    Next
End Sub

Sub PopulateListByColumns()
    Dim n As Long, a As Long, ArrTxt
    n = InputBox("How many days?")
    ArrTxt = Array("Day", "Off")
    For a = 1 To n
        ActiveCell.Offset(0, a - 1).Value = ArrTxt((a + 1) Mod 2)
    Next
End Sub

you may use formulas你可以使用公式

Sub Day()
    Dim n As Long
    n = CLng(InputBox("How many days?"))
    With ActiveCell.Resize(1, 2 * n)
        .FormulaR1C1 = "=IF(RC[-1]=""DAY"",""OFF"",""DAY"")"
        .Value = .Value
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM