【问题标题】:Build a series of number strings based on combinations of True and False in another column根据另一列中的真假组合构建一系列数字字符串
【发布时间】:2020-04-07 01:26:04
【问题描述】:

这是我正在使用的表格:

我在此项目的 B 列中手动输入“True”。例如,我手动输入 A 列,但目标是仅通过引用 B 列获得相同的结果。

我需要它从 166 开始计数并添加到数字字符串中,直到它达到一个真、一个双真(背靠背真)或一个双空白(空白单元格背靠背)。例如,B 列中的第一个单元格为空白,第二个单元格为“True”,第三个单元格为空白 - 因此输入 166、167。如果它变为空白 > True > True,则输入将为 167、168 , 前三行 169 个。

连续的真值不能超过两个,只有一个或两个。如果一行中有两个空格,则只输入一个数字(参见 179)。

我需要输入相同的内容(例如166、167),直到满足blank>true>blank、blank>true>true或blank>blank条件。然后它开始一个新的字符串并根据下一个条件输入,依此类推。

如果这令人困惑,请对第 # 行表示歉意...行号对 A 列中的 # 没有影响,只需引用 B 列即可。

感谢您的宝贵时间。

【问题讨论】:

    标签: excel


    【解决方案1】:

    我想我得到了它的工作。请查看下面的代码。它应该安装在您想要结果的工作表的代码表中。

    Private Sub Worksheet_Change(ByVal Target As Range)
        ' Variatus @STO 07 Apr 2020
    
        Dim Arr As Variant
        Dim Rng As Range
        Dim Result As String
        Dim R As Long, Ra As Long
    
        With Target
            If .Cells.CountLarge > 1 Then Exit Sub
    
            Set Rng = Range(Cells(2, 2), Cells(Rows.Count, 2).End(xlUp).Offset(1))
            If Not Application.Intersect(Target, Rng) Is Nothing Then
                Arr = Range(Cells(1, 1), Cells(Rows.Count, 2).End(xlUp).Offset(1)).Value
    
                For R = 2 To Rng.Rows.Count
                    If Result = "" Then Result = ResultString(Result, R, Arr)
                    Arr(R, 1) = Result
                    Cells(R, 1).Value = Result
                    If R < UBound(Arr) Then
                        If Arr(R + 1, 2) = False Then
                            Result = ResultString(Result, R + 1, Arr)
                        End If
                    End If
                Next R
            End If
        End With
    End Sub
    
    Private Function ResultString(ByVal Seed As Variant, _
                                  ByVal R As Long, _
                                  Arr As Variant) As String
        ' Variatus @STO 07 Apr 2020
    
        Const Start As Integer = 166
    
        Dim Fun As String
        Dim Sp() As String
        Dim i As Integer
    
        On Error Resume Next
        Sp = Split(Seed, ",")
        Seed = Val(Sp(UBound(Sp))) + 1
        If Err.Number Then Seed = Start
        Fun = Seed
    
        On Error GoTo 0
        Do While (R + i) < UBound(Arr)
            i = i + 1
            If Arr(R + i, 2) = False Then Exit Do
            Fun = Fun & ", " & CStr(Val(Seed) + i)
        Loop
    
        ResultString = Fun
    End Function
    

    事件过程响应 B 列中的更改,并将根据在那里找到的条目 - True 和 False(或空白)构建 A 列。每次更改都必须重建整个列。观察包含起始编号的Const Start As Integer = 166

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 2020-07-13
      • 1970-01-01
      • 2017-06-19
      • 1970-01-01
      • 2022-01-21
      相关资源
      最近更新 更多