[英]Excel VBA — Find first occurrence of any value in column A, then insert value of column B into column C (same row)
I'm trying to write a VBA script for a weekly chore. 我正在尝试为每周的琐事编写VBA脚本。 Here is my data: 这是我的数据:
I run a macro that clears out the values of column C. Then i have to find the first occurrence of every unique value in Column A. When I find the first occurrence, I write in the same row of Column C "=Column B" just for that row. 我运行一个宏,清除了C列的值。然后,我必须在A列中找到每个唯一值的第一个匹配项。当我找到第一个匹配项时,我在C列的同一行中写“ = Column B”仅用于那一行。 Basically, I'm trying to say, "A4 is the first occurrence of a unique value; therefore C4=B4." 基本上,我想说的是:“ A4是唯一值的首次出现;因此C4 = B4。” I can't do a simple IF() function and drag it down, because the rest of column C needs to stay truly blank (it's referenced by an IF(NOT(ISBLANK)) function). 我无法执行简单的IF()函数并将其向下拖动,因为C列的其余部分需要保持真正空白(由IF(NOT(ISBLANK))函数引用)。
I'm new to VBA, and unfortunately recording what I did, did not capture my actual intent. 我是VBA的新手,但不幸的是,我记录了所做的事情,但并没有抓住我的实际意图。 This was the macro, which doesn't seem efficient: 这是宏,似乎效率不高:
Sub Macro1()
'
' Macro1 Macro
'
Range("C8").Select
Application.Left = 3591.25
Application.Top = 268
Windows("Copy of OPS_schedule_6.xlsm").Activate
Application.Left = 3451.75
Application.Top = 163
Windows("Book1").Activate
Range("A1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$B$9").AutoFilter Field:=1, Criteria1:="Alpha"
Range("C1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-1]"
Range("C2").Select
ActiveSheet.Range("$A$1:$B$9").AutoFilter Field:=1, Criteria1:="Bravo"
Range("C4").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-1]"
Range("C5").Select
ActiveSheet.Range("$A$1:$B$9").AutoFilter Field:=1, Criteria1:="Charlie"
Range("C8").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-1]"
Range("C9").Select
End Sub
If you used a formula such as 如果您使用的公式是
=IF(COUNTIF(A$1:A1,A1)=1,B1,"del")
You could clear the del
s with Find & Select or, since you have tagged this VBA, with: 您可以使用“查找并选择”或由于已标记了此VBA而使用以下命令清除del
:
Selection.SpecialCells(xlCellTypeFormulas, 2).ClearContents
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.