Excel Worksheet_SelectionChange事件根本没有触发? (在Office 2013和2016上)

时间:2017-02-07 16:00:06

标签: excel vba security shared-libraries mouseevent

我一直在努力完成我认为非常简单的测试。我想要实现的是在用户选择新单元格或更改单元格内容时弹出MsgBox

我已经在这里待了大约6个小时,到目前为止没有成功!我与Office 2016(Windows 10)和Office 2013(Windows 7)具有相同的行为。

以下是我的方法:

  1. 创建一个新的启用宏的工作簿。
  2. 在工作簿中记录一个新宏。停止录音。打开VBA。
  3. 打开“模块1”的代码,并使用下面的代码替换不需要的代码。保存文件。
  4. 文件 - >选项 - >信托中心 - >信任中心设置 - >宏设置 - >选择“信任对VBA项目对象模型的访问”。保存文件。
  5. 我也确保了Application.EnableEvents = True
  6. 我希望能够点击各种单元格或编辑单元格,并在事件发生时收到MsgBox
  7. 这是我的代码:

    Option Explicit
    
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)       
        MsgBox "changed!"  
    End
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        MsgBox "selected!"
    End Sub
    
    Public Sub Just_In_Case()
        Application.EnableEvents = True
    End Sub
    

    我错过了什么?是否存在阻止此操作事件的安全设置?我在网上工作的行为与在家离线时一样。

    提前感谢您的帮助! :)

    PS以下是我的VBA环境的屏幕截图,如果相关:https://i.stack.imgur.com/yXkMK.png

2 个答案:

答案 0 :(得分:0)

Workbook_SheetChange代码需要在ThisWorkbook代码模块中,而不是在常规模块中。

编辑:Worksheet_SelectionChange进入工作表代码模块

http://www.cpearson.com/excel/events.aspx

答案 1 :(得分:0)

为了完整参考,解决方案是我的代码被错误地放在 package com.medalle; import android.content.ContentValues; import android.content.Context; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DBAdapter { public static final String KEY_ROWID = "id"; public static final String KEY_NAME = "name"; public static final String KEY_STATUS = "status"; private static final String TAG = "DBAdapter"; private static final String DATABASE_NAME = "myDB"; private static final String DATABASE_TABLE = "person"; private static final int DATABASE_VERSION = 1; private static final String DATABASE_CREATE = "create table contacts (_id integer primary key autoincrement, " + "name text not null, status integer not null);"; private final Context context; private DatabaseHelper DBHelper; private SQLiteDatabase db; public DBAdapter(Context ctx) { this.context = ctx; DBHelper = new DatabaseHelper(context); } private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { try { db.execSQL(DATABASE_CREATE); } catch (SQLException e) { e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); db.execSQL("DROP TABLE IF EXISTS contacts"); onCreate(db); } } //---opens the database--- public DBAdapter open() throws SQLException { db = DBHelper.getWritableDatabase(); return this; } //---closes the database--- public void close() { DBHelper.close(); } // add new person public long addNewPerson(int id, String name, int status){ ContentValues initialValues = new ContentValues(); initialValues.put(KEY_NAME,name); initialValues.put(KEY_STATUS,status); return db.insert(DATABASE_TABLE, null, initialValues); } } package com.medalle; public class Person { int id; String name; int status; public Person(){} public Person (int id, String name, int status){ super(); this.id = id; this.name = name; this.status = status; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", status=" + status + "]"; } } package com.medalle; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TextView; import android.widget.Toast; public class PersonLists extends Activity { TextView personName, personId, personStatus; RadioGroup radioStatus; Button submit; DBAdapter database; int pos, pos1; @Override /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.addpersons); personId = (TextView)findViewById(R.id.idTV); personStatus = (TextView)findViewById(R.id.statusTV); personName = (TextView)findViewById(R.id.nameTV); // name = (EditText)findViewById(R.id.pname); // id = (EditText)findViewById(R.id.pid); submit = (Button)findViewById(R.id.btnSubmit); radioStatus = (RadioGroup)findViewById(R.id.radioStatusGroup); radioStatus.setOnCheckedChangeListener(new OnCheckedChangeListener(){ public void onCheckedChanged(RadioGroup group, int checkedId){ pos= radioStatus.indexOfChild(findViewById(checkedId)); pos1=radioStatus.indexOfChild(findViewById(radioStatus.getCheckedRadioButtonId())); switch (pos) { case 0 : Toast.makeText(getBaseContext(), "You have Clicked Online", Toast.LENGTH_SHORT).show(); break; case 1 : Toast.makeText(getBaseContext(), "You have Clicked Offline", Toast.LENGTH_SHORT).show(); break; } } }); } public void addPerson(View v){ EditText ed = (EditText)findViewById(R.id.pname); Person p = new Person(); p.name = ed.getText().toString(); Log.v("Checking name",p.name); Log.v("Checking id", Integer.toString(p.id)); p.status = radioStatus.getCheckedRadioButtonId(); database.addNewPerson(p.id, p.name, p.status); // p.status Toast.makeText(this, "Person Successfully Added", Toast.LENGTH_LONG).show(); } } 编码区域中。 (我不知道代码可以去其他地方!)

Module1属于Workbook_SheetChange()

ThisWorkbook属于Worksheet_SelectionChange()

喜欢这个!图片截图: Sheet1 ThisWorkbook

非常感谢蒂姆,感谢您的帮助! :d