简体   繁体   English

将 LONG 变量引用到工作表中的单元格

[英]reference LONG variable to cell in a sheet

How to link a Long variable to a value entered in an excel sheet?如何将 Long 变量链接到 Excel 工作表中输入的值? I want to create large number of rows and although I am able to change the variable directly in the code (eg x=100000) but as soon as I link the variable x to a cell (eg x = Cells(1,1) no value seems to be recognized and printed to the range.我想创建大量的行,虽然我可以直接在代码中更改变量(例如 x=100000)但是一旦我将变量 x 链接到一个单元格(例如 x = Cells(1,1) no值似乎被识别并打印到范围内。

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:将变量变暗为PublicGlobal (在设置它的例程之外),以便其他子例程可以“看到”它的值:

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):然后在Worksheet_SelectionChange事件或Worksheet_Change事件中添加将其值设置到工作表的代码(取决于您希望它如何工作):

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:将此添加到WorkSheet_Change事件中:

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

Thanks for taking the time to answer my question.感谢您抽出时间回答我的问题。 I found the following approach to work best for me.我发现以下方法最适合我。

 Sub test() Dim x As LongLong x = Worksheets("Sheet1").Cells(4, 2) 'eg value is 100.000 range(Cells(1, 1), Cells(x, 1)).Value = "*" 'fill x rows with a * as a test End Sub

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

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