Excel 提取特定字符前的文本/数字

Excel 提取特定字符前的文本/数字,excel,vba,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

所以我一直在玩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
我想在连接的同时,抓住每个单元格中虚线左侧的所有内容。所以它看起来像

Running_This_Test_In_Some_Kind_Of_Format
而不是:

Running_This_Test - Testing_In_Some_Kind_Of_Format

我曾尝试创建一个整数和一个Left语句,但始终没有给我足够的内存错误或使用了错误的参数,不确定我做得不对。因此,任何帮助都将不胜感激

做了一些轻微的改动。。。可能不是最干净的解决方案,但仍然是一种解决方案:

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
子验证\u输入\u单击()
Dim temp作为字符串,nextstring作为字符串
我想我会坚持多久
对于行=7到250
如果Application.WorksheetFunction.CountBlank(范围(单元格(第2行)、单元格(第12行))=0,则
temp=“”
对于col=2到12
如果单元格(行、列)“,则
如果InStr(单元格(行、列),“-”>0,则
对于i=1到Len(单元格(行、列))
如果Mid(单元格(行,列),i,1)=“-”,则
nextstring=左(单元格(行、列),i-2)
退出
如果结束
接下来我
其他的
nextstring=单元格(行、列)
如果结束
如果是temp“”,则temp=temp&“
温度=温度和下一个字符串
如果结束
下一列
单元格(第1行)=温度
如果结束
下一排
端接头

您可以更换

temp = temp & Cells(Row, col)


在乱搞代码的过程中,我想我找到了解决我自己问题的另一种方法。代码如下所示:

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
子验证\u输入\u单击()
作为字符串的Dim temp
像线一样变暗
对于行=7到250
如果Application.WorksheetFunction.CountBlank(范围(单元格(第2行)、单元格(第12行))=0,则
temp=“”
对于col=2到12
如果单元格(行、列)“,则
s=温度
如果温度为“”,则温度=拆分、“-”(0)和
温度=温度和单元格(行、列)
如果结束
下一列
单元格(第1行)=温度
如果结束
下一排
端接头

这也是一个可行的解决方案吗?或者像上面@dwirony的回答那样,其他方法是否更有效?

无需再次检查空单元格,因为您已经用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

或以下。与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
数据:


您能提供一些示例数据和预期输出吗?此外,我看不出如何从“测试-测试”中获得“以某种形式运行此测试”……例如,如果您只在给定行内跨列连接,这将是很有帮助的……是的,这是解决您问题的另一个解决方案。但是,您需要再次使用Trim来删除空格:
Trim(Split(s,“-”)(0))
Ah,因此即使在拆分公式中破折号前后都有空格,它也无法识别是否要删除空格?如果字符串始终且仅包含“-”,则无需进行修剪。如果有更多空间,则需要修剪。如果您100%确定只使用“-”,请忽略我的第一条评论。
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
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