简体   繁体   English

CODE根据其他三个单元格的值更改单元格的文本

[英]CODE Change the text of a cell based on the value of three other cells

Everyone. 大家。

I am currently using the below code to change the text of cells based on what text is in other cells. 我目前正在使用以下代码根据其他单元格中的文本更改单元格的文本。 What I would also like to do, is on some cells change the text based on the Number/Date in those cells. 我还想做的是,在某些单元格上根据这些单元格中的数字/日期更改文本。

Example: if J2, K2 and L2 are either a date or a number, enter "N" into cell N2. 示例:如果J2,K2和L2是日期或数字,则在单元格N2中输入“ N”。 Any ideas which function would work for this? 任何想法哪个功能将对此起作用?

Code currently using to change text based on text: 当前用于基于文本更改文本的代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Row > 1 Then
        Application.EnableEvents = False
        Select Case Target.Column
            Case Columns("G:G").Column
                If UCase(Target.Text) = "Y" Then
                    Target = UCase(Target.Text)
                    Range("I" & Target.Row).Value = "N"
                    Range("M" & Target.Row).Value = "N"
                End If
            Case Columns("F:F").Column
                If UCase(Target.Text) = "CAR" Or UCase(Target.Text) = "BIKE" Then
                    Target = UCase(Target.Text)
                        Range("I" & Target.Row).Value = "N"
                            ElseIf UCase(Target.Text) = "N" Then
                                Target = UCase(Target.Text)
                                    Range("I" & Target.Row).Value = "Y"
                                    Range("G" & Target.Row).Value = "N"
                                    Range("H" & Target.Row).Value = ""
                End If
            Case Columns("F:F").Column
                If UCase(Target.Text) = "N" Then
                    Target = UCase(Target.Text)
                        Range("I" & Target.Row).Value = "Y"
                End If
            Case Columns("G:G").Column
                If UCase(Target.Text) = "Y" Then
                    Target = UCase(Target.Text)
                        Range("I" & Target.Row).Value = "N"
                End If
        End Select
        Application.EnableEvents = True
    End If
End Sub

You can create a custom function to do this, call it like: 您可以创建一个自定义函数来执行此操作,如下所示:

MsgBox(IsNumberOrDate(Range("J2:L2")))

Or for non-contiguous cells: 或对于不连续的单元格:

MsgBox(IsNumberOrdate(Range("J2", "F2")))

Here is how you can do that. 这是您可以执行的操作。 Create a function that returns a boolean value. 创建一个返回布尔值的函数。 Send the array of values to the function, which will then iterate the item values in the array, testing whether each is either numeric or date (that might actually be redundant unless your dates are string), if any of the items is neither , then the function returns False . 将值数组发送到函数,函数将迭代该数组中的项目值,测试每个值数字还是日期(除非您的日期是字符串,否则实际上可能是多余的),如果其中任何一个都不是 ,则该函数返回False If all items are either date/numeric, then the function returns True . 如果所有项目均为日期/数字,则该函数返回True You may need to modify it slightly depending on what you want to do with empty cells (these would evaluate as numeric ), ordinarily. 通常,您可能需要根据要对空单元格进行的操作对其进行稍作修改(这些单元格的计算结果为numeric )。

Function IsNumberOrDate(values As Variant) As Boolean
Dim ret As Boolean
Dim v As Variant
For Each v In values
    If IsNumeric(v) Or IsDate(v) Then
        ret = True
    Else
        ret = False
        Exit For
    End If
Next

IsNumberOrDate = ret

End Function

So in your case, you can do like: 因此,就您而言,您可以这样做:

If IsNumberOrDate(Range("J2:L2")) Then
     'do something if all items ARE number or date
     MsgBox "All values are numeric/date", vbOkOnly
Else
     'do something else if they are not
     MsgBox "NOT all values are numeric/date", vbCritical
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM