让我更好地解释一下。如果A1的值是" 0"然后在A2中我想要公式= vlookup(B1,C1:E3,2,0),如果A1的值是" 1",那么我只想要A2的空白单元格值。我想在打开excel时发生这个宏。我认为这会起作用,但它不是
Sub test()
Dim indicator As Value
Dim result As String
indicator = Range("A1").Value
If indicator = 0 Then result = "=VLOOKUP(A3,C1:D3,2,0)"
Range("a2").Value = result
End Sub
答案 0 :(得分:1)
ThisWorkbook
部分中的将其放入
Sub Workbook_open()
If Range("A1") = 0 Then
Range("A2").Formula = "=vlookup(B1,C1:E3,2,0)"
ElseIf Range("A1") = 1 Then
Range("A2").ClearContents
Else
End If
End Sub
答案 1 :(得分:1)
如果您希望代码在特定工作表上运行,而不是上次保存文件时恰好处于活动状态的代码,请尝试以下操作:
Sub Workbook_open()
Dim ws As Worksheet
' define the worksheet to be changed here:
Set ws = ThisWorkbook.Worksheets("Sheet5")
If ws.Range("A1") = 0 Then
ws.Range("A2").Formula = "=vlookup(B1,C1:E3,2,0)"
ElseIf Range("A1") = 1 Then
ws.Range("A2").ClearContents
Else
End If
End Sub