vba复制整个sheet内容_VBA将数据从一个工作表复制到另一个工作表

我对VBA很新,需要一些项目帮助 . 我需要编写一个宏来读取列C中的工作表名称,并将源工作簿中的值粘贴到目标工作簿中的范围,该范围在列D中指定 .

因此,例如,它需要复制Myworkbook book的Sheet2中的数据,并将其粘贴到他们的工作簿Sheet2的范围内 . 范围和工作表编号信息存储在单独工作簿中的位置 .

编辑:我添加了一张wbOpen的图片 . This is it here.

Option Explicit

Sub PasteToTargetRange()

Dim arrVar As Variant 'stores all the sheets to get the copied

Dim arrVarTarget As Variant 'stores names of sheets in target workbook

Dim rngRange As Range 'each sheet name in the given range

Dim rngLoop As Range 'Range that rngRange is based in

Dim wsSource As Worksheet 'source worksheet where ranges are found

Dim wbSource As Workbook 'workbook with the information to paste

Dim wbTarget As Workbook 'workbook that will receive information

Dim strSourceFile As String 'location of source workbook

Dim strTargetFile As String 'location of source workbook

Dim wbOpen As Workbook 'Current open workbook(one with inputs)

Dim wsRange As Range 'get information from source workbook

Dim varRange As Range 'Range where values should be pasted

Dim i As Integer 'counter for For Loop

Dim wbkNewSheet As Worksheet 'create new worksheet if target workbook doesn't have

Dim wsTarget As Worksheet 'target workbook worksheet

Dim varNumber As String 'range to post

Set wbOpen = Workbooks.Open("WorkbookWithRanges.xlsx")

'Open source file

MsgBox ("Open the source file")

strSourceFile = Application.GetOpenFilename

If strSourceFile = "" Then Exit Sub

Set wbSource = Workbooks.Open(strSourceFile)

'Open target file

MsgBox ("Open the target file")

strTargetFile = Application.GetOpenFilename

If strTargetFile = "" Then Exit Sub

Set wbTarget = Workbooks.Open(strTargetFile)

'Activate transfer Workbook

wbOpen.Activate

Set wsRange = ActiveSheet.Range("C9:C20")

Set arrVarTarget = wbTarget.Worksheets

For Each varRange In wsRange

If varRange.Value = 'Target workbook worksheets

varNumber = varRange.Offset(0, -1).Value

Set wsTarget = X.Offset(0, 1)

wsSouce.Range(wsTarget).Value = varNumber

Else

wbkNewSheet = Worksheets.Add

wbkNewSheet.Name = varRange.Value

End If

Next

End Sub

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个示例代码,演示如何使用VBA将两个工作中的数据筛选后复制一个新的工作中: ``` Sub FilterAndCopy() Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet Dim lastRow1 As Long, lastRow2 As Long, lastRow3 As Long Dim i As Long, j As Long '获取需要操作的三个工作对象 Set ws1 = ThisWorkbook.Worksheets("Sheet1") Set ws2 = ThisWorkbook.Worksheets("Sheet2") Set ws3 = ThisWorkbook.Worksheets("Sheet3") '获取两个原始工作的最后一行 lastRow1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row lastRow2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row '清空目标工作 ws3.Cells.ClearContents '复制一个工作中满足条件的数据到目标工作 j = 1 '目标工作的行数 For i = 1 To lastRow1 If ws1.Cells(i, 2) = "条件1" And ws1.Cells(i, 3) > 10 Then ws1.Rows(i).Copy ws3.Rows(j) j = j + 1 End If Next i '复制第二个工作中满足条件的数据到目标工作 For i = 1 To lastRow2 If ws2.Cells(i, 2) = "条件2" And ws2.Cells(i, 3) < 20 Then ws2.Rows(i).Copy ws3.Rows(j) j = j + 1 End If Next i '自适应调整目标工作的列宽 lastRow3 = ws3.Cells(Rows.Count, 1).End(xlUp).Row ws3.Cells.EntireColumn.AutoFit End Sub ``` 在上面的示例代码中,我们首先获取了需要操作的三个工作对象,然后通过Cells函数和End(xlUp)方法获取了两个原始工作的最后一行。接着,我们清空了目标工作,然后依次遍历两个原始工作中的每一行,筛选出满足条件的数据,并使用Copy方法将其复制到目标工作中。最后,我们使用EntireColumn.AutoFit方法自适应调整目标工作的列宽。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值