我正在尝试将特定列值从一个excel文件复制到另一个excel文件。这就是我尝试过的。
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Open("C:\A.xls")
Set objWorkbook2 = objExcel.Workbooks.Open("C:\C.xls")
Set objWorksheet = objWorkbook.Worksheets(1)
objWorksheet.Activate
Set objRange = objWorksheet.Range("A:Q").EntireColumn
objRange.Copy
Set objWorksheet2 = objWorkbook2.Worksheets(1)
objWorksheet.Activate
objWorksheet.Range("A:B").EntireColumn.Copy objWorksheet2.Paste
objWorksheet2.Range ("A1")
objWorksheet.Columns("Q").EntireColumn.Copy objWorksheet2.Paste
objWorksheet2.Range ("C1")
objWorkbook2.Save
objWorkbook2.Close
我的疑问是,Q栏包含公式。因此,复制列时,只复制公式而不复制值。 请帮助复制单元格中的值,但不能复制公式。
提前致谢。
答案 0 :(得分:2)
试试这个。你应该养成声明变量的习惯(以及格式化你的代码)。
Sub x()
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Open("C:\A.xls")
Set objWorkbook2 = objExcel.Workbooks.Open("C:\C.xls")
Set objWorksheet = objWorkbook.Worksheets(1)
Set objRange = objWorksheet.Range("A:Q").EntireColumn
objRange.Copy 'not sure what this does
Set objWorksheet2 = objWorkbook2.Worksheets(1)
objWorksheet.Range("A:B").Copy objWorksheet2.Range("A1")
objWorksheet.Columns("Q").EntireColumn.Copy
objWorksheet2.Range("C1").PasteSpecial xlValues
objWorkbook2.Save
objWorkbook2.Close
End Sub