取出字符并在Excel中放入一个新列

时间:2016-07-18 12:55:07

标签: excel vba excel-vba excel-2010

嗨,我对vba有点新意,所以我会尽量解释我的问题。 我在列A中的Excel中有一个数据集,我有很多这样的文件名:

 1. AB000**1234**45.tif 
 2. AB000**1235**45.tif
 3. AB000**1236**45.tif
 4. AB000**1237**45.tif

等。

由此我想要取出所有强大的字符并放入C列,所以它看起来像这样:

 1. 1234
 2. 1235
 3. 1236
 4. 1237

等..

目前我的代码看起来像这样:

Sub TakeOut
    Dim str1 As String
    Dim LR As Long
    Dim cell As Range, RNG As Range

    LR = Range("A" & Rows.Count).End(xlUp).Row   
    Set RNG = Range("A1:A" & LR)

    For Each cell In RNG
        L = Len(RNG) 
        If L > 0 Then
           RNG = ...
        End If
    Next cell

    Range("C:C").Columns.AutoFit

End Sub 

我试图计算左(5)和右(6),但不知道如何取出我想要的4个字符。 希望你能帮助我。

3 个答案:

答案 0 :(得分:2)

查看Mid()函数link

用于您的情况:

Mid(cell.Value, 6, 4) 'First parameter is the string, 6 is the start character, 4 is length

答案 1 :(得分:2)

没有循环的最简单方法是这样的:

Sub TakeOut()
  Dim rng As Range
  Set rng = Range("A1", Range("A" & Rows.Count).End(xlUp))
  rng.Offset(, 1) = Evaluate("IF(" & rng.Address & "="""","""",MID(" & rng.Address & ",6,4))")
End Sub

答案 2 :(得分:2)

如果你想从字符串中取出强字符。试试下面。它会将单元格中的所有粗体字符放在C列中。

希望你在寻找这个?

Sub get_bold_content()
    Dim lastrow, i, j, totlength As Long
    lastrow = Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To lastrow
        totlength = Len(Range("A" & i).Value)
        For j = 1 To totlength
            If Range("A" & i).Characters(j, 1).Font.Bold = True Then
                outtext = outtext & Range("A" & i).Characters(j, 1).Text
            End If
        Next j
        Range("C" & i).Value = outtext
        outtext = ""
    Next i
End Sub

enter image description here

相关问题