Excel精英培训网

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

[已解决]请问将其他工作表的内容复制粘贴到同一张工作表,为什么会出错

[复制链接]
发表于 2015-10-27 12:43 | 显示全部楼层 |阅读模式
我的目的是将不同工作表的内容复制粘贴到指定工作表的指定区域内。
奇怪的是,我运行完这串代码,只有book4的sheet2 被复制粘贴到了Autoload1 的sheet3上,另一个没成功。
自己也实在看不出来问题在哪儿,能否请大神们帮我看下,谢谢。

sourcing
Destination
DIR
file name
sheet name
range
file name
sheet name
range
C:\Users\19008483\Desktop
Book4.xls
Sheet1
a1:q500
Autoload1.xlsm
sheet2
a1
C:\Users\19008483\Desktop
Book4.xls
Sheet2
a1:q501
Autoload1.xlsm
sheet3
a1


Sub test()
Dim name As String, i%, file, sht, rng, destination, desfile, dessht As String

Application.ScreenUpdating = False
On Error Resume Next

For i = 6 To 10
     name = Range("b" & i).Text & "\" & Range("c" & i).Text
     file = Range("c" & i).Text
     sht = Range("d" & i).Text
     rng = Range("e" & i).Text
     destination = Range("i" & i).Text
     desfile = Range("g" & i).Text
     dessht = Range("h" & i).Text
         
          If file = "" Then GoTo 100
         Workbooks.OpenText Filename:=name
   
         Windows(file).Activate
         Sheets(sht).Range(rng).Select
         Selection.Copy
         Windows(desfile).Activate
   
         Sheets(dessht).Select
         Range(destination).Select
         ActiveSheet.Paste
   
         Workbooks.OpenText Filename:=name
   
         ActiveWindow.Close
      
          Windows("Autoload1.xlsm").Activate
          Sheets("Sheet1").Select
      
Next

100
Application.ScreenUpdating = True
End Sub

最佳答案
2015-10-27 15:43
  1. Sub test()

  2. Dim name As String, i%, file, sht, rng, destination, desfile, dessht As String


  3. Application.ScreenUpdating = False
  4. Application.DisplayAlerts = False
  5. 'On Error Resume Next


  6. For i = 6 To 10
  7.      name = Range("b" & i).Text & "" & Range("c" & i).Text
  8.      file = Range("c" & i).Text
  9.      sht = Range("d" & i).Text
  10.      rng = Range("e" & i).Text
  11.      destination = Range("i" & i).Text
  12.      desfile = Range("g" & i).Text
  13.      dessht = Range("h" & i).Text
  14.          
  15.           If file = "" Then GoTo 100
  16.          Workbooks.OpenText Filename:=name
  17.    
  18.          Windows(file).Activate
  19.          Sheets(sht).Select
  20.          Range(rng).Select
  21.          Selection.Copy
  22.          Windows(desfile).Activate
  23.    
  24.          Sheets(dessht).Select
  25.          Range(destination).Select
  26.          Selection.PasteSpecial Paste:=xlPasteValues
  27.    
  28.          'Workbooks.OpenText Filename:=name
  29.    
  30.          Windows(file).Close
  31.       
  32.           Windows("Autoload1.xlsm").Activate
  33.           Sheets("Sheet1").Select
  34.       
  35. Next


  36. 100

  37. Application.ScreenUpdating = True
  38. Application.DisplayAlerts = True

  39. End Sub
复制代码
 楼主| 发表于 2015-10-27 12:46 | 显示全部楼层
附件

Desktop.zip

27.91 KB, 下载次数: 6

回复

使用道具 举报

发表于 2015-10-27 15:43 | 显示全部楼层    本楼为最佳答案   
  1. Sub test()

  2. Dim name As String, i%, file, sht, rng, destination, desfile, dessht As String


  3. Application.ScreenUpdating = False
  4. Application.DisplayAlerts = False
  5. 'On Error Resume Next


  6. For i = 6 To 10
  7.      name = Range("b" & i).Text & "" & Range("c" & i).Text
  8.      file = Range("c" & i).Text
  9.      sht = Range("d" & i).Text
  10.      rng = Range("e" & i).Text
  11.      destination = Range("i" & i).Text
  12.      desfile = Range("g" & i).Text
  13.      dessht = Range("h" & i).Text
  14.          
  15.           If file = "" Then GoTo 100
  16.          Workbooks.OpenText Filename:=name
  17.    
  18.          Windows(file).Activate
  19.          Sheets(sht).Select
  20.          Range(rng).Select
  21.          Selection.Copy
  22.          Windows(desfile).Activate
  23.    
  24.          Sheets(dessht).Select
  25.          Range(destination).Select
  26.          Selection.PasteSpecial Paste:=xlPasteValues
  27.    
  28.          'Workbooks.OpenText Filename:=name
  29.    
  30.          Windows(file).Close
  31.       
  32.           Windows("Autoload1.xlsm").Activate
  33.           Sheets("Sheet1").Select
  34.       
  35. Next


  36. 100

  37. Application.ScreenUpdating = True
  38. Application.DisplayAlerts = True

  39. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2015-10-28 15:16 | 显示全部楼层
谢谢!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-21 12:07 , Processed in 0.290645 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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