EXCEL VBA编程问题
答案:6 悬赏:0 手机版
解决时间 2021-03-17 04:11
- 提问者网友:咪咪
- 2021-03-16 10:44
如何设置一个响应事件, 比如A1是下拉框(选项为是和否),B1要显示当前日期。C1要显示当前时间。要A1下拉选择“是”的时候,将B1的值自动填充为当前日期,C1自动填充为当前时间?
最佳答案
- 五星知识达人网友:第四晚心情
- 2021-03-16 11:47
'在工作表的Worksheet_Change事件中加入下面代面可实现你的要求
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 1 And Target.Column = 1 Then
If CStr(Target.Value) = "是" Then [B1] = Date: [C1] = Time
If CStr(Target.Value) = "否" Then [B1] = "": [C1] = ""
End If
End Sub
样表联系我QQ
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 1 And Target.Column = 1 Then
If CStr(Target.Value) = "是" Then [B1] = Date: [C1] = Time
If CStr(Target.Value) = "否" Then [B1] = "": [C1] = ""
End If
End Sub
样表联系我QQ
全部回答
- 1楼网友:第四晚心情
- 2021-03-16 17:12
在B1输入=if(A1="是",today(),"")
C1=if(A1="是",NOW(),"")就行了。
- 2楼网友:举杯邀酒敬孤独
- 2021-03-16 15:55
vba 代码及简单说明
a1 = 6 '定义变量a1,假设初值是6
an = 0 '定义变量an ,数列中任一项
d = 3 '定义变量d 公差,假设值是6
n = 10 '定义变量n,数列元素个数
for k = 1 to n
an = an + a1 '逐项求出
cells(k, 2) = an '写入单元格第k 行,第2列
next
- 3楼网友:十鸦
- 2021-03-16 15:10
用函数就可以实现你的功能
录制vba的话也是差不多这样的函数,判断语句来实现响应事件
B1=if(A1="是",today(),"")
C1=if(A1="是",NOW(),""),c1的单元格格式设置成hh:MM
- 4楼网友:迷人又混蛋
- 2021-03-16 14:27
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$A$1" Then
With Target.Validation
.Delete
.Add Type:=xlValidateList, _
AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, _
Formula1:="是,否"
End With
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" And Target.Value = "是" Then
Cells(Target.Row, "b").Value = DateValue(Now)
Cells(Target.Row, "c").Value = TimeValue(Now)
End If
End Sub
- 5楼网友:行路难
- 2021-03-16 13:07
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim r As Integer
r = Selection.Row
If Cells(r, 1) = "是" Then
Cells(r, 2) = Format(Now(), "yyyy/mm/dd")
Cells(r, 3) = Format(Now(), "hh:mm:ss")
End If
End Sub
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯