Excelvba refers to the string code in the cell and executes

Source: Internet
Author: User

Click the Security button on the Visual Basic toolbar to √ "Trust for Visual Basic project" Under Trusted Publishers tab in the Pop-up Security dialog box.

Alt+f11 Open the VBE editor, click the Menu tool-reference, and in the Pop-up Reference-vbaproject dialog box, select the item that references Microsoft Visual Basic 6.0 Extensibility.

Create a new module named: YYZX

In the Sheet1 (SHEET1) Code window, paste the following code:

Sub refers to the cell string code and executes ()

Dim Sr, Ksh, en

On Error Resume Next

Ksh = ThisWorkbook.VBProject.VBComponents ("Yyzx"). Codemodule.procbodyline ("Yyrgzx", Vbext_pk_proc)

If Err.Number = Then ' To determine if the YYRGZX process exists

sr = "Sub yyrgzx ()" & VbCrLf & Cells (1, 1) & VbCrLf & "End Sub"

With ThisWorkbook.VBProject.VBComponents ("Yyzx"). CodeModule

. addfromstring SR

End With

Call reference

Else

With ThisWorkbook.VBProject.VBComponents ("Yyzx"). CodeModule

Ksh =. ProcBodyLine ("Yyrgzx", Vbext_pk_proc)

En =. ProcCountLines ("Yyrgzx", Vbext_pk_proc)

. DeleteLines Ksh, Zh-1

End With

sr = "Sub yyrgzx ()" & VbCrLf & Cells (1, 1) & VbCrLf & "End Sub"

With ThisWorkbook.VBProject.VBComponents ("Yyzx"). CodeModule

. addfromstring SR

End With

Call reference

End If

End Sub

Sub Reference ()

Call YYRGZX

End Sub

Using the buttons on the Form toolbar, drag out a button named: Reference cell content execution, specifying: "Sheet1. Reference cell string code and execute" macro.

6 viewing effects, when A1 cell contents change, clicking the contents of a reference cell executes, depending on the string code in the cell A1, resulting in different effects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.