当前位置: 动力学知识库 > 问答 > 编程问答 >

How to stop Excel VBS running if cell has contents

问题描述:

I have created a spreadsheet which auto-fills date/time in column B when there are contents entered into column C. There is a risk however of double clicking or making changes in the cell will update the date and time, I don't want it to do this if it has already been populated.

What do I need to add to my code to stop the macro running IF the cell has contents?

This is the coding I used:

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count > 1 Then Exit Sub

If Not Intersect(Target, Range("C8:C27")) Is Nothing Then

With Target(1, -0)

.Value = Now

.EntireColumn.AutoFit

End With

End If

End Sub

网友答案:

Checking if the cell in column B is empty before writing the date into it is fairly straightforward. Inside your With statement just check if the value of the cell in B is blank and exit the sub if there is something in it. Otherwise run through your code to write the date to the cell.

With Target(1, -0)
    If .Value <> "" Then Exit Sub

    .Value = Now

    .EntireColumn.AutoFit

End With
分享给朋友:
您可能感兴趣的文章:
随机阅读: