作者pomelolin (勇气、智慧、善良)
看板Office
标题[问题] VBA workbooks不重新开启档案
时间Fri Aug 10 23:59:48 2018
(若是和其他不同软体互动之问题 请记得一并填写)
软体:excel
版本:2013
这是别人写的VBA, 现在想自己修改, 是从A档案各个sheet不同位置
copy到B档案各sheet不同位置, 有个表格是对照表
原本写的档案都得重新开启A档案和B档案, 但其实原本都会同时开着,
不想每次run程式都要重开档案, 上网研究好久, 依然不得其门而入
把Open删掉, 程式就出现错误了@@
麻烦版上各位先进帮忙~~
Dim scWB As Workbook, tgWB As Workbook
Dim scWS As Worksheet, tgWS As Worksheet, ws As Worksheet
Dim row As Integer
Set ws = ThisWorkbook.Worksheets("XXX")
Set scWB = Application.Workbooks.Open(ws.Cells(1, 3).Value)
Set tgWB = Application.Workbooks.Open(ws.Cells(2, 3).Value)
For row = 4 To 63
If (Len(ws.Cells(row, 2).Value) > 0) Then
ws.Cells(row, 6).Value = "GO"
Set scWS = scWB.Worksheets(ws.Cells(row, 2).Value)
Set tgWS = tgWB.Worksheets(ws.Cells(row, 4).Value)
scWS.Range(ws.Cells(row, 3).Value).Copy
tgWS.Range(ws.Cells(row, 5).Value).PasteSpecial xlPasteValues
ws.Cells(row, 6).Value = "OK"
'scWB.Close
'tgWB.Close
End If
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 219.70.232.113
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Office/M.1533916790.A.EF3.html
1F:→ soyoso: 1.回圈於workbooks的集合,判断workbook.name是否符合原文 08/11 00:34
2F:→ soyoso: 内的ws.cells(1,3)或cells(2,3),符合就set 变数= 08/11 00:35
3F:→ soyoso: workbooks(完整档名) 08/11 00:37
4F:→ soyoso: 并设个变数(可数字、文字或布林),用於当回圈执行完时都没 08/11 00:40
5F:→ soyoso: 有符合的条件,再判断该变数,来判断是否要以workbooks. 08/11 00:41
6F:→ soyoso: open 08/11 00:41
7F:→ soyoso: 2.直接set 变数=workbooks(完整档名),没开启变数会是 08/11 00:43
8F:→ soyoso: nothing,就可以判断当变数is nothing时workbooks.open 08/11 00:44
9F:→ soyoso: 但因set 变数一个无开启的档案,会出现阵列索引超出范围的 08/11 00:45
10F:→ soyoso: 错误讯息,因此上方要加上on error resume next 08/11 00:46