Vba excel从给定长度之间的字符串中提取文本
Posted
技术标签:
【中文标题】Vba excel从给定长度之间的字符串中提取文本【英文标题】:Vba excel extract text from string between given length 【发布时间】:2016-02-19 19:35:47 【问题描述】:Vba excel: 从存储在具有 255 个字符的单元格(2,1)中的字符串中,我试图将前 50 个字符复制到一个单元格中,然后将 50 到 100 个字符复制到另一个单元格中,将 150 到 200 个字符复制到另一个单元格中
我有一个存储在单元格(2,1)中的字符串,包含 255 个字符,并且想要提取 50 个单词,我的意思是分成 5 个单元格,每 50 个序列字符。我使用了下面的代码,但是对于给定的限制,字符不是复制的,它继续使用早期的限制。你能帮忙吗
ThisWorkbook.Sheets("Sheet2").Cells(2, 1) = Data_comp
Comp_data1 = Left(Data_comp, 50) ' copying 50 characters
Comp_data2 = Mid(Data_comp, 50, 100) ' copying 100 characters
Comp_data3 = Mid(Data_comp, 100, 150) ' copying 150 characters
Comp_data4 = Mid(Data_comp, 150, 200)' copying 200 characters
Comp_data5 = Mid(Data_comp, 200, 250) ' copying 250 characters ThisWorkbook.Sheets("Manual_Checks").Cells(1, 2) = Comp_data1 ThisWorkbook.Sheets("Manual_Checks").Cells(1, 3) = Comp_data2 ThisWorkbook.Sheets("Manual_Checks").Cells(1, 4) = Comp_data3 ThisWorkbook.Sheets("Manual_Checks").Cells(1, 5) = Comp_data4 ThisWorkbook.Sheets("Manual_Checks").Cells(1, 6) = Comp_data5
abc1 = Len(Comp_data1) 'shows 50 characters
abc2 = Len(Comp_data2) ' shows 100 characters
abc3 = Len(Comp_data3) '150 characters
abc4 = Len(Comp_data4) '200 characters
abc5 = Len(Comp_data50 '250 characters
【问题讨论】:
Mid() 长度中的第三个条件没有结束,因此将第三个条件全部更改为 50,因此Mid(Data_comp, 200, 250)
应该是 Mid(Data_comp, 200, 50)
【参考方案1】:
让我整理并简化答案,以供有一个好主意的 Raju 开始。我将删除 Data_comp 的 Dim,因为它没有显示在问题中。
Dim arrData(4) as String, abc(4) as Integer
ThisWorkbook.Sheets("Sheet2").Cells(2, 1) = Data_comp
For intI = 0 to 4
arrData(intI) = Mid(Data_comp, iniI * 50 + 1, 50)
abc(intI) = Len(arrData(intI))
ThisWorkbook.Sheets("Manual_Checks").Cells(1, 2 + intI) = arrData(intI)
Next
For 循环提取每个 50 个字符的块,将它们分配给字符串数组,然后分配给目标单元格,然后从字符串数组中获取长度以验证长度。
intI * 50 + 1
将从 1,51,101,151,201
开始 Mid。2 + intI
将定位单元格列 2,3,4,5,6
。
【讨论】:
【参考方案2】:您几乎做到了,但只需要稍作修正
中间函数(字符串、开始、长度)
Comp_data2 = Mid(Data_comp, 50, 100)
==> Comp_data2 = Mid(Data_comp, 51, 50)
Comp_data3 = Mid(Data_comp, 100, 150)
==> Comp_data3 = Mid(Data_comp, 101, 50)
Comp_data4 = Mid(Data_comp, 150, 200)
==> Comp_data4 = Mid(Data_comp, 151, 50)
Comp_data5 = Mid(Data_comp, 200, 250)
==> Comp_data5 = Mid(Data_comp, 201, 50)
整体更新代码如下
Dim Data_comp As String
Data_comp = "Some Text"
ThisWorkbook.Sheets("Sheet2").Cells(9, 2) = Data_comp
Comp_data1 = Left(Data_comp, 50)
Comp_data2 = Mid(Data_comp, 51, 50)
Comp_data3 = Mid(Data_comp, 101, 50)
Comp_data4 = Mid(Data_comp, 151, 50)
Comp_data5 = Mid(Data_comp, 201, 50)
ThisWorkbook.Sheets("Manual_Checks").Cells(1, 2) = Comp_data1
ThisWorkbook.Sheets("Manual_Checks").Cells(1, 3) = Comp_data2
ThisWorkbook.Sheets("Manual_Checks").Cells(1, 4) = Comp_data3
ThisWorkbook.Sheets("Manual_Checks").Cells(1, 5) = Comp_data4
ThisWorkbook.Sheets("Manual_Checks").Cells(1, 6) = Comp_data5
abc1 = Len(Comp_data1)
abc2 = Len(Comp_data2)
abc3 = Len(Comp_data3)
abc4 = Len(Comp_data4)
abc5 = Len(Comp_data50
【讨论】:
以上是关于Vba excel从给定长度之间的字符串中提取文本的主要内容,如果未能解决你的问题,请参考以下文章
(VBA Excel) 从字符串中提取文本和相关字母并输出结果