Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 3082|回复: 13

[已解决]求助高手们来看看。

[复制链接]
发表于 2013-10-21 20:06 | 显示全部楼层 |阅读模式
本帖最后由 exlover 于 2013-10-21 23:23 编辑

求助可否办到。
最佳答案
2013-10-21 20:48
  1. Sub test1()
  2.     Dim rg As Range, rg2 As Range
  3.     Dim lLastRow As Long
  4.     Dim arr
  5.     On Error Resume Next
  6.     Set rg = Application.InputBox("请选择要复制的多区域", Type:=8)
  7.     If Err.Number <> 0 Then Exit Sub

  8.     With Worksheets("sheet3")
  9.         lLastRow = .Cells(Rows.Count, 1).End(xlUp).Row + 2
  10.         For Each rg2 In rg.Areas
  11.             arr = rg2.Value
  12.             With .Cells(lLastRow, 1)
  13.                 If IsArray(arr) Then
  14.                     .Resize(UBound(arr), UBound(arr, 2)).Value = arr
  15.                 Else
  16.                     .Value = arr
  17.                 End If
  18.             End With
  19.             lLastRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1
  20.         Next
  21.     End With
  22.     MsgBox "复制完成"
  23. End Sub

  24. Sub test2()
  25.     Dim rg As Range, rg2 As Range
  26.     Dim lLastRow As Long
  27.     Dim arr
  28.     On Error Resume Next
  29.     Set rg = Application.InputBox("请选择要复制的多区域", Type:=8)
  30.     If Err.Number <> 0 Then Exit Sub
  31.     With Worksheets("sheet3")
  32.         For Each rg2 In rg.Areas
  33.             arr = rg2.Value
  34.             lLastRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1
  35.             With .Cells(lLastRow, 1)
  36.                 If IsArray(arr) Then
  37.                     .Resize(UBound(arr), UBound(arr, 2)).Value = arr
  38.                 Else
  39.                     .Value = arr
  40.                 End If
  41.             End With
  42.             lLastRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1
  43.         Next
  44.     End With
  45.     MsgBox "复制完成"
  46. End Sub
复制代码

EPtwo.rar

3.55 KB, 下载次数: 13

代码完成附件效果

EPone.rar

3.34 KB, 下载次数: 7

代码完成附件效果

发表于 2013-10-21 20:42 | 显示全部楼层
  1. Sub test1()
  2.     Dim rg As Range, rg2 As Range
  3.     Dim lLastRow As Long
  4.     Dim arr
  5.     On Error Resume Next
  6.     Set rg = Application.InputBox("请选择要复制的多区域", Type:=8)
  7.     If Err.Number <> 0 Then Exit Sub

  8.     With Worksheets("sheet3")

  9.         lLastRow = .Cells(Rows.Count, 1).End(xlUp).Row + 2
  10.         For Each rg2 In rg.Areas
  11.             arr = rg2.Value
  12.             If IsArray(arr) Then
  13.                 .Cells(lLastRow, 1).Resize(UBound(arr), UBound(arr, 2)).Value = arr
  14.             Else
  15.                 .Cells(lLastRow, 1).Value = arr
  16.             End If
  17.             lLastRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1
  18.         Next
  19.     End With
  20.     MsgBox "复制完成"
  21. End Sub
复制代码
回复

使用道具 举报

发表于 2013-10-21 20:48 | 显示全部楼层    本楼为最佳答案   
  1. Sub test1()
  2.     Dim rg As Range, rg2 As Range
  3.     Dim lLastRow As Long
  4.     Dim arr
  5.     On Error Resume Next
  6.     Set rg = Application.InputBox("请选择要复制的多区域", Type:=8)
  7.     If Err.Number <> 0 Then Exit Sub

  8.     With Worksheets("sheet3")
  9.         lLastRow = .Cells(Rows.Count, 1).End(xlUp).Row + 2
  10.         For Each rg2 In rg.Areas
  11.             arr = rg2.Value
  12.             With .Cells(lLastRow, 1)
  13.                 If IsArray(arr) Then
  14.                     .Resize(UBound(arr), UBound(arr, 2)).Value = arr
  15.                 Else
  16.                     .Value = arr
  17.                 End If
  18.             End With
  19.             lLastRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1
  20.         Next
  21.     End With
  22.     MsgBox "复制完成"
  23. End Sub

  24. Sub test2()
  25.     Dim rg As Range, rg2 As Range
  26.     Dim lLastRow As Long
  27.     Dim arr
  28.     On Error Resume Next
  29.     Set rg = Application.InputBox("请选择要复制的多区域", Type:=8)
  30.     If Err.Number <> 0 Then Exit Sub
  31.     With Worksheets("sheet3")
  32.         For Each rg2 In rg.Areas
  33.             arr = rg2.Value
  34.             lLastRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1
  35.             With .Cells(lLastRow, 1)
  36.                 If IsArray(arr) Then
  37.                     .Resize(UBound(arr), UBound(arr, 2)).Value = arr
  38.                 Else
  39.                     .Value = arr
  40.                 End If
  41.             End With
  42.             lLastRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1
  43.         Next
  44.     End With
  45.     MsgBox "复制完成"
  46. End Sub
复制代码
回复

使用道具 举报

发表于 2013-10-21 20:49 | 显示全部楼层
test1是epTWO,弹出对话框后,直接选中区域操作即可
test2是epone,弹出对话框后,直接选中区域操作即可
回复

使用道具 举报

 楼主| 发表于 2013-10-21 21:03 | 显示全部楼层
hwc2ycy 发表于 2013-10-21 20:49
test1是epTWO,弹出对话框后,直接选中区域操作即可
test2是epone,弹出对话框后,直接选中区域操作即可

非常感谢版主的解答,操作方式可以这样么.

我先选取好单元格,然后做2个按钮,用单击按钮来完成要求。
这样操控性比较好。版主老师可否再修改下代码。
回复

使用道具 举报

 楼主| 发表于 2013-10-21 21:08 | 显示全部楼层
本帖最后由 exlover 于 2013-10-21 21:20 编辑
hwc2ycy 发表于 2013-10-21 20:48


你好,版主老师。
1.本求助中,可否把代码用按钮来控制。即先选取好要复制的单元格,然后单击按钮,实现复制粘贴效果。这样实际使用起来比较方便。
2.实际代码运行后不能完成以上2个附件效果。总计是2个选取方式:
第1个附件选取单元格是一次选取三个单元格。它的粘贴结果到sheet2是紧挨着一行一行的。
第2个附件选取单元格是一次选取同行的三段三个单元格,它的粘贴结果复制到sheet3。且每选取一次,它的结果需要隔一行放置。与第一个附件紧挨一行一行放置有些区别。
回复

使用道具 举报

发表于 2013-10-21 21:20 | 显示全部楼层
eponetwo.rar (24.07 KB, 下载次数: 8)
回复

使用道具 举报

 楼主| 发表于 2013-10-21 21:38 | 显示全部楼层
本帖最后由 exlover 于 2013-10-21 23:17 编辑
hwc2ycy 发表于 2013-10-21 21:20

谢谢老师,就是第一次复制的时候,sheet3里的结果是从第一行开始的比较好。

比如我把sheet3里的数据全部清空后,它总是从第三行开始的。
回复

使用道具 举报

发表于 2013-10-21 22:44 | 显示全部楼层
exlover 发表于 2013-10-21 21:38
就是有一点点问题及咨询下。
1:2个文件里选取后复制第一次的时候,结果都不是从第一行开始的放置的。 ...

你每次都要从第一行开始写?
那原有的数据清空后你可不要说要恢复数据。
回复

使用道具 举报

 楼主| 发表于 2013-10-21 23:21 | 显示全部楼层
hwc2ycy 发表于 2013-10-21 22:44
你每次都要从第一行开始写?
那原有的数据清空后你可不要说要恢复数据。

谢谢。也就是我把sheet3里的数据全部清空后。
做这样的复制,是从sheet3的第三行开始填入的。
不过,就是这里一点点。其它都很好。也基本达到。就是多了一步把sheet3里的前2行空格删除下。
已设最佳,再次感谢!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-20 01:08 , Processed in 0.351607 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表