excel VBA:如果满足条件,则标记、、x、、

jjhzyzn0  于 2022-12-30  发布在  其他
关注(0)|答案(2)|浏览(197)

我对VBA不是很有经验,这也是我第一次写,但我还是尝试了一下。我想我从我尝试做的事情开始,在开始的时候,我为,,x,,标记设置了一些条件。我写了一个代码,当它从列F中找到任何值时,将,,x,,写入列AN。
之后,我希望,,,x,,在每个匹配中只出现一次(这样就不会有来自F列的重复)。
那起作用了,但那不是目标,那是我唯一能达到的目标。

代码完成后,我会解释我必须实现的目标
目前代码:

Sub mark_x()

Dim lastRow As Long
Dim values As New Collection

lastRow = ActiveSheet.Cells(Rows.Count, "F").End(xlUp).Row

For i = 2 To lastRow
    If Not IsEmpty(Cells(i, "F")) Then
        On Error Resume Next
        values.Add Cells(i, "F").Value, CStr(Cells(i, "F").Value)
        If Err.Number <> 0 Then
            Cells(i, "AN").Value = "x"
        End If
        On Error GoTo 0
    End If
Next i

End Sub

所以我在这里描述我想要达到的目标:
我有一个excel文件,其中我的主要重点是列C,E,F和AN。

C -项目编号,E -国家,F -供应商和AN - X标记
在以下情况下,我需要在AN列中有X标记:-条件是AN中的X对于物料- C、供应商- F和国家/地区- E只能出现一次,例如

总会有相同的国家:斯洛伐克、捷克、罗马尼亚、PL、HR、保加利亚、德国

  • 也有多行,我只做了几个屏幕
  • 我试着把它做对,但我做不到
    对不起,如果我写的帖子在错误的方式。
hrirmatl

hrirmatl1#

大家好欢迎来到Stack Overflow
我相信你的问题问对了。至少你说清楚了期望的产出是什么。
所以你要做的是:为那些给定值的并集不同的行设置一个值“x”。下面是我的解决方案代码(你可以找到一些注解,这样你就可以理解一切):

Sub markDistinct()
    ' Declaration of some variables
    Dim rowsNum As Integer
    Dim rgToFilter As Range
    
    ' This code will consider your headers are in the 1st row and your data at 2nd row
    ' Change cell references otherwise
    
    ' Concat formula of the columns of interest (C, E, F)
    ' Place the formula in the first data row of column AN (the one we will fill with "x")
    Range("AN2").Formula = "=CONCATENATE(C2,E2,F2)"
    
    ' Store the number of rows of your data
    rowsNum = Range("C1").End(xlDown).Row
    
    ' Select the range we will filter (column AN)
    Set rgToFilter = Range("AN2:AN" & rowsNum)
    ' Drop down the concat formula to all the rows
    rgToFilter.FillDown
    
    ' Filter by distinct concatenation values
    Range("AN1:AN" & rowsNum).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
    
    ' For each distinct filtered values assign the value "x" in the AN column
    For Each cl In rgToFilter.SpecialCells(xlCellTypeVisible)
        cl.Value = "x"
    Next cl
    
    ' Unfilter data
    ActiveSheet.ShowAllData
    
    ' For each cell in column AN, if its value is different from "x", then empty/clear it 
    For r = 2 To rowsNum
        If Cells(r, "AN") <> "x" Then Cells(r, "AN").ClearContents
    Next r
End Sub

虽然我已经尝试尽可能地使其适应您的需要,但您可能需要进行一些小的更改才能使其适合您的数据。希望这对您有所帮助

ua4mk5z4

ua4mk5z42#

标记第一行(多列)

  • 标记前几行,即运行以下命令后将保留的行:
ws.Range("A1").CurrentRegion.RemoveDuplicates Columns:=Array(3, 5, 6), Header:=xlYes
Option Explicit

Sub FlagFirstRows()

    Const DST_COL As Long = 40 ' AN
    Const DST_FLAG As String = "x"
    Const SRC_DELIMITER As String = "@"
    Dim sCols(): sCols = VBA.Array(3, 5, 6) ' C, E, F
    
    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    
    Dim rg As Range, rCount As Long

    With ws.Range("A1").CurrentRegion
        rCount = .Rows.Count - 1
        If rCount = 0 Then Exit Sub
        Set rg = .Resize(rCount).Offset(1)
    End With
    
    Dim sData(): sData = rg.Value
    Dim dData(): ReDim dData(1 To rCount, 1 To 1)
    
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
    dict.CompareMode = vbTextCompare
    
    Dim cUB As Long: cUB = UBound(sCols)
    
    Dim r As Long, c As Long, rString As String
    
    For r = 1 To rCount
        rString = sData(r, sCols(0))
        For c = 1 To cUB
            rString = rString & SRC_DELIMITER & sData(r, sCols(c))
        Next c
        If Not dict.Exists(rString) Then
            dict(rString) = Empty
            dData(r, 1) = DST_FLAG
        End If
    Next r
    
    rg.Columns(DST_COL).Value = dData

    MsgBox "First rows flagged.", vbInformation
    
End Sub

相关问题