简体   繁体   English

VBA Excel:在另一个工作簿上获取单击的单元格地址

[英]VBA Excel: Get the clicked cell´s adress on another workbook

I have an excel file A with a macro and I have to retrive a cell´s adress in another excel file B by the user´s click on it.我有一个带有宏的 excel 文件 A,我必须通过用户单击它来检索另一个 excel 文件 B 中的单元格地址。

The macro looks like this.宏看起来像这样。

In the Class :课堂上

Public WithEvents appevent As Application

Private Sub appevent_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
ClickedCell = ActiveCell.Address
End Sub

In the Module模块中

Sub ClickedCellSub()
Dim WbA As Variant, WbB As Variant
WbA = ThisWorkbook.Name
WbB = "B.xlsx"
MsgBox "Please double click on the Assembly SS 00 you want to compare"
Set myobject.appevent = Application
Workbooks(WbB).Sheets(1).Activate
Set myobject.appevent = Nothing
MsgBox ClickedCell
Workbooks(WbA).Activate
End Sub

The problem is, the macro doesn´t wait for the event DoubleClick on the other excel sheet and goes to the end.问题是,宏不会等待另一个 Excel 工作表上的 DoubleClick 事件并结束。

How can I stop the macro until the event happens?如何在事件发生之前停止宏?

Many thanks in advance!提前谢谢了!

I would use event sinking, but not sure how you have approached it, but this is how i'd use.我会使用事件沉没,但不确定您是如何处理它的,但这就是我的使用方式。

In a class use the following :在类中使用以下内容:

Private WithEvents wb As Excel.Workbook
Private rng As Excel.Range

Public Event evtCellClicked(rngClicked As Excel.Range)
Public Event evtCellDoubleClicked(rngDoubleCliecked As Excel.Range)

Public Sub init(wbInput As Excel.Workbook)
    Set wb = wbInput
End Sub

Public Property Get CellClicked() As Excel.Range
    CellClicked = rng
End Property

Private Sub wb_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    Set rng = Target
    RaiseEvent evtCellClicked(Target)
End Sub

Private Sub wb_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Set rng = Target
    RaiseEvent evtCellClicked(Target)
End Sub

I changed the code.我改变了代码。 In the module:在模块中:

enter Option Explicit
Private mobjApplication As clsApplication
Global ClickedCell As String

Sub Vergleichen()
Dim StruktBer As Variant        


StruktBer = Application.GetOpenFilename(, , "Strukturbericht öffnen")                       '####Name der Datei aus der Strukturberich

Set mobjApplication = New clsApplication
Workbooks.Open StruktBer

MsgBox "Bitte die Sachnummer der Strukturstufe '00' anklicken die man vergleichen möchte"

End Sub

Sub GOFURTHER

In the Class:在课堂里:

Option Explicit

Private WithEvents mobjApplication As Application

Private Sub Class_Initialize()
Set mobjApplication = Application
End Sub

Private Sub mobjApplication_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    ClickedCell = Target.Address
    Set mobjApplication = Nothing
    Call GOFURTHER

End Sub

The macro stops until the user clicks on the other excel table.宏停止,直到用户单击另一个 Excel 表。 Is there any way not to exit the first sub and to remain in the first one?有没有办法不退出第一个子并留在第一个?

Why not simply using an InputBox ?为什么不简单地使用 InputBox ?
https://docs.microsoft.com/en-us/office/vba/api/excel.application.inputbox https://docs.microsoft.com/en-us/office/vba/api/excel.application.inputbox
In your prompt you ask the user to select a range in the appropriate document, and that's it.在您的提示中,您要求用户在适当的文档中选择一个范围,就是这样。

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

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