![](/img/trans.png)
[英]How to drag a formula down (autofill) using VBA after finding the first blank cell
[英]How to remove characters in a cell after the first blank space using VBA?
我想使用VBA处理一些Excel工作表。 我已经设法将数据复制到适当的单元格中,但是现在我需要从一系列单元格中删除出现在第一个空格之后的一些字符。
我不知道该列中的最后一个单元号,所以我必须找出那个单元号是第一个。
这是单元格中数据的示例:
资讯科技
IB6-信息业务
我想离开 :
它
IB6
并删除其余的单元格内容。 这是我正在使用的脚本:
Dim strCoIDFull As String
Dim strCoIDShrt As String
'remove characters after the first blank space in column M 'Department Code'
LastRow = Cells(Rows.Count, 13).End(xlUp).Row
strCoIDFull = Range("M22:M" & LastRow).Value
strCoIDShrt = Left(CompanyIDFull, InStr(strCoIDFull, " ") - 1)
Range("M22:M" & LastRow).Value = strCoIDShrt
VBA代码的这一部分不起作用,关于我哪里出错了或有更好的解决方案的任何想法?
有两个问题:
(1)下一行
strCoIDShrt = Left(CompanyIDFull, InStr(strCoIDFull, " ") - 1)
可能需要更改为
strCoIDShrt = Left(strCoIDFull, InStr(strCoIDFull, " ") - 1)
(2)完成上述操作后,您可能还会在所有其他行中获得M22的截断值。 那是因为您缺少循环。 您需要遍历各行,以便截断其中的每一行和每个字符串。 有关循环的更多信息,您可能需要查看以下链接: http : //www.homeandlearn.org/excel_vba_for_loops.html
您真正需要的是:
Dim LastRow As Long
LastRow = Cells(Rows.Count, "M").End(xlUp).Row
Range("M22:M" & LastRow).Replace what:=" *", replacement:="", lookat:=xlPart
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.