VBA Concatenate&和

时间:2015-08-03 16:20:57

标签: excel vba excel-vba

我需要帮助编写一个可以在SHEET2上连接数字的宏(可能看起来像这样):

         A     B     C     D
 1       4     3     2     1
 2       5     6
 3       7     8     0

并在SHEET1上得到一笔钱(4321 + 56 + 780 = 5157):

         A
 1     5157     

到目前为止我还没有写过宏,所以任何帮助都将不胜感激!

6 个答案:

答案 0 :(得分:7)

你知道没有宏可以做到这一点,对吗?

=SUMPRODUCT((A1:A3&B1:B3&C1:C3&D1:D3)+0)

答案 1 :(得分:3)

使用Ctrl + Shift + Enter:

=SUM(CONCATENATE(A1:A3,B1:B3,C1:C3,D1:D3)*1)

忽略错误:

=SUM(IFERROR(CONCATENATE(B2:B4,C2:C4,D2:D4,E2:E4)*1,0))

答案 2 :(得分:3)

这应该是你要求的。在Sheet2的每一行中连接所有值(永远)的Excel VBA脚本将行总数添加到一起,并在Sheet1中显示它们。我已经使用您的数据集对其进行了测试,并且它可以正常工作。

Sub concatSum()
    Dim row As Integer
    Dim rowVal As String
    Dim col As Integer
    Dim colVal As String
    row = 1
    col = 1
    totalVal = 0
    rowVal = ""
    With Worksheets("Sheet2")
        Do While Len(.Cells(row, 1).Value) > 0
            colVal = .Cells(row, col).Value
            Do While Len(colVal) > 0
                rowVal = rowVal & colVal
                col = col + 1
                colVal = .Cells(row, col).Value
            Loop
            col = 1
            row = row + 1
            totalVal = totalVal + rowVal
            rowVal = ""
        Loop
    End With
    Worksheets("Sheet1").Cells(1, 1).Value = totalVal
End Sub

答案 3 :(得分:1)

不需要 VBA ,请使用:

=VALUE(CONCATENATE(A1,B1,C1,D1))+VALUE(CONCATENATE(A2,B2,C2,D2))+VALUE(CONCATENATE(A3,B3,C3,D3))

并将引用更改为您喜欢的任何工作表。

enter image description here

答案 4 :(得分:1)

Sub concadinate_sum()
Dim LstRow As Long
Dim LstCol As Long, c As Variant
Dim i As Long
Dim j As Long

Sheets("Sheet2").Activate

LstRow = Sheets(2).Range("A1", Range("A1").End(xlDown).Address).Count

For i = 1 To LstRow
    LstCol = Sheets(2).Range("A" & i, Range("A" & i).End(xlToRight).Address).Count
    For j = 0 To LstCol
       c = c & Sheets("Sheet2").Range("A" & i).Offset(0, j).Value
    Next j
    Sheets("Sheet1").Range("A1").Value = CInt(c) + CInt(Sheets("Sheet1").Range("A1").Value)
    c = ""
Next i
End Sub

答案 5 :(得分:0)

这是VBA解决方案

Sub AddThem()

Dim sh2 As Worksheet
Set sh2 = ThisWorkbook.Worksheets("Sheet2")

lr = sh2.Cells(sh2.Rows.Count, "A").End(xlUp).Row
tot = 0
For i = 1 To lr

tot = tot + (Cells(i, 1) & Cells(i, 2) & Cells(i, 3) & Cells(i, 4)) + 0

Next i

Range("A1") = tot

End Sub