简体   繁体   中英

Automatically set value to empty cells in Excel when another cell is populated

I have a spreadsheet that tracks certain information that is on a shared network drive. When a user adds a row, specifically be entering data into the next available row, cell B, I want to set a few other cells to default values. For instance, cell A should be a 1up number, cell C should be the user name, and cell D should be the current date, time stamp.

Individually, I know how to do all the pieces, but I do not know how to set those cells when cell B is changed from blank to not blank.

I am not opposed to using VBA, but would like to avoid it if possible.

Some of the things you wish to record are easier with VBA . I would use the following worksheet event macro:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim s As String

    If Intersect(Target, Range("B:B")) Is Nothing Then Exit Sub
    ary = Split(Environ("homepath"), "\")
    s = ary(UBound(ary))
    Application.EnableEvents = False
        With Target
            .Offset(0, -1).Value = .Offset(-1, -1).Value + 1
            .Offset(0, 1).Value = s
            .Offset(0, 2).Value = Now
        End With
    Application.EnableEvents = True
End Sub

在此处输入图片说明

Because it is worksheet code, it is very easy to install and automatic to use:

  1. right-click the tab name near the bottom of the Excel window
  2. select View Code - this brings up a VBE window
  3. paste the stuff in and close the VBE window

If you have any concerns, first try it on a trial worksheet.

If you save the workbook, the macro will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

To remove the macro:

  1. bring up the VBE windows as above
  2. clear the code out
  3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm

Macros must be enabled for this to work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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