简体   繁体   English

Excel VBA 当前范围单元格选择问题

[英]Excel VBA current range cell selection issue

i have this code below in which i select range and then to offset to get the handle of one cell down of base_red range.我在下面有这段代码,其中我 select 范围然后偏移以获得 base_red 范围下一个单元格的句柄。 The moment the next line of pastespecial is run it copies the data to range "mon" but also the cursor selection moves to "mon" range.运行下一行 pastespecial 时,它会将数据复制到“mon”范围,但 cursor 选择也会移动到“mon”范围。

I don't want the selection to remain at the old place only and not able to figure out how to stop the selection at old place only.我不希望选择仅保留在旧位置,而无法弄清楚如何仅在旧位置停止选择。 I require this because i have to do some more offset and paste the values to new locations.我需要这个,因为我必须做更多的偏移并将值粘贴到新位置。

If count1 = 1 Then
        ws.Range("base_red").Select
    End If
    ActiveCell.Offset(1, 0).Select 
    ActiveCell.Copy

    ws.Range("mon").PasteSpecial

You may not want to use select. The following code copies one range to another.您可能不想使用 select。以下代码将一个范围复制到另一个范围。 I implied from your example that you want to ensure you only copy and paste a single cell.我从你的例子中暗示你想确保你只复制和粘贴一个单元格。 If that is not the case, remove both instances of ".Cells(1,1)" from the code.如果不是这种情况,请从代码中删除“.Cells(1,1)”的两个实例。

Public Sub Temp()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    count1 = 1
    If count1 = 1 Then
        ws.Range("mon").Cells(1, 1) = ws.Range("base_red").Offset(1, 0).Cells(1, 1)
    End If
End Sub

Here is another method which makes the fewest changes to your code.这是对代码进行最少更改的另一种方法。

Public Sub Temp()
    Dim rng As Range
    Dim ws As Worksheet
    
    Set ws = ActiveSheet
    count1 = 1
    If count1 = 1 Then
        ws.Range("base_red").Select
    End If
    ActiveCell.Offset(1, 0).Select
    ActiveCell.Copy

    ' save the location of the active cell to rng.
    Set rng = ActiveCell
    
    ws.Range("mon").PasteSpecial
    ' return to the previous active cell
    rng.Select

End Sub

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

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