[英]Excel VBA paste special disjoint range from one workbook to another
[英]How do I use the Inputbox in VBA to obtain a range from a user from a already open Excel workbook and paste it into another, standardized workbook?
我試圖在 Excel 工作簿 A 中按下按鈕時提示用戶轉到他們想要在其計算機上已經打開的任何工作簿,選擇文本范圍(不考慮用戶大小)。 然后,我希望將該范圍粘貼到 Excel 工作簿 A 中,從工作簿 A 的單元格 A9 開始。目標是從一個已經打開的標准化 Excel 表中獲取一堆名稱,並將其放入一個主 Excel 表中。
我有以下代碼:
Option Explicit
Sub selectRange()
Dim rng As Range
Set rng = Application.InputBox("Select your range", , , , , , , 8)
If rng Is Nothing Then
MsgBox "Please Select the Range:"
End If
Range(rng).Copy
Range("a9").Select
Range(rng).Paste
End Sub
如果您需要任何澄清,請告訴我!
我遇到的一個問題是它會提示用戶選擇數據,但數據不會顯示在工作簿 A 中,也不會粘貼到任何地方。
我認為這樣的事情應該讓你接近
Dim lRange As Range
On Error Resume Next
Set lRange = Application.InputBox(Title:="Select Range", Prompt:="Select a Range", Type:=8)
'Type 8 is range, see details here: https://docs.microsoft.com/en-us/office/vba/api/excel.application.inputbox
On Error GoTo 0
If Not lRange Is Nothing Then
Set lRange = lRange.Areas(1) 'Just keep the first area if more than 1 is selected
Workbooks("A").Worksheets("CollectionSheet").Range("A9").Resize(lRange.Rows.Count, lRange.Columns.Count).Value2 = lRange.Value2
End If
如果我理解你想要做什么,你會想讓“A9”動態化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.