Create Excel VBA to delete specific text from cell in one column

时间:2015-07-28 16:44:23

标签: excel excel-vba vba

I am trying to create an Excel VBA that would delete only a specific part of the cell in only one column.

In Column A, I have a directory values:

For example:

Directoryof K:\data\Admin\

What I would like to do is remove the "Directoryof" from all the cells in column A and leave only the remaining text that follows it.

3 个答案:

答案 0 :(得分:1)

You could use something like text to columns, fixed width, and split the columns after Directoryof and then copy/paste the values back into column A.

I'm not sure if there's a method to do this without a helper column without VBA. If you can afford to use a second column, you can also use =LEFT(Cell, # of characters) assuming that the part you want to strip off is always "Directoryof" and then copy/paste values back into column A.

答案 1 :(得分:1)

You could probably use regex to accomplish what you are going for. Regular Expressions are often used for finding patterns. If all of your follows the same format, you could break your strings apart into two capture groups with something like:

(.+)([A-Z]:\\.+)

https://regex101.com/r/uD4uJ0/2 <-- this will show you your capture groups

Edit: I updated this link, sorry, originally had the wrong one.

This here How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops will show you how to split up capture groups if you are interested.

答案 2 :(得分:1)

要创建宏来执行上述操作,请按照以下步骤操作:

  1. 点击&#34;开发人员&#34;顶部菜单上的标签。
  2. 您会找到一个选项&#34;记录宏&#34;。
  3. 单击录制宏 - &gt;
    一个。出现一个对话框,为您的宏命名 湾快捷键(如果需要)可以通过按下(shift和任何键,如
      信) C。将宏存储在:此工作簿(这允许您的宏在此工作表上运行)。

  4. 点击&#34;使用相对参考&#34;。

  5. 完成后,只需在其中一个列上执行删除操作(通过删除不需要的部分),以便宏可以记录您正在执行的进程。

  6. 完成后,在最下面的窗格中,您会看到“停止宏”选项(一个小的蓝色方框)。单击它以停止录制宏。

  7. 现在您已准备好使用宏来复制它,而无需执行操作。

  8. 只需转到您要执行操作的任何其他列,然后点击&#34; Macro&#34;在开发人员选项卡上选项,然后点击您创建的marco,您将看到魔术发生。