Current location - Education and Training Encyclopedia - Graduation thesis - How to assign values to excel cells with variables
How to assign values to excel cells with variables
1, first open a work sample table as an example.

2. use alt+f 1 1 to enter the vbe editor, insert a new module, and enter the following code in the module: subtest2 () worksheets ("sheet1"). Range ("a 1"). Value =

_ worksheet ("sheet 1"). Range ("a5"). Valuemsgbox "Now the value in cell A 1 is also"&;

_ worksheet ("Sheet 1"). Range ("A5"). End of value Sub

3. You can see that a worksheet named sheet 1 is specified here, and this range is used to select cell a 1 and select the properties of cell values. In other words, cell a 1 in sheet 1 is an object, and value value is an attribute.

4. There is an underline, which exists as a connector in vba, meaning that the next line of code is a statement that will not be interrupted after being linked to this line of code.

5. Use the msgbox function to display a message that the cell value of a 1 is, followed by the value of a5 cell. In fact, because there is an assignment in front, you can also write the value of a 1 cell directly here.

6. Because the objects here are unified, we can abbreviate the code as with, so we only need to write a statement with the worksheet ("sheet 1") as follows.

. Range ("A 1"). Value = _

. Range ("A5"). value

MsgBox "The value in cell A 1 now is also"&; _

. Range ("A5"). value

End with.

7. You can see that the code runs successfully. A 1 is the same as the value in a5 cell, which proves that the assignment is successful.

8、