gpt4 book ai didi

excel - 将不同工作簿中的数据复制并粘贴到多个单元格

转载 作者:行者123 更新时间:2023-12-02 04:19:47 24 4
gpt4 key购买 nike

我是 VBA 的新手,我正在尝试将数据从一个工作簿复制到另一个工作簿。在我的“复制工作簿”wb1(.dbf 格式)中,我有 3 组数据要复制到我的“粘贴工作簿”wb2(.xlsm 格式)。

我需要将三个“ block ”(我称之为带)数据从一个 WB 复制到另一个。波段 1 的范围为“C2:M5”,波段 2 的范围为“N2:X5”,波段 3 的范围为“Y2:AI5”。

我希望用户能够选择粘贴每个波段的位置,最好是让他只选择每个波段范围的第一个单元格。

到目前为止,我的代码如下所示。它一次只能复制和粘贴一个波段,这意味着我必须运行它 3 次。我的目

我希望这已经足够清楚了。预先感谢您的所有帮助!

Sub CopyData()

' Keyboard shortcut: Ctrl+d

Dim band As Integer
Dim wb1 As Workbook
Dim wb2 As Workbook

Set band = InputBox("Choose bands 1, 2 or 3:")

Set wb1 = Workbooks.Open("C:\Users\mmm\CopyFile.dbf") ' File I want to copy the data from
Set wb2 = Workbooks.Open("C:\Users\mmm\PasteFile.xlsm") ' File I want to paste my data to

If band = 1 Then

wb1.Worksheets(dbf_name).Range("C2:M5").Copy 'Range of Band1 to copy
wb1.Close savechanges:=False
Application.DisplayAlerts = True

Application.DisplayAlerts = False
ActiveSheet.Paste
Application.CutCopyMode = False
Range("A1").Select

ElseIf band = 2 Then

wb1.Worksheets(dbf_name).Range("N2:X5").Copy 'Range of Band2 to copy
wb1.Close savechanges:=False
Application.DisplayAlerts = True

Application.DisplayAlerts = False
ActiveSheet.Paste
Application.CutCopyMode = False
Range("A1").Select

ElseIf band = 3 Then

wb1.Worksheets(dbf_name).Range("Y2:AI5").Copy 'Range of Band3 to copy
wb1.Close savechanges:=False
Application.DisplayAlerts = True

Application.DisplayAlerts = False
ActiveSheet.Paste
Application.CutCopyMode = False
Range("A1").Select

End If
End Sub

[更新最终代码]

Sub CopyData()

' Keyboard shortcut: Ctrl+d

Dim dbf_path As String
Dim dbf_name As String
Dim rCopy As Range
Dim i As Long
Dim rPaste As Range
Dim wb1 As Workbook

dbf_path = "C:\Users\mmm\CopyFile.dbf"
dbf_name = "filename_dbf"
Set wb1 = Workbooks.Open(dbf_path)

ThisWorkbook.Activate

Set rCopy = wb1.Worksheets(dbf_name).Range("C2:M5,N2:X5,Y2:AI5")

For i = 1 To rCopy.Areas.Count 'loop through each distinct block or area
Set rPaste = Application.InputBox("Enter starting cell for range " & i, Type:=8) 'invite paste cell, specifying range input
If rPaste.Count > 1 Then Set rPaste = rPaste(1) 'if more than one cell selected use the first one
rCopy.Areas(i).Copy rPaste 'paste
Next i

wb1.Close savechanges:=False

End Sub

最佳答案

这是一个简单的例子,展示了如何通过输入框为每个 block 设置粘贴目标。希望您可以根据您的精确设置对其进行调整。

Sub x()

Dim rCopy As Range, i As Long, rPaste As Range

Set rCopy = Range("C2:M5,N2:X5,Y2:AI5") 'define ranges to copy

For i = 1 To rCopy.Areas.Count 'loop through each distinct block or area
Set rPaste = Application.InputBox("Enter starting cell for range " & i, Type:=8) 'invite paste cell, specifying range input
If rPaste.Count > 1 Then Set rPaste = rPaste(1) 'if more than one cell selected use the first one
rCopy.Areas(i).Copy rPaste 'paste
Next i

End Sub

关于excel - 将不同工作簿中的数据复制并粘贴到多个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61256033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com