首页 > 解决方案 > 将 LONG 变量引用到工作表中的单元格

问题描述

如何将 Long 变量链接到 Excel 工作表中输入的值?我想创建大量行,虽然我可以直接在代码中更改变量(例如 x=100000),但是只要我将变量 x 链接到一个单元格(例如 x = Cells(1,1) 否值似乎被识别并打印到范围内。

标签: excelvba

解决方案


In order to simulate the cell being linked to the variable, you would have to add some code that gets triggered by something to write it to the cell.

Dim the variable as Public or Global (outside of the routine that sets it), so that other subroutines can "see" its value:

Public x as Long

and then add the code that sets its value to the worksheet in the Worksheet_SelectionChange event or the Worksheet_Change event (depending on how you want it to work):

Cells(1, 1) = x

If you are trying to set the variable automatically, then here's the opposite. Add this to the WorkSheet_Change event:

If Target.Row = 1 and Target.Column = 1 Then 
    x = Cells(Target.Row, Target.Column)
End If

推荐阅读