[英]Repeat or change cell value based on other cell
我有一个宏,它根据用户对起始值的提示填充 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
我认为有几个错误。 这是你正在尝试的吗?
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
我在输入框中选择了“Apple”和“10”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.