如何在单元格中的第一个空格之后删除所有字符? [英] How do I delete all characters after the first space in a cell?

查看:121
本文介绍了如何在单元格中的第一个空格之后删除所有字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个城市名称列表,后面跟着他们所在的状态全部位于Excel中的一列。如何删除第一个空格之后的所有内容,以便城市名称是单元格中唯一的一个?

I have a list of city names followed by the state in which they are located all in one column in Excel. How can I delete everything after the first space so that the city name is the only thing that's left in the cell?

示例:A1 =johnson TX

应该只是A1 =johnson

example: A1 = "johnson TX"
should be just A1= "johnson"

推荐答案

我假设你想要一个VBA解决方案,一个href =/ questions / tagged / excel-vbaclass =post-tagtitle =show questions tagged'excel-vba' =tag> excel-vba 。

I assume you want a VBA solution since you tagged your question excel-vba.

这样做:

Sub KeepCity()
    Dim strCityAndState As String
    Dim strCityOnly As String
    strCityAndState = Range("A1").Value
    strCityOnly = Left(strCityAndState, InStr(strCityAndState, " ") - 1)
    Range("A2").Value = strCityOnly
End Sub

如果不想要VBA,而想要一个单元格公式,那么@ JToland的答案可以正常工作,虽然这一个更简洁,并没有保留尾随空格字符:

If you don't want VBA and want a cell formula instead, then @JToland's answer works fine, though this one is more concise and doesn't keep the trailing space character:

=LEFT(A1, FIND(" ",A1)-1)

这篇关于如何在单元格中的第一个空格之后删除所有字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆