Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Excel中返回行军蚂蚁的位置?_Excel_Vba - Fatal编程技术网

如何在Excel中返回行军蚂蚁的位置?

如何在Excel中返回行军蚂蚁的位置?,excel,vba,Excel,Vba,我知道Application.CutCopyMode,但它只返回CutCopyMode False、xlCopy或xlCut的状态 如何使用VBA在Excel中返回当前复制范围的地址?我不需要当前选择的范围Application.Selection.Address。我需要一系列牢房的地址,周围有移动的边境蚂蚁 换句话说,如果选择一个单元格区域,按CTRL+C,然后将所选内容移动到另一个单元格,我需要用户按CTRL+C时所选单元格的地址 谢谢 据我所知,使用vba无法做到这一点。但是,您可以编写自

我知道Application.CutCopyMode,但它只返回CutCopyMode False、xlCopy或xlCut的状态

如何使用VBA在Excel中返回当前复制范围的地址?我不需要当前选择的范围Application.Selection.Address。我需要一系列牢房的地址,周围有移动的边境蚂蚁

换句话说,如果选择一个单元格区域,按CTRL+C,然后将所选内容移动到另一个单元格,我需要用户按CTRL+C时所选单元格的地址


谢谢

据我所知,使用vba无法做到这一点。但是,您可以编写自己的复制子代码,并将源代码存储在全局变量中

大概是这样的:

Option Explicit
Dim myClipboard As Range

Public Sub toClipboard(Optional source As Range = Nothing)
    If source Is Nothing Then Set source = Selection
    source.Copy
    Set myClipboard = source
End Sub

复制区域时,地址将与其他格式一起复制到剪贴板。您可以使用剪贴板查看器应用程序进行检查。
因此,如果您需要复制的范围,请从剪贴板获取它。它将类似于>$A2:$B5或类似的

我能想到的唯一方法是跟踪使用全局变量选择的最后一个范围,然后等待,直到您认为复制操作完成。不幸的是,两者都不容易

下面是一个有两个问题的快速尝试

如果将同一数据复制两次 没有更新 如果需要复制或粘贴 从另一个应用程序激发,结果 可能会有所不同。 这是跟踪不存在的事件时最后的希望技巧之一。希望这有帮助


哦,请原谅这些评论,它们只是为了帮助SO的语法更清晰。

我认为您可以使用这种方法


此方法为热键Ctrl+C分配一个函数,每次使用此组合时,该函数将被触发,您可以获得范围的地址

10年后,您仍然无法直接引用复制的范围 由行进中的蚂蚁边界又称舞蹈边界、移动边界显示

但您可以通过将单元格作为链接复制到临时工作表来获取其地址。在那里,您可以收集所需范围的地址


该代码也适用于多个范围,如果复制的范围和选定的范围在不同的工作表上,也适用。

看起来您可能是对的。从字面上看,似乎没有办法返回被复制的细胞框或行进蚂蚁的位置。我仍然坚持,但目前我已经回到了使用Application.InputBox选择起点和终点的旧方法。用户界面可以更加优雅,它们可以进入当前复制的范围,并以复制和粘贴的方式工作。谢谢你最终想做什么?如果您试图控制粘贴的内容,可以通过工作表更改事件进行控制。也许您的最终目标是什么的一点细节…我正在尝试允许我的外接程序用户使用他们用于复制和粘贴的相同熟悉的选择源、Ctrl+C、选择目标过程,以在我的外接程序中启动自定义复制粘贴过程。目前,“我的外接程序”要求用户单击按钮启动外接程序程序,然后选择源应用程序.InputBox,然后选择目标另一个应用程序.InputBox。我认为select origin、Ctrl+C、select destination方法更直观,我可以使用CutCopyMode验证该过程是否已正确启动。谢谢Bummer:VBA不支持剪贴板上其他非文本值所需的数据类型。从这里开始:
''# Add a reference to : FM20.dll or Microsoft Forms 2.0
''# Some more details at http://www.cpearson.com/excel/Clipboard.aspx

Option Explicit

Dim pSelSheet As String
Dim pSelRange As String

Dim gCopySheet As String
Dim gCopyRange As String

Dim gCount As Long
Dim prevCBText As String

Dim DataObj As New MSForms.DataObject



Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
        ByVal Target As Excel.Range)

    CopyTest
    pSelSheet = Sh.Name
    pSelRange = Target.Address


    ''# This is only so you can see it working
    gCount = gCount + 1
    application.StatusBar = gCopySheet & ":" & gCopyRange & ", Count: " & gCount
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Source As Range)
    CopyTest ''# You may need to call CopyTest from other events as well.

    ''# This is only so you can see it working
    gCount = gCount + 1
    application.StatusBar = gCopySheet & ":" & gCopyRange & ", Count: " & gCount
End Sub




Sub CopyTest()
    Dim curCBText As String
    Dim r As Range
    DataObj.GetFromClipboard

    On Error GoTo NoCBData
      curCBText = DataObj.GetText
    On Error Resume Next


    ''# Really need to test the current cells values
    ''# and compare as well. If identical may have to
    ''# update the gCopyRange etc.

    If curCBText <> prevCBText Then
      gCopySheet = pSelSheet
      gCopyRange = pSelRange
      prevCBText = curCBText
    End If

  Exit Sub


NoCBData:
  gCopySheet = ""
  gCopyRange = ""
  prevCBText = ""
End Sub
Private Sub ThereAreTheMarchingAnts()
    Dim rngCopied As Range          ' the copied range with the marching ants border
    Dim rngSelected As Range        ' the selected range
    Dim tmpWorksheet As Worksheet   ' a temporary worksheet
    Dim c As Range                  ' a cell for looping

    ' Exit, if nothing was copied (no marching ants border):
    If Not (Application.CutCopyMode = xlCopy Or Application.CutCopyMode = xlCut) Then Exit Sub

    ' Exit, if no range is selected (just for demonstration)
    If Not TypeName(Selection) = "Range" Then Exit Sub

    ' remember selected Range:
    Set rngSelected = Selection

    ' add a temporary sheet and paste copied cells as link:
    Set tmpWorksheet = ActiveWorkbook.Sheets.Add
    tmpWorksheet.Paste link:=True

    ' go through all pasted cells and get the linked range from their formula:
    For Each c In tmpWorksheet.UsedRange
        If rngCopied Is Nothing Then
            Set rngCopied = Range(Mid(c.Formula, 2))
        Else
            Set rngCopied = Union(rngCopied, Range(Mid(c.Formula, 2)))
        End If
    Next c

    ' delete the temporary worksheet without asking:
    Application.DisplayAlerts = False
    tmpWorksheet.Delete
    Application.DisplayAlerts = True

    ' show the addresses:
    MsgBox "Copied Range: " & rngCopied.Address(0, 0, xlA1, True) & vbLf & _
           "Selected Range: " & rngSelected.Address(0, 0, xlA1, True)
End Sub