[英]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]') 此代码迭代
a
( col
= 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.