在特定字符之前提取文本/数字

时间:2018-04-11 12:44:13

标签: excel vba

所以我一直在玩Excel VBA,看看我能用它做些什么。目前,我遇到了一个问题。我的代码是这样的:

Sub Validate_Input_Click()
 Dim temp As String
 For Row = 7 To 250
      If Application.WorksheetFunction.CountBlank(Range(Cells(Row, 2), Cells(Row, 12))) = 0 Then
    temp = ""
    For col = 2 To 12
      If Cells(Row, col) <> "" Then
        If temp <> "" Then temp = temp & "_"
        temp = temp & Cells(Row, col)
      End If
     Next col
    Cells(Row, 1) = temp
 End If
Next Row
End Sub

这完全符合我的要求。我现在要做的是,让我们说在B到E列的几个单元格中有一个带有短划线然后更多文本的文本,例如:

Test - Testing

我想要连接的是,在每个单独的单元格中抓住那个破折号左边的所有内容。所以它看起来像,

Running_This_Test_In_Some_Kind_Of_Format

而不是:

Running_This_Test - Testing_In_Some_Kind_Of_Format

我已经尝试创建一个整数并创建一个Left语句,但一直给我没有足够的内存错误或使用错误的参数,不确定我做错了什么。所以任何帮助都会非常感激!

5 个答案:

答案 0 :(得分:1)

您可以替换

temp = temp & Cells(Row, col)

pos = InStr(1, Cells(Row, col), "-", vbTextCompare) 'find the position of dash
If pos Then 'if dash position exists
    temp = temp & Trim(Left(Cells(Row, col), pos - 1)) 'take left part of that string and trim to get rid of spaces
Else
    temp = temp & Cells(Row, col) 'else do it as you did it before
End If

答案 1 :(得分:0)

进行了一些细微的改动......可能不是最干净的解决方案,但仍然是一个解决方案:

Sub Validate_Input_Click()
Dim temp As String, nextstring As String
Dim i As Long

For Row = 7 To 250
    If Application.WorksheetFunction.CountBlank(Range(Cells(Row, 2), Cells(Row, 12))) = 0 Then

    temp = ""

    For col = 2 To 12
      If Cells(Row, col) <> "" Then

        If InStr(Cells(Row, col), "-") > 0 Then
            For i = 1 To Len(Cells(Row, col))
                If Mid(Cells(Row, col), i, 1) = "-" Then
                    nextstring = Left(Cells(Row, col), i - 2)
                    Exit For
                End If
            Next i
        Else
            nextstring = Cells(Row, col)
        End If

        If temp <> "" Then temp = temp & "_"
            temp = temp & nextstring
        End If
    Next col

    Cells(Row, 1) = temp

    End If
Next Row

End Sub

Image1

答案 2 :(得分:0)

在搞乱代码时,我认为我找到了另一个解决我自己问题的方法。代码如下:

Sub Validate_Input_Click()
Dim temp As String
Dim s As String
For Row = 7 To 250
 If Application.WorksheetFunction.CountBlank(Range(Cells(Row, 2), Cells(Row, 12))) = 0 Then
    temp = ""
    For col = 2 To 12
      If Cells(Row, col) <> "" Then
      s = temp
        If temp <> "" Then temp = Split(s, " - ")(0) & "_"
        temp = temp & Cells(Row, col)
      End If
    Next col
    Cells(Row, 1) = temp
End If
Next Row
End Sub

这也是一个可行的解决方案吗?或者其他东西会像@dwirony上面的答案那样更好地工作吗?

答案 3 :(得分:0)

您无需再次检查空单元格,因为您已使用CountBlank检查它们。

这个怎么样?

Sub Validate_Input_Click()
 Dim temp As String, str As String
 Dim iRow As Long, Col As Long
 For iRow = 7 To 250
    If Application.WorksheetFunction.CountBlank(Range(Cells(iRow, 2), Cells(iRow, 12))) = 0 Then
    temp = ""
    For Col = 2 To 12
        str = Trim(Split(Cells(iRow, Col), "-")(0))
        If temp = "" Then
            temp = str
        Else
            temp = temp & "_" & str
        End If
     Next Col
    Cells(iRow, 1) = temp
 End If
Next iRow
End Sub

答案 4 :(得分:0)

或以下。它将使用数组,类型函数,使用范围并与vbNullString进行比较。

Option Explicit

Public Sub Concat()
    Application.ScreenUpdating = False
    Dim arr(), wb As Workbook, ws As Worksheet, i As Long, j As Long, concatString As String
    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Sheet9") 'Change as required

    With ws
        arr = Intersect(.Range("B:E"), .UsedRange)
        For i = LBound(arr, 1) To UBound(arr, 1)
             concatString = vbNullString
            For j = LBound(arr, 2) To UBound(arr, 2)
                If InStr(1, arr(i, j), "-") > 0 Then concatString = concatString & Left$(arr(i, j), InStr(1, arr(i, j), "-") - 1)
            Next j
            .Cells(i, 1) = Join$(Split(Trim$(concatString), Chr$(32)), "_")
        Next i
    End With
    Application.ScreenUpdating = True
End Sub

数据:

Data