将功能键分配给excel宏

时间:2018-04-26 22:35:50

标签: excel vba excel-vba

我已经看到以下示例代码建议将功能键分配给excel宏,但无论我在哪里尝试复制代码(模块,工作簿,工作表),其中包含原始分配的原始代码始终执行功能键任务而不是尝试/分配的宏。此代码是否需要某种类型的高级代码才能首先禁用功能键的原始分配任务?

void Derived_test_stub(Base *this)
{
    Derived *adjusted_this = reinterpret_cast<Derived*>(reinterpret_cast<uintptr_t>(this) + offset_from_Base_to_Derived);
    Derived::test(adjusted_this);
}

int Derived_get_stub(Base *this)
{
    Derived *adjusted_this = reinterpret_cast<Derived*>(reinterpret_cast<uintptr_t>(this) + offset_from_Base_to_Derived);
    return Derived::get(adjusted_this);
}

struct vtable_Base
{
    void* funcs[2] = {&Base::test, &Base::get};
};

struct vtable_Derived
{
    void* funcs[2] = {&Derived_test_stub, &Derived_get_stub};
};

Base::Base()
{
    this->vtable = &vtable_Base;
    bob = 0;
}

Derived::Derived() : Base()
{
    Base::vtable = &vtable_Derived;
    this->vtable = &vtable_Derived;
    alex = 0;
}

...

Base *b = new Derived;

//b->test(); // calls Derived::test()...
typedef void (*test_type)(Base*);
static_cast<test_type>(b->vtable[0])(b); // calls Derived_test_stub()...

//int i = b->get(); // calls Derived::get()...
typedef int (*get_type)(Base*);
int i = static_cast<get_type>(b->vtable[1])(b); // calls Derived_get_stub()...

1 个答案:

答案 0 :(得分:3)

将其放在工作簿代码区

Private Sub Workbook_Open()
    Application.OnKey "{F1}", "A_1"
    Application.OnKey "{F2}", "B_1"
    Application.OnKey "{F3}", "C_1"
    Application.OnKey "{F4}", "D_1"
    Application.OnKey "{F5}", "E_1"
End Sub

将您的其他潜水员放在标准模块

工作簿打开时将发生OnKey分配。要恢复正常的功能键行为,请运行:

Sub reset()
    Application.OnKey "{F1}"
    Application.OnKey "{F2}"
    Application.OnKey "{F3}"
    Application.OnKey "{F4}"
    Application.OnKey "{F5}"
End Sub