简体   繁体   English

如何在excel单元格中自动插入1?

[英]How to insert a 1 automatically in an excel cell?

I want to be able to put a value of 1 in an excel cell when it is selected.我希望能够在选择时将值 1 放入 excel 单元格中。 Cells that are not selected remain blank.未被选中的单元格保持空白。

ActiveCell will use the current cell that is selected then, as you can see, it assigns the value of 1 to that cell. ActiveCell将使用当前选定的单元格,如您所见,它将值 1 分配给该单元格。

ActiveCell.value = 1

If you would like it to be more modular if it is perhaps occurring many times, then consider having a look at change events here , as suggested by @Tom如果您希望它更加模块化,如果它可能发生多次,那么请考虑在 此处查看更改事件,如@Tom 所建议的

I really don't recommend you doing this especially if this is for monitoring your stock and so for this time and this time only I've written it to make it a bit more safer if you're actually to do this BUT I DO NOT RECOMMEND IT.我真的不建议你这样做,特别是如果这是为了监控你的股票,所以这次和这次只是我写它是为了让它更安全,如果你真的这样做,但我不这样做推荐它。

you're going to need to paste this into the worksheet module that it relates to您将需要将其粘贴到与之相关的工作表模块中

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    If Target.Columns.Count = 1 And Target.Rows.Count = 1 And Target.Column = 2 Then
        Target.Value = 1
    End If
End Sub

To get to the worksheet module you need to go into the vba editor (Shortcut Alt+F11 on windows) and paste in the correct worksheet module (Notice highlighted sheet)要进入工作表模块,您需要进入 vba 编辑器(Windows 上的快捷键 Alt+F11)并粘贴正确的工作表模块(注意突出显示的工作表)

在此处输入图片说明

I warn you though this could very quickly lead to your stock levels being inaccurate as this will run whenever you click on a cell or move around a worksheet with the arrow keys.我警告您,尽管这可能会很快导致您的库存水平不准确,因为每当您单击单元格或使用箭头键在工作表中移动时,它都会运行。 This could mess up your whole system and there will be no undo (vba wipes the undo memory)这可能会弄乱您的整个系统,并且无法撤消(vba 会擦除撤消内存)

What would be much better to do is actually monitor your exact stock levels in excel, then use a formula such as (say you keep your stock level in column c)更好的做法是在 excel 中实际监控您的确切库存水平,然后使用一个公式,例如(假设您将库存水平保留在 c 列中)

=IF(C1>0, B1=1, B1=0)

This would then automatically accurately represent whether it was in stock or not.然后这将自动准确地表示它是否有库存。

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

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