简体   繁体   English

VBA:如何从一个单元格复制一部分字符串,并用它替换另一单元格的一部分?

[英]VBA: How to copy part of the string from one cell and replace part of another cell with it?

The text in excel I have looks like this: 我在excel中的文本如下所示:

[tab]SECTION 01. [tab]第01节。
*Name of section 1 *第1节的名称
TEXT 文本
TEXT 文本
TEXT 文本
[tab]SECTION 2. [tab]第2节。
*Name of section 2 *第2节的名称
TEXT 文本
... ...

I want to find next SECTION in column (1, 2, 3...), copy only string SECTION 01. (02., 03.,...,52.,...)and paste it in the cell below replacing only *. 我想在列(1、2、3 ...)中找到下一个SECTION,仅复制字符串SECTION 01.(02.,03。,...,52。,...)并将其粘贴到下面的单元格中仅替换*。

The end result in excel should look like this: excel中的最终结果应如下所示:

SECTION 01. Name of section 1 第01节。第1节的名称
TEXT 文本
TEXT 文本
TEXT 文本
SECTION 02. Name of section 2 第02节。第2节的名称
TEXT 文本
... ...

I tried to record macro, but it only copies the same text to only one cell specified, so I will have to rearrange VB code. 我试图记录宏,但是它只将相同的文本复制到指定的一个单元格中,因此我将不得不重新排列VB代码。 Can anyone help? 有人可以帮忙吗?

Thank you, 谢谢,

This code iterates column a ( col =1) and when it finds a '[tab]' it sets the sctn variable to the value of the cell (ignoring the '[tab]') 此代码迭代acol = 1) col并在找到“ [tab]”时将sctn变量设置为单元格的值(忽略“ [tab]”)

When it finds a '*', it replaces it with the content of the sctn variable 找到“ *”时,将其替换为sctn变量的内容

Dim row as Integer,col as Integer,sctn as String
col=1
row=1
While Cells(row,col)<>""
  If Instr(Cells(row,col),"[tab]")>0 Then
    sctn=Replace(Cells(row,col),"[tab]","")
    Cells(row,col)=sctn
  Else
    Cells(row,col)=Replace(Cells(row,col),"*",sctn)
  End If
  row=row+1
Wend

暂无
暂无

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

相关问题 如何将一个单元格中的一部分字符串替换为另一单元格中的字符串? - How do I replace part of a string from one cell from a string in another cell? 使用VBA用另一个单元格中的文本替换单元格中的部分文本 - Replace part of text in a cell with text from another cell using VBA 如何使用 Excel 宏将部分字符串从特定单元格复制到另一个单元格? - How to copy part of a string from a specific cell to another cell with Excel macros? 根据另一个单元格中的文本替换字符串的一部分 - Replace part of a string based on text in another cell 使用VBA宏在一个单元格中查找并替换其下面的单元格值替换字符串的一部分 - Find and replace part of string in one cell with value of cell below it with VBA Macro 如何使用 VBA 仅格式化单元格中字符串的一部分 - How to only format a part of a string in a cell with VBA 如何在Excel VBA中将单元格从一行复制到另一行 - How to copy cell from one row to another in excel vba Excel VBA如何将单元格内容从一本书复制到另一本书 - Excel VBA how to copy cell content from one book to another 如何根据单元格选择复制行的固定部分,Excel vba? - How to copy fixed part of rows based on cell selection , Excel vba? 从另一张纸上的单元格值复制同一张纸中范围的一部分的粘贴范围 - Copy Range From One Sheet Paste Part of Range In Same Sheet Based On Cell Value On Another Sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM