简体   繁体   English

Excel VBA —查找列A中任何值的第一个出现位置,然后将列B的值插入列C(同一行)

[英]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: 这是我的数据:

IMG1

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.

相关问题 VBA:根据列中第一次出现的值删除 excel 行 - VBA: Deleting an excel row based on first occurrence of a value in a column Excel / VBA-在列中查找特定值的拳头和最后出现 - excel/vba - find fist and last occurrence of a particular value in a column VBA - 如果 A 列中的单元格 = 值,则将同一行的 B、C、D 列复制到新工作表 - VBA - IF a cell in column A = Value, then copy column B, C, D of same row to new worksheet Excel VBA如果第一列中的值相同,则串联另一列中的值 - Excel VBA Concatenate values in another column if the value in first column is the same 如果列A单元格与列B单元格匹配,则显示与列B相同的行的C列值 - if Column A cell matches Column B cell, display column C value of same row as column B 一个程序,它将在最后一行中找到具有最小值的列,并将其在第一行中的值分配给变量? [Excel VBA] - A program that will find the column with a minimum value in its last row and assign its value in the first row to a variable? [Excel VBA] 仅当同一行的 B 列是同类中的第一个且 A 列小于指定值时,才计算 A 列的公式或方法(在 excel 中)? - Formula or way (in excel) to count column A only if column B of the same row is the first of it's kind and column A is less then a specified value? Excel VBA:在工作表的“偏移量”列中查找一个值以查找相同的值 - Excel VBA: Find a value in Worksheet, offset column to find same value 如何使用Excel / VBA查找每个唯一column_a元素的最大column_c的column_b值 - How to use Excel/VBA to find column_b value with max column_c for each unique column_a element VBA在A列中找到x值,在B列中找到y值,在C列中找到和值 - VBA find value x in column A and value y in column B and sum values in column C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM