Vba 在Excel中取出字符并放入新列
嗨,我对vba有点陌生,所以我会尽量解释我的问题。 我在Excel中的Vba 在Excel中取出字符并放入新列,vba,excel,excel-2010,Vba,Excel,Excel 2010,嗨,我对vba有点陌生,所以我会尽量解释我的问题。 我在Excel中的a列中有一个数据集,我有很多文件名如下: 1. AB000**1234**45.tif 2. AB000**1235**45.tif 3. AB000**1236**45.tif 4. AB000**1237**45.tif 1. 1234 2. 1235 3. 1236 4. 1237 等等 从这里,我想去掉所有强字符,并将其放入列C,因此它将如下所示: 1. AB000**1234**45.tif
a列中有一个数据集,我有很多文件名如下:
1. AB000**1234**45.tif
2. AB000**1235**45.tif
3. AB000**1236**45.tif
4. AB000**1237**45.tif
1. 1234
2. 1235
3. 1236
4. 1237
等等
从这里,我想去掉所有强字符,并将其放入列C
,因此它将如下所示:
1. AB000**1234**45.tif
2. AB000**1235**45.tif
3. AB000**1236**45.tif
4. AB000**1237**45.tif
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
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
我试着数到左(5)和右(6),但不知道如何去掉我想要的4个字符。
希望你能帮我 看看Mid()
函数
在您的情况下的用法:
Mid(cell.Value, 6, 4) 'First parameter is the string, 6 is the start character, 4 is length
查看Mid()
函数
在您的情况下的用法:
Mid(cell.Value, 6, 4) 'First parameter is the string, 6 is the start character, 4 is length
没有循环的最简单方法如下:
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
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
没有循环的最简单方法如下:
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
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
如果要从字符串中删除强字符。在下面试试。它将获取单元格中所有粗体字符,并将其放置在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
如果要从字符串中删除强字符。在下面试试。它将获取单元格中所有粗体字符,并将其放置在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(cell.Value,6,4)
??RNG=MID(cell,instrev(cell,“1”),4一样,这会将单元格中第一个实例中的值1减少四个字符。@JessieQuick,你检查了我下面的答案了吗。希望您正在寻找该方法?如MID(cell.Value,6,4)
??RNG=MID(cell,instrev(cell,“1”),4),这将在第一个实例中,将单元格中的值减少四个字符。@JessieQuick,您检查了我下面的答案了吗。希望您正在寻找这种方法?谢谢您的回复。乐于助人!谢谢你的回复。乐于助人!这也是一个很好的一个!但是我真的不知道**Evaluate中发生了什么(“如果(“&rng.Address&“=”,“,”,**此部分如果列A中的单元格为空,则此部分只返回一个空单元格…通常,此部分返回一个数组,就像工作表中的数组函数一样。这样,您就不需要循环了。)这也是一个很好的例子!但是我真的不知道**Evaluate(“IF(“&rng.Address&“=”,“,”,**此部分如果列a中的单元格为空,则此部分只返回一个空单元格…通常,此部分返回的数组与工作表中的数组函数类似。这样您就不需要循环了。)谢谢大家的重播和帮助!我从中学到了很多:)谢谢大家的重播和帮助!我从中学到了很多:)