取出字符并在Excel中添加一个新列

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

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

等等..

从这个angular度我想把所有强壮的angular色都拿出来放在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个字符。 希望你能帮助我。

如果你想从string中取出强壮的字符。 试试下面。 它将采取一个单元格中的所有粗体字符,并将其放置在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 

在这里输入图像描述

看看Mid()函数链接 。

在你的情况下使用:

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

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

 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