【问题标题】:Copy data from another workbook to an active cell将数据从另一个工作簿复制到活动单元格
【发布时间】:2021-02-04 07:38:46
【问题描述】:

我需要更改我的代码,以便从另一个文件复制的选择粘贴到打开的工作簿中的活动单元格:


Sub Get_Data_From_File()
`Dim FiletoOpen As Variant
Dim OpenBook As Workbook
Application.ScreenUpdating = False

FiletoOpen = Application.GetOpenFilename(Title:="Browse for your file & Import Range", FileFilter:="Excel Files(*.xlsx),*xlsx*")

If FiletoOpen <> False Then

Set OpenBook = Application.Workbooks.Open(FiletoOpen)
OpenBook.Sheets(1).Range("D1:D100").Copy
ThisWorkbook.Worksheets("Sheet1").ActiveCell.PasteSpecial xlPasteValues
OpenBook.Close False
End If

Application.ScreenUpdating = True


End Sub


我尝试更改此行: ThisWorkbook.Worksheets("Sheet1").ActiveCell.PasteSpecial xlPasteValues

ActiveCell.PasteSpecial xlPasteValues

另外,我尝试定义一个范围,如下例所示: ThisWorkbook.Worksheets("Sheet1").Range("J4").PasteSpecial xlPasteValues

这很有效。但是,我不想复制到该范围,而是复制到我打开的工作簿中的活动选定单元格。

有什么建议吗?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    ActiveCell 是应用程序的属性,而不是工作表,这就是为什么您无法按最初编写的方式访问它的原因。我正在试验您的代码,看起来当打开工作表时,ActiveCell 变为打开工作表的A1

    那么怎么样:

    ' ++ make a note of current active cell before opening file
    Dim r As Range
    Set r = ActiveCell
    
    
    FiletoOpen = Application.GetOpenFilename(Title:="Browse for your file & Import Range", FileFilter:="Excel Files(*.xlsx),*xlsx*")
    
    ...
    
    '-- ThisWorkbook.Worksheets("Sheet1").ActiveCell.PasteSpecial xlPasteValues
    '++ Copy to saved range
    r.PasteSpecial xlPasteValues
    
    

    完整的子:

    Option Explicit
    
    Sub Get_Data_From_File()
    Dim FiletoOpen As Variant
    Dim OpenBook As Workbook
    Application.ScreenUpdating = False
    
    ' ++ make a note of current active cell before opening file
    Dim r As Range
    Set r = ActiveCell
    
    
    FiletoOpen = Application.GetOpenFilename(Title:="Browse for your file & Import Range", FileFilter:="Excel Files(*.xlsx),*xlsx*")
    
    If FiletoOpen <> False Then
    
    Set OpenBook = Application.Workbooks.Open(FiletoOpen)
    OpenBook.Sheets(1).Range("D1:D100").Copy
    
    '-- ThisWorkbook.Worksheets("Sheet1").ActiveCell.PasteSpecial xlPasteValues
    '++ Copy to saved range
    r.PasteSpecial xlPasteValues
    
    OpenBook.Close False
    End If
    
    Application.ScreenUpdating = True
    
    
    End Sub
    

    【讨论】:

    • 是否也可以选择特定范围,例如在消息窗口中?还是那会是另一个代码?
    • 感谢@LittleCode - 如果您对 taht 感到满意,您可以投票/选择作为解决方案吗?将不胜感激...输入一个范围,您可以使用Application.InputBox 方法 - 他们甚至在微软文档中有一个示例:This example prompts the user to select a cell on Sheet1. The example uses the Type argument to ensure that the return value is a valid cell reference (a Range object). VB Copy Worksheets("Sheet1").Activate Set myCell = Application.InputBox( _ prompt:="Select a cell", Type:=8)
    • ^ 格式不太好 - 请检查:docs.microsoft.com/en-us/office/vba/api/…
    猜你喜欢
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多