使用数组函数根据其当前值来更改单元格值?

几个星期前,我问了一个( 类似的问题) ,我试图用我的方式导航到我的新问题的答案,但我一直无法。 基本上我想要做的是这样的:我从一个工作簿中复制数据并粘贴到模板工作簿的A列中。 之后,我想要粘贴的时候,根据单元格内容来更改刚粘贴的单元格的内容。 例如,一个单元格将被粘贴,它会说“ALBY Total”,我想自动(使用VBA)改变单元格来说“ALBY”。 我试图自己编码,但不能得到它的工作。 我没有得到一个错误,但没有任何反应。 下面是我的代码示例(有大量的数组,所以我不会给你所有的):

Sub TSA_Template_Creation_Macro() Workbooks("TSA Template.xlsm").Activate Set wb = ThisWorkbook Set ws = wb.Sheets(1) alby = Array("ALBY Total") anch = Array("ANCH Total") atlc = Array("ATLC Total") lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row For x = 1 To lastRow If stringIsInArray(ws.Cells(x, 1), alby) Then ws.Cells(x, 1) = "ALBY" ElseIf stringIsInArray(ws.Cells(x, 1), anch) Then ws.Cells(x, 1) = "ANCH (+office)" ElseIf stringIsInArray(ws.Cells(x, 1), atlc) Then ws.Cells(x, 1) = "ATLC" End If Next x End Sub Function stringIsInArray(stringToBeFound As String, arr As Variant) As Boolean stringIsInArray = (UBound(Filter(arr, stringToBeFound)) > 0) End Function 

我对VBA的世界相当陌生,所以很可能是一个简单的修复。 我已经摆弄它,但我不知道如何使其工作。

这听起来像你手动粘贴值。 如果是这种情况,从包含值的模板工作簿运行时,以下内容应该可以工作:

Sub TSA_Template_Creation_Macro()

lastRow = Cells(Rows.Count,1).End(xlUp).Row

对于x = 1到lastRow

 If InStr(1, Cells(x, 1), "ALBY") Then Cells(x, 1) = "ALBY" ElseIf InStr(1, Cells(x, 1), "ANCH") Then Cells(x, 1) = "ANCH (+office)" ElseIf InStr(1, Cells(x, 1), "ATLC") Then Cells(x, 1) = "ATLC" 

万一

下一个x

结束小组