【发布时间】:2016-08-03 19:22:07
【问题描述】:
我是编写 excel 宏的新手,但我正在努力完成工作。无论如何,我正在尝试将数据从 2 个工作表导出到另一个工作簿中的另外 2 个工作表,但是当我到达清除剪贴板的地步时,它根本不起作用 :( 任何人都可以帮助我吗?这是我的代码:
Sub manufacturer_export()
Dim Return_Shipment_Template_Proba As Workbook
Dim wbTarget As Workbook
Dim activeSht As Worksheet
Set Return_Shipment_Template_Proba = ThisWorkbook
Set wbTarget = Workbooks.Open("file:///F:\FEM Backup\20.07.2016\08_GT Returns & Sample tracking\01_Returns Summary\Returns Summary.xlsm")
Set activeSht = Return_Shipment_Template_Proba.Sheets("GT_GU10_Lamp")
With Return_Shipment_Template_Proba.Sheets("GT_GU10_Lamp").Range("A3:X3")
' Column B may be empty. If so, xlDown will return cell C65536
' and whole empty column will be copied... prevent this.
If .Cells(1, 24).Value = "" Then
'Nothing in this column.
'Do nothing.
Else
Return_Shipment_Template_Proba.Sheets("GT_GU10_Lamp").Range(.Cells(1, 24), .End(xlDown)).Copy
wbTarget.Sheets("GT_GU10_Lamp").Activate
ActiveCell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
End If
End With
Application.CutCopyMode = False
Set activeSht = Return_Shipment_Template_Proba.Sheets("GT_COB_GU10_Lamp")
With Return_Shipment_Template_Proba.Sheets("GT_COB_GU10_Lamp").Range("A3:X3")
' Column B may be empty. If so, xlDown will return cell C65536
' and whole empty column will be copied... prevent this.
If .Cells(1, 24).Value = "" Then
'Nothing in this column.
'Do nothing.
Else
Return_Shipment_Template_Proba.Sheets("GT_COB_GU10_Lamp").Range(.Cells(1, 24), .End(xlDown)).Copy
wbTarget.Sheets("GT_COB_GU10_Lamp").Activate
ActiveCell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
End If
End With
wbTarget.Save
End Sub
【问题讨论】:
-
“根本行不通”到底是什么意思?某处有错误吗?我也不确定您所说的“清除剪贴板”是什么意思。你指的是
Return_Shipment_Template.Application.CutCopyMode = False吗? -
代替
Return_Shipment_Template.Application.CutCopyMode = False试试Application.CutCopyMode = False -
当我说它不起作用时,我的意思是它将相同的数据从第一个源工作表复制到两个目标工作表。
-
您是否尝试过使用 Worksheet 变量,例如
Dim activeSht As Worksheet Set activeSht = Return_Shipment_Template.Sheets("GT_GU10_Lamp") With activeSht.Range("A3:X3") ... End With并重复第二张工作表 -
Range(.Cells(1, 24), .End(xlDown)).Copy,这行很麻烦:第一个范围没有被点“。”引用并且单元格是......所以它当然会从活动工作表中复制。