Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 使用lotusscript计算excel中的单元格总数_Vba_Excel - Fatal编程技术网

Vba 使用lotusscript计算excel中的单元格总数

Vba 使用lotusscript计算excel中的单元格总数,vba,excel,Vba,Excel,我有一个表单,表单中有一个包含excel文件的对象(工作表)。 然后我有一个按钮,可以计算工作表中字段的总和,并在表单的字段中显示它的总和。我该如何编写代码?这个想法是这样的 totalr=总和(A2:A51) doc.total=totalr如果您在Notes客户端使用的表单上执行此操作,并且运行Notes客户端的计算机具有Excel,则可以使用VBA。只需将文件解压缩到用户的硬盘上,用Excel打开,在列的底部添加一个单元格,并在那里放置一个SUM()函数,获取新单元格的值,更新UIDOC中

我有一个表单,表单中有一个包含excel文件的对象(工作表)。 然后我有一个按钮,可以计算工作表中字段的总和,并在表单的字段中显示它的总和。我该如何编写代码?这个想法是这样的

totalr=总和(A2:A51)


doc.total=totalr

如果您在Notes客户端使用的表单上执行此操作,并且运行Notes客户端的计算机具有Excel,则可以使用VBA。只需将文件解压缩到用户的硬盘上,用Excel打开,在列的底部添加一个单元格,并在那里放置一个SUM()函数,获取新单元格的值,更新UIDOC中的字段,然后删除临时文件

下面的代码假定您有一个表单,其中包含一个名为“excelfile”的RichText字段(包含Excel文件),一个名为“Sum”的字段(包含Excel文件中的总和值),以及一个包含代码的按钮。此外,文件必须与附件一起保存,代码才能正常工作

希望能有帮助

Sub Click(Source As Button)

On Error Goto errorhandler

Dim s As New NotesSession
Dim ws As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Set uidoc = ws.CurrentDocument
Dim doc As NotesDocument
Set doc = uidoc.Document
Dim rtitem As NotesRichTextItem
Dim isfileopen As Boolean

Dim filename As String, rows As Double, cols As Double, colToSum As String, c As Integer
Dim xlApp As Variant, xlWorkbook As Variant, xlSheet As Variant, pathname As String

isfileopen = False

'convert column letter to number
colToSum = "A"
c = Asc(colToSum) - 64

'extract file
pathname = s.GetEnvironmentString( "Directory", True ) & "\"
Set rtitem = doc.GetFirstItem("excelfile")
If Not (doc.HasEmbedded) Then
    Msgbox "There are no file attachments on this document"
    Goto ExitGracefully
End If  
Forall o In rtitem.EmbeddedObjects
    If ( o.Type = EMBED_ATTACHMENT ) Then
        filename = pathname & o.Name            
        Call o.ExtractFile( filename )          
    End If
End Forall

'open file
Print "Opening file " & filename & "..."
Set xlApp = CreateObject("Excel.Application") 
xlApp.Workbooks.Open filename
isfileopen = True

'get sheet
Print "Gathering data ..."
Set xlWorkBook = xlApp.ActiveWorkbook 
Set xlSheet = xlWorkBook.ActiveSheet 
xlSheet.Cells.SpecialCells(11).Activate
rows = xlApp.ActiveWindow.ActiveCell.Row 
cols = xlApp.ActiveWindow.ActiveCell.Column 

'add a row and put the sum() formula there
Print "Computing sum with Excel..."
xlSheet.Cells( rows+1, c ).value = "=SUM(" & colToSum & "1:" & colToSum & Cstr(rows) & ")"

'update UI doc
Call uidoc.FieldSetText( "Sum", Cstr(xlSheet.Cells( rows+1, c ).value) )

exitgracefully:

'close excel
Print "Closing Excel..."
If Not xlWorkbook Is Nothing Then
    xlWorkbook.Close False 
End If
If Not xlApp Is Nothing Then
    xlApp.DisplayAlerts = False
    xlApp.Quit
    Set xlApp = Nothing 
End If 

If isfileopen Then
    'remove temp file
    Print "Removing " & filename
    Kill filename
End If

'done
Print "Done"

Exit Sub
errorhandler:
Msgbox Error & " line " & Erl
Exit Sub
End Sub