Vba Excel:在特定列中输入日期
我的要求是: 我想在E5到AI5列中输入从1月1日到1月31日的日期。当前正在使用以下代码,但该代码不起作用 第二年,我把它作为用户输入,每次都应该改变Vba Excel:在特定列中输入日期,vba,excel,Vba,Excel,我的要求是: 我想在E5到AI5列中输入从1月1日到1月31日的日期。当前正在使用以下代码,但该代码不起作用 第二年,我把它作为用户输入,每次都应该改变 Sub LoopA() Call Using_InputBox_Method Dim i As Integer Dim j As Integer Dim PH As Integer i = 5 For j = 5 To 35 Cells(i, j).Value = "
Sub LoopA()
Call Using_InputBox_Method
Dim i As Integer
Dim j As Integer
Dim PH As Integer
i = 5
For j = 5 To 35
Cells(i, j).Value = "=Date(E1,1,j)"
Next j
End Sub
Public Function Using_InputBox_Method() As Integer
Dim Response As Integer
' Run the Input Box.
Response = Application.InputBox("Enter a Year.", _
"Number Entry", , 250, 75, "", , 1)
' Check to see if Cancel was pressed.
If Response <> False Then
' If not, write the number to the first cell in the first sheet.
Worksheets(1).Range("E1").Value = Response
End If
Using_InputBox_Method = Response
End Function
Sub-LoopA()
使用\u InputBox\u方法调用
作为整数的Dim i
作为整数的Dim j
将PH值设置为整数
i=5
对于j=5到35
单元格(i,j).Value=“=日期(E1,1,j)”
下一个j
端接头
使用作为整数的\u InputBox\u方法()的公共函数
作为整数的Dim响应
'运行输入框。
响应=Application.InputBox(“输入年份”_
“数字输入”,250,75,“,1)
'检查是否按了“取消”。
如果回答为假,那么
'如果不是,请将数字写入第一页的第一个单元格。
工作表(1).范围(“E1”).值=响应
如果结束
使用\输入框\方法=响应
端函数
A)
“
中的任何内容都将被视为字符串。因此“=Date(E1,1,j)”
只是一个字符串。我想您想要的是
"=Date(E1,1," & j & ")"
B)
对于j=5到35
你确定要涨到35岁吗?任何一个月你最多可以涨到31岁:)
=Date()
的语法是Date(年、月、日)
此外,您还需要在此处进行额外检查,以查看该日期是否有效。例如,2月30日将给您一个错误
C)
应避免使用InputBox
来接受日期。它可能会生成错误。您可能需要使用。如果仍要使用InputBox
,则必须进行验证以确保没有错误
D)
关于年
自动更改,一旦用户自动输入日期,您必须增加E列中的年数
这就是你想要的吗
Sub Sample()
Dim Yr As Long, i As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
Yr = Application.InputBox("Enter a Year.", _
"Number Entry", , 250, 75, "", , 1)
'~~> Set it to whatever Year Range you want
If Yr < 1900 Or Yr > 9999 Then
MsgBox "Incorrect Year"
Exit Sub
End If
With ws
.Range("E1").Value = Yr
For i = 5 To 35
.Cells(5, i).Formula = "=Date(E1,1," & (i - 4) & ")"
Next i
End With
End Sub
子样本()
你一样长,我一样长
将ws设置为工作表
设置ws=ThisWorkbook.Sheets(“Sheet1”)
Yr=Application.InputBox(“输入年份”_
“数字输入”,250,75,“,1)
“~~>将其设置为您想要的任何年份范围
如果Yr<1900或Yr>9999,则
MsgBox“不正确的年份”
出口接头
如果结束
与ws
.范围(“E1”).值=年
对于i=5到35
.Cells(5,i).Formula=“=日期(E1,1,”&(i-4)&”)
接下来我
以
端接头