繁体   English   中英

对工作簿中的单元格求和,然后粘贴到另一个工作簿中

[英]Sum cells from a workbook and paste in another workbook

我有2个工作簿,我需要在另一个工作簿的一个单元格中复制2个单元格的总和。 我正常复制值,但在一种情况下,我需要复制2个单元格的总和。 我正在尝试使用此代码,但是它不起作用:

Sub Excel_Excel()
Dim x As ThisWorkbook
Dim y As Workbook
Dim Total As Double

Application.ScreenUpdating = False
Application.DisplayAlerts = False

''allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
''make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
''determine what choice the user made
If intChoice <> 0 Then
''get the file path selected by the user
    strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)

End If

'## Open workbook:
Set y = Workbooks.Open(strPath)

'Now, copy what you want from y:
y.Sheets("Deducciones").Range("F70:F86").Copy
'Now, paste to ThisWorksheet x:
ThisWorkbook.Sheets("Deducciones").Range("rngdeducciónID").PasteSpecial    xlPasteValues

'Define range that you need to sum, sum it and copy:
range1 = y.Sheets("Deducciones").Range("F87:F88")
Total = WorksheetFunction.Sum(Range(range1))
y.Sheets("Deducciones").Total.Copy
'Now, paste to ThisWorksheet x:
ThisWorkbook.Sheets("Deducciones").D86.PasteSpecial

'Close y:
y.Close

End Sub

该代码不起作用:Total = WorksheetFunction.Sum(Range(range1))

如下所示如何操作:

Sub Excel_Excel()
Dim x As ThisWorkbook
Dim y As Workbook
Dim Total As Double

Application.ScreenUpdating = False
Application.DisplayAlerts = False

''allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
''make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
''determine what choice the user made
If intChoice <> 0 Then
''get the file path selected by the user
    strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)

End If

'## Open workbook:
Set y = Workbooks.Open(strPath)

'Now, copy what you want from y:
y.Sheets("Deducciones").Range("F70:F86").Copy
'Now, paste to ThisWorksheet x:
ThisWorkbook.Sheets("Deducciones").Range("rngdeducciónID").PasteSpecial xlPasteValues

'Define range that you need to sum, sum it and add to variable Total:
Total = WorksheetFunction.Sum(y.Sheets("Deducciones").Range("F87:F88"))
'Now, pass the value of Total to ThisWorksheet x:
ThisWorkbook.Sheets("Deducciones").Range("D86").Value = Total

'Close y:
y.Close
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM