首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >宏来编辑字符串并将它们写入其他工作表。

宏来编辑字符串并将它们写入其他工作表。
EN

Stack Overflow用户
提问于 2018-07-03 18:40:10
回答 2查看 27关注 0票数 0

我是个编程新手,我很难想办法完成我的任务。

我有一个模板表,它的组织方式是在它被填充之后,它将被转换为".csv“,然后导入到一个程序中去构建一个数据库。

我还有一张纸,上面有我需要的一些数据,还有一些我不需要的数据。我想要阅读的第一列是遵循上述模型的“标记名称”:

标签名/ LOW / HIGH // ELU

80-坑-0001/0/1/C

32-XI-004B /1//0/C

45-CIT-4001 //0/1/D

我需要所有这些标签和:

( a)从标签名称中删除"-“( b)在模板表( c)中没有"-”的情况下写标签名时,它必须在标记名的末尾添加"_PV“和"_PB”。每个人都需要在一条不同的线上。d)所有属性都需要复制

就像这样:

80PIT0001_PV /0//1//C

80PIT0001_PB /0//1//C

32XI004B_PV /1/0//C

32XI004B_PB /1/0//C

45CIT4001_PV /0/1//D

45CIT4001_PB /0/1//D

如果有人能告诉我如何正确地操作和编辑这些字符串,以及如何将这些字符串写入一个新的工作表,在我的例子中,模板表。

我真的很感谢任何想帮我的人。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-03 20:40:57

未经测试,在手机上写的,但可能是这样的。我假设您的数据(不包括列标题/名称)开始于名为“Source”的工作表上的单元格A2 --而且数据是A列是连续的。

在运行代码之前保存工作簿的副本,因为代码将试图覆盖工作表“模板”上的数据(假设它存在)。

代码语言:javascript
复制
Option Explicit

Sub FillTemplateSheet()

With Thisworkbook

' Change this to whatever sheet your data is on and change cell reference to the first tag name (excluding header) '
With .worksheets("Source").range("A2")

Dim lastRow as long
lastRow = .end(xldown).row ' This is not the best way to find the last row, but might work in your use case '

Dim arrayOfValues() as variant
arrayOfValues = .resize(lastRow - .row+1, 4).value2 ' Assumes we want the first four columns '

End with

Dim rowIndex as long
Dim columnIndex as long
Dim alteredTagName as string

' Appending PV and PB to each tag name means we basically need twice as many body rows in the output.'

Dim outputArray() as variant
Redim outputArray(lbound(arrayOfValues,1) to (ubound(arrayOfValues,1)*2), lbound(arrayOfValues,2) to ubound(arrayOfValues,2))

For rowIndex = lbound(arrayOfValues,1) to ubound(arrayOfValues,1)

For columnIndex = lbound(arrayOfValues,2) to ubound(arrayOfValues,2)

If columnIndex <> 1 then
' d) copy all other attributes to outputArray'

outputArray(rowIndex, columnIndex) = arrayOfValues(rowIndex,columnIndex)

outputArray(rowIndex+1, columnIndex) = arrayOfValues(rowIndex,columnIndex)

Else

alteredTagName = arrayOfValues(rowIndex,columnIndex) 

'a) get rid of all - '
alteredTagName = vba.strings.join(VBA.strings.split(alteredTagName,"-",-1,vbbinarycompare),vbnullstring)

'b) write to output array '
'c) append PV and PB '

outputArray(rowIndex, columnIndex) = alteredTagname & "_PV"

outputArray (rowIndex +1, columnIndex) = alteredTagName & "_PB"

End if

Next columnIndex

Next rowIndex

End with

' Assumes headers are on row 1 of sheet Template, and therefore first tag name will be in cell A2'
.worksheets("Template").range("A2").resize(ubound(outputArray,1),ubound(outputArray,2)).value2 = outputArray

End with

End sub
票数 0
EN

Stack Overflow用户

发布于 2018-07-03 18:47:45

好吧,你不需要编程来达到这个目的。使用Excel和Google中的公式,这是可以实现的。替代公式将删除'-‘和级联公式与组合,如果将添加'_PV’。在这些其他公式之上的数组公式将在整个列中完成此计算,而不必每次拖放公式。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51161092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档