我目前正在将整个工作表从文件A复制到文件B.一些工作表只需复制值而不复制公式。有一个特殊的工作表,我需要复制格式和公式,但我不希望公式引用原始文件。我希望它引用新文件中的工作表..如何复制工作表/公式,而不是引用原始文件。
下面我有代码复制工作表,然后只用一个工作表的值替换公式。
from Tkinter import *
class FullScreenApp(object):
def __init__(self, master, **kwargs):
self.master=master
pad=3
self._geom='200x200+0+0'
master.geometry("{0}x{1}+0+0".format(
master.winfo_screenwidth()-pad, master.winfo_screenheight()-pad))
master.bind('<Escape>',self.toggle_geom)
def toggle_geom(self,event):
geom=self.master.winfo_geometry()
print(geom,self._geom)
self.master.geometry(self._geom)
self._geom=geom
root = Tk()
app = FullScreenApp(root)
t = Text(root)
t.pack()
#Import file
with open('data.txt') as f:
for line in f:
t.insert(END, line)
#Search terms - Leave blank if not required
search_term0 = '0xCAFE'
search_term1 = '0x0011'
search_term2 = '0x961E'
search_term3 = '0x0000'
search_term4 = ''
#Assigns highlighted colours for terms not blank
t.tag_config(search_term0, background='red')
if search_term1 != '':
t.tag_config(search_term1, background='red')
if search_term2 != '':
t.tag_config(search_term2, background='red')
if search_term3 != '':
t.tag_config(search_term3, background='red')
if search_term4 != '':
t.tag_config(search_term4, background='red')
#Define search
#Requires text widget, the keyword, and a tag
def search(text_widget, keyword, tag):
pos = '1.0'
while True:
idx = text_widget.search(keyword, pos, END)
if not idx:
break
pos = '{}+{}c'.format(idx, len(keyword))
text_widget.tag_add(tag, idx, pos)
#Search for terms that are not blank
search(t, search_term0, search_term0)
if search_term1 != '':
search(t, search_term1, search_term1)
if search_term2 != '':
search(t, search_term2, search_term2)
if search_term3 != '':
search(t, search_term3, search_term3)
if search_term4 != '':
search(t, search_term4, search_term3)
root.mainloop()
尝试解决方案pasteformulas时出错
xlWorksheetSource = xlWorkbookSource.Sheets(sheet.Name)
xlWorksheetSource.Copy(After:=xlWorkbookDestination.Worksheets(xlWorkbookDestination.Worksheets.Count))
xlWorkDestSource = xlWorkbookDestination.Worksheets(xlWorkbookDestination.Worksheets.Count)
ws = xlWorkDestSource
With ws.UsedRange
.Copy()
.PasteSpecial(Paste:=XlPasteType.xlPasteValues, _
Operation:=XlPasteSpecialOperation.xlPasteSpecialOperationNone, SkipBlanks:=False, Transpose:=False)
xlApp.CutCopyMode = False
End With
结束如果
答案 0 :(得分:1)
将PasteSpecial的第一个参数值更改为xlPasteFormulas
.PasteSpecial(Paste:=XlPasteType.xlPasteFormulas, _
Operation:=XlPasteSpecialOperation.xlPasteSpecialOperationNone, SkipBlanks:=False, Transpose:=False)
xlApp.CutCopyMode = False
答案 1 :(得分:1)
这是VBA中的一种方法,希望您可以轻松转换.Net,我没有在这台机器上安装VisualStudio,因此无法编写VB.Net。另外我对VB.Net不太满意
首先加载数组中的所有公式(它也加载值,以防你有混合单元格),然后在数组中只更新工作表名称(或wb名称或任何你想要的),然后将数组转储到目的地。然后再次复制并仅使用pastespecial格式。
Sub test()
Dim arr
Dim strSheetFrom As String
Dim strSheetTo As String
Dim lctrRow As Long
Dim lctrCol As Long
strSheetFrom = "Sheet3"
strSheetTo = "Sheet2"
'/ First: Update your formulas in array and simply dump them.
arr = Sheet2.UsedRange.Formula
For lctrRow = LBound(arr, 1) To UBound(arr, 1)
For lctrCol = LBound(arr, 2) To UBound(arr, 2)
arr(lctrRow, lctrCol) = Replace(arr(lctrRow, lctrCol), strSheetFrom, strSheetTo)
Next
Next
Sheet1.Cells(1, 1).Resize(UBound(arr, 1), UBound(arr, 2)) = arr
'/ Now formats
Sheet2.UsedRange.Copy
Sheet1.Cells(1, 1).PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End Sub