我对VBA很陌生。如何编写此VBA:
如果一个单元格以DY开头,则在另一个单元格中返回NAS。如果不 返回NAI。
答案 0 :(得分:0)
使此示例适应您的需求:
Sub simple()
If Left([A1], 2) = "DY" Then
[B1] = "NAS"
Else
[B1] = "NAI"
End If
End Sub
答案 1 :(得分:0)
我同意Pierre44(Excel公式可以解决问题),但是如果您想学习VBA,则可能需要使用更多详细的代码才能理解范围等。
Sub simpleVerbose()
' define constants for the strings
' to search and to output
Const SEARCH_FOR = "DY"
Const OUTPUT1 = "NAS"
Const OUTPUT2 = "NAI"
' declare variables for access to the cells
Dim rg1 As Range
Dim rg2 As Range
' assign cells from the activesheet
' If you use range without a reference
' it always refers to the activesheet
Set rg1 = Range("A1")
Set rg2 = Range("B1")
' rg1.value2 accesses the content of the cell
If Left(rg1.Value2, 2) = SEARCH_FOR Then
rg2.Value2 = OUTPUT1
Else
rg2.Value2 = OUTPUT2
End If
End Sub
也为初学者看一下text