简体   繁体   English

如何在有符号的Excel 2007 VBA中修剪单元格中的数据

[英]how to trim data in a cell in Excel 2007 VBA where there is a symbol

I want to trim the data inside the cell so that i have eveything before the hyphen 我想整理单元格中的数据,以便在连字符之前进行整理

For example, if a cell contains: 例如,如果一个单元格包含:

'Net Operating Loss - All Pre-1990 Carryforwards'

i want to trim it in such a way that i only have: 我想以一种只有我的方式修剪它:

'Net Operating Loss'

Any suggestions on how to do this 有关如何执行此操作的任何建议

Like this? 像这样?

Option Explicit

Sub Sample()
    Dim strSample As String

    strSample = "Net Operating Loss - All Pre-1990 Carryforwards"

    Debug.Print Split(strSample, "-")(0)
End Sub

BTW Do you need to do this in VBA. 顺便说一句,您是否需要在VBA中执行此操作。 The above can be achieved using Excel Formulas as well. 以上也可以使用Excel公式来实现。

Let's say you have " Net Operating Loss - All Pre-1990 Carryforwards " in Cell A1 then put this in Cell B1 假设您在单元格A1中有“ 净运营亏损-所有1990年以前的结转额 ”,然后将其放入单元格B1中

EXCEL 2003 2003年度

=IF(ISERROR(MID(A1,1,FIND("-",A1,1)-1)),"",MID(A1,1,FIND("-",A1,1)-1))

EXCEL 2007 Onwards EXCEL 2007起

=IFERROR(MID(A1,1,FIND("-",A1,1)-1),"")

Hy, HY,

you use InStr to search for the hyphen and use the resulting position as length for the Right Command. 您可以使用InStr搜索连字符,并将结果位置用作Right Command的长度。

dim p as integer
p = InStr(YourStr, '-');
Trim(Right(YourStr, p - 1))

A little reference is here http://www.techonthenet.com/access/functions/index.php 这里有一些参考资料http://www.techonthenet.com/access/functions/index.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM