Excel的VisualBasic脚本(String concat)

时间:2018-02-16 10:18:37

标签: excel-vba vba excel

我正在努力帮助我的朋友做相当长的工作 - 他必须在电子表格中连接很多字符串(我们正在谈论我听到的数百个字符串)。为了拯救他,我决定为他编写一个VB脚本(尽管几年内没有用VB编写,而且几乎不知道它应该如何工作)。经过一些调试后,我们来到了这段代码:

Sub Concatenate

Dim LCounter As Long
Dim FinalValue As String

FinalValue = ""

For LCounter = 1 To Rows.Count
   Set NumberCell = Cells(LCounter, 1)
   Set StringCell = Cells(LCounter, 2)
   FinalValue = FinalValue & StringCell.Value
   If IsEmpty(NumberCell) Then
      Cells(LCounter, 3).Value = FinalValue
      FinalValue = ""
   End If
Next LCounter

End Sub

最后,代码不会编译 - 我们得到424错误(需要对象)。我试图将NumberCell更改为NumberCell.Value,但是徒劳无功。知道我做错了什么吗?

[对于那些想知道它应该如何工作的人 - B栏中有很多字符串。在A列中我们有一些数字 - 如果存在一个数字,我们应该将最后一个数字后面的所有字符串连接到当前数字,并在C列中写入结果,出现在同一行数上。]

3 个答案:

答案 0 :(得分:0)

你应该声明所有变量,然后你就不会有问题......总是使用Option Explicit

Option Explicit

Sub Concatenate
'decare all variables you are using
Dim NumberCell As Range, StringCell As Range, LCounter As Long

For LCounter = 1 To Rows.Count
    Set NumberCell = Cells(LCounter, 1)
    Set StringCell = Cells(LCounter, 2)

    If IsEmpty(NumberCell) Then
        Cells(LCounter, 3).Value = NumberCell.Value & StringCell.Value
        'I don't know what you want exactly, so here you have more options
        'Cells(LCounter, 3).Value = StringCell.Value & NumberCell.Value
        'this would be equivalent to your code, but makes no sense to me
        'Cells(LCounter, 3).Value = StringCell.Value
    End If
Next LCounter

End Sub

答案 1 :(得分:0)

我相信这会做你所期望的,它将循环通过B列,并连接所有单元格,直到它在A列上找到某些东西,在这种情况下,它会将连接的字符串写入字符串中的C列/编号在A栏中找到。

声明您正在使用的工作表也是一种好习惯,因此VBA不会采用ActiveSheet:

Sub Concatenate()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
'get the last row with data on Column B
Dim LCounter As Long
Dim FinalValue As String

FinalValue = ""
For LCounter = 1 To LastRow
   NumberCell = ws.Cells(LCounter, 1)
   StringCell = ws.Cells(LCounter, 2)
   FinalValue = FinalValue & StringCell
   If Not IsEmpty(NumberCell) Then
      Cells(LCounter, 3).Value = FinalValue
      FinalValue = ""
   End If
Next LCounter
End Sub

答案 2 :(得分:0)

在方括号中使用逻辑:

Option Explicit
Sub Concatenate()
    Dim MySheet As Worksheet, MyArea As Range
    Dim iCell As Range, lRow As Long, FinalValue As String
    'Change "MySheet" accordingly
    Set MySheet = ThisWorkbook.Worksheets("MySheet")
    'Calculate last row of column B
    lRow = MySheet.Range("B" & MySheet.Rows.Count).End(xlUp).Row
    'Loop through each area of blank cells in column A
    For Each MyArea In MySheet.Range("A1:A" & lRow).SpecialCells(xlCellTypeBlanks).Areas
        'Loop through each cell in this area
        For Each iCell In MyArea.Offset(-1, 0).Resize(MyArea.Rows.Count + 1, 1).Offset(0, 1).Cells
            'Add Cell value to FinalValue
            FinalValue = FinalValue & iCell.Value
        Next
        'Put string into according cell
        MyArea.Cells(1, 1).Offset(-1, 2) = FinalValue
        'Clear FinalValue
        FinalValue = ""
    Next
    MsgBox "Done!"
End Sub

以下是样本:
enter image description here