重复或更改基于其他单元格的单元格值

我有一个macros,根据用户对起始值的提示填充G列中的值(从单元格G2开始),然后通过每次将值加10来重复到列的末尾。

 Sub Macro1() Dim TagName As String Dim TagNum As Long, k As Long ' Prompts user for tag names/numbers TagName = InputBox("What is the product tag name? Ex. APPLE") TagNum = InputBox("What is the starting tag #? Ex. 10") ' Set values in column G, up to last row in column J k = 0 With ActiveSheet LastRow = .Cells(.Rows.Count, "J").End(xlUp).Row End With With ActiveSheet.Range("G2") For i = 1 To LastRow Step 1 .Item(i + 0) = TagName & "_" & TagNum2 + k k = k + 10 Next End With End Sub 

如果C列在每一行中都有唯一的值,就可以很好地工作,如下所示: 之前

但是,如果C列中的值重复,那么我希望G列中的值相同,如下所示: 在这里输入图像说明

我认为有几个错误。 这是你正在尝试?

 Sub Sample() Dim TagName As String, sTag As String Dim TagNum As Long, k As Long ' Prompts user for tag names/numbers TagName = InputBox("What is the product tag name? Ex. APPLE") TagNum = InputBox("What is the starting tag #? Ex. 10") With ActiveSheet LastRow = .Cells(.Rows.Count, "J").End(xlUp).Row k = TagNum .Range("G2").Value = TagName & "_" & k For i = 3 To LastRow '~~> Increment k only if the values do not match If .Range("C" & i).Value <> .Range("C" & i - 1).Value _ Then k = k + 10 .Range("G" & i).Value = TagName & "_" & k Next End With End Sub 

我在input框中select了“Apple”和“10”

在这里输入图像说明