在Excel VBA中将文本框的值设置为多个其他文本框的总和:

huangapple go评论107阅读模式
英文:

Setting the value of a textbox in Excel VBA to be the sum of multiple other textbox's

问题

我有多个文本框,用于输入数字值,我正在尝试对所有文本框中的值进行求和,并将其设置到另一个文本框,但出现了问题。我附上了我尝试过的代码,任何帮助将不胜感激!

Public Sub SumTotalAmount()
    ThisWorkbook.Worksheets("Sheet 1").TextBox5.Value = ThisWorkbook.Worksheets("Sheet 1").TextBox1 + ThisWorkbook.Worksheets("Sheet 1").TextBox2 + ThisWorkbook.Worksheets("Sheet 1").TextBox3 + ThisWorkbook.Worksheets("Sheet 1").TextBox4
End Sub
英文:

I have multiple text box's that take in numeric values, and I am trying to sum the value of all the textbox's and set it to another textbook but it is not working for some reason. I have attached what I have tried below, any help would be greatly appreciated!

Public Sub SumTotalAmount()
    ThisWorkbook.Worksheets("Sheet 1").TextBox5.Value = ThisWorkbook.Worksheets("Sheet 1").TextBox1+ ThisWorkbook.Worksheets("Sheet 1").TextBox2+ ThisWorkbook.Worksheets("Sheet 1").TextBox3 + ThisWorkbook.Worksheets("Sheet 1").TextBox4
End Sub

答案1

得分: 1

你应该确保在尝试添加文本框内容时使用数值:

Public Sub SumTotalAmount()
    With ThisWorkbook.Worksheets("Sheet 1")
        .TextBox5.Value = GetNumber(.TextBox1) + GetNumber(.TextBox2) + _
                          GetNumber(.TextBox3) + GetNumber(.TextBox4)
    End With
End Sub

' 从文本框中返回数值(如果不是数值或为空,则返回0)
Function GetNumber(objTB As Object)
    Dim v
    v = objTB.Value
    If Len(v) > 0 And IsNumeric(v) Then GetNumber = CDbl(v)
End Function
英文:

You should make sure you're working with numeric values when trying to add the content of the textboxes:

Public Sub SumTotalAmount()
    With ThisWorkbook.Worksheets("Sheet 1")
        .TextBox5.Value = GetNumber(.TextBox1) + GetNumber(.TextBox2) + _
                          GetNumber(.TextBox3) + GetNumber(.TextBox4)
    End With
End Sub

'Return numeric value from textbox (or 0 if not numeric or blank)
Function GetNumber(objTB As Object)
    Dim v
    v = objTB.Value
    If Len(v) > 0 And IsNumeric(v) Then GetNumber = CDbl(v)
End Function

答案2

得分: 0

我已经为您翻译好了代码部分,如下:

Option Explicit

Public Function sumTextBoxes(r As Range, txtBxNameBegin As String, targetTextBoxName As String, volat As Date) As Variant
   Dim ole As OLEObject, tole As Object, ws As Worksheet, v As Variant
   Set ws = r.Worksheet
   For Each ole In ws.OLEObjects
      If ole.OLEType = xlOLEControl Then
      'remove both Ucase for case sensitive comparison
         If UCase(Left(ole.Name, Len(txtBxNameBegin))) = UCase(txtBxNameBegin) Then
            v = ole.Object.Value
            If IsNumeric(v) Then sumTextBoxes = sumTextBoxes + Val(v)
         End If
      End If
   Next
   ws.OLEObjects(targetTextBoxName).Object.Value = sumTextBoxes & ""
End Function

希望这对您有所帮助。

英文:

I have a general solution for this problem, with conditions. The names of the textboxes that we want to sum must have the same initial part of the name, e.g. TextBox1, TextBox2 ... The common part is "TextBox". The adder TextBox should have no same common part, eg "TextBxSum". In a cell of the sheet we call the Function as follows:
=sumTextBoxes(A1;"TextBox";"TextBxSum";NOW())
Its function is twofold: on the one hand it returns the sum to the cell, on the other hand it updates the target TextBox with the sum.
The parameters it accepts are: a) Any cell of the sheet, b) The common initial part of the name of the TexBoxes to be added c) The full name of the target TexBox d) the now() makes the formula respond immediately to value changes
The textboxes are ActiveX objects.

Option Explicit

Public Function sumTextBoxes(r As Range, txtBxNameBegin As String, targetTextBoxName As String, volat As Date) As Variant
   Dim ole As OLEObject, tole As Object, ws As Worksheet, v As Variant
   Set ws = r.Worksheet
   For Each ole In ws.OLEObjects
      If ole.OLEType = xlOLEControl Then
      'remove both Ucase for case sensitive comparison
         If UCase(Left(ole.Name, Len(txtBxNameBegin))) = UCase(txtBxNameBegin) Then
            v = ole.Object.Value
            If IsNumeric(v) Then sumTextBoxes = sumTextBoxes + Val(v)
         End If
      End If
   Next
   ws.OLEObjects(targetTextBoxName).Object.Value = sumTextBoxes & ""
End Function

在Excel VBA中将文本框的值设置为多个其他文本框的总和:

huangapple
  • 本文由 发表于 2023年4月10日 22:58:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定