在Microsoft Excel 2013中删除/筛选单元格内容

时间:2015-02-04 15:30:32

标签: excel vba excel-vba excel-formula excel-2010

我正在寻找清理电子表格中某个特定单元格的单元格内容的最佳方法。现在存在,D列在同一单元格中列出了城市,州和高中(屏幕截图#1)。我需要将这些值拆分为3个唯一的列/单元格,如屏幕截图#2所示。我该如何做到这一点?

**注意 - 下面的示例只是一个样本大小。我有数千个单元格来执行此操作。

现有单元格格式:

enter image description here

新格式(希望):

enter image description here

4 个答案:

答案 0 :(得分:3)

使用文本到列"分隔"在逗号上拆分,然后再次使用它"固定宽度"将状态从学校名称中分离出来(假设状态总是2个字符)。如果需要,使用TRIM清理空间。

答案 1 :(得分:0)

这样的事情应该查看D行中的所有单元格,并在接下来的3列中返回您要查找的值;可能需要一点调整

For each cell in Range("D2:D & Range("D2".End(xlDown).Row)) cell.offset(columnOffset:=1) = left(cell,instr(String1:=cell, String2:= ",")) cell.offset(columnOffset:=2) = Mid(cell,instr(String1:=cell, String2:= ",")+1,2) cell.offset(columnOffset:=3) = right(cell,len(cell)-instr(String1:=cell, String2:= ",")+3) Next

答案 2 :(得分:0)

我试过Jake Duddy的代码,它包含了一些错误,我不知道它是否有效。同时你可以尝试下面的代码。

Dim LastRow As Long
Dim State_L As String
Dim City_L As String
Dim HS_L As String

LastRow = Range("D" & Rows.Count).End(xlUp).Row


For i = 2 To LastRow

    City_L = Left(Range("D" & i), InStr(1, Range("D" & i), ",") - 1) ' Getting up to comma
    State_L = Mid(Range("D" & i), InStr(1, Range("D" & i), ",") + 1, 2) 'Getting after comma for 2 characters
    HS_L = Mid(Range("D" & i), InStr(1, Range("D" & i), ",") + 3, Len(Range("D" & i)) - InStr(1, Range("D" & i), ",")) 'Getting after comma+2 characters upto the end

    Range("E" & i).Value = City_L
    Range("F" & i).Value = State_L
    Range("G" & i).Value = HS_L

Next

答案 3 :(得分:0)

选择您要处理的单元格并运行此小宏:

Sub ParseData()
    Dim r As Range, L As Long, L1 As Long, L2 As Long

    For Each r In Selection
        t = r.Text
        L = Len(t)
        L1 = InStr(1, t, ",")
        For i = 1 To L
            If r.Characters(i, 1).Font.Italic = True Then
                L2 = i
                Exit For
            End If
        Next i

        r.Offset(0, 1).Value = Mid(t, 1, L1 - 1)
        r.Offset(0, 2).Value = Mid(t, L1 + 1, L2 - L1 - 1)
        r.Offset(0, 3) = Mid(t, L2)
        r.Offset(0, 3).Font.Italic = True
    Next r
End Sub

例如:

enter image description here