Excel VBA - 输入框不将数据写入单元格

时间:2017-03-01 19:34:03

标签: excel vba excel-vba

我在Workbook_Open上运行一个只读模板文件,该文件将重新用于新客户端。我在Name = .Range("L2")行显示错误"运行时错误' 91':对象变量或者没有设置块变量"我不完全确定它为什么会这样出现。我确信我只是简单地忽略了一些非常简单的东西,但是我们非常感谢任何帮助,如果有更有效的方法来编写这些代码,请告诉我。我渴望学习。

Private Sub Workbook_Open()
Dim tB As Workbook
Dim ws As Worksheet
Dim DailyWS As Worksheet
Dim Client As String
Dim Name As Range

Set tB = ThisWorkbook
With tB
    Set DailyWS = .Sheets("Daily Tracker")
End With 'tB
With DailyWS
    Name = .Range("L2")
End With 'DailyWS


If Len(Name.Value) = 0 Then
    Client = InputBox("Enter your name :", "Client's Name Please.")
    If Len(Client) = 0 Then 'Checking if Length of name is 0 characters
        MsgBox "Please enter a valid name!", vbCritical
        Goto Tryagain
    End If
    Name.Value = Client
    If Client = vbNullString Then
    MsgBox ("DO NOT CONTINUE!" & _
    "" & vbCrLf & _
    "DO NOT SAVE THIS FILE! " & vbCrLf & _
    "" & vbCrLf & _
    "AT PROMPT PLEASE ENTER A NAME!" & vbCrLf & _
    ""), vbOKOnly + vbCritical, "!! Name Required !!"
    Goto Tryagain
    End If
Else
MsgBox "Not Empty (Test)"
End
End If
end sub

1 个答案:

答案 0 :(得分:4)

Name是一个范围对象,需要设置所有对象:

Set Name = .Range("L2")

一个注意事项,我会小心使用vba中使用的单词。许多地方都使用Name。我会使用一个不替换现有命令的变量,如Nme