Excel VBA 中的变量错误赋值
Assign value to variable error in Excel VBA
我是 VBA 的新人,从一开始我就遇到了一些问题。我想创建新的 sheet ,其名称与活动单元格中的值相同。我写了这样一个脚本:
Sub Makro1()
Dim country As String
Let country = ActiveCell.Value
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = country
End Sub
调试器点与 ActiveSheet.Name = country
一致。这段代码有什么问题?
命名工作表时,请注意以下限制:
工作表不能与已存在的同名
工作表.
工作表名称不能超过 31 个字符。
不允许使用某些特殊字符:这包括 forward/bacwkard 斜线、方括号、问号、星号和冒号。
工作表名称可能还有其他限制,但我会立即想到这些,因为我过去遇到过。
这是处理工作表名称验证的代码片段:
Function validateWSName(ByVal test_name As String) As String
'if name is blank, give a valid name to continue testing
If test_name = "" Then test_name = "wsName"
'if name is longer than 31 characters, then cut it off
If Len(test_name) > 31 Then test_name = Left(test_name,31)
'if name contains forbidden characters : \ / ? * [ or ] then eliminate them
test_name = Replace(test_name, ":", "")
test_name = Replace(test_name, "/", "")
test_name = Replace(test_name, "\", "")
test_name = Replace(test_name, "?", "")
test_name = Replace(test_name, "*", "")
test_name = Replace(test_name, "[", "")
test_name = Replace(test_name, "]", "")
'if name already exists in the worbook, then add a counter at the end
For Each ws In ThisWorkbook.Sheets
If ws.Name = test_name Then
keyName = ws.Name
counter = 1
test_name = test_name & counter
Do While test_name = keyName
If counter < 10 Then
test_name = Left(test_name, Len(test_name)-1) & counter
Else
test_name = Left(test_name, Len(test_name)-2) & counter
End If
counter = counter + 1
Loop
Exit For
End If
Next ws
validateWSName = test_name
End Function
如果您将此代码段添加到您的项目并按如下方式重写代码,那么您在那里出错的可能性会大大降低:
Sub Makro1()
Dim country As String
Let country = ActiveCell.Value
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = validateWSName(country)
End Sub
我是 VBA 的新人,从一开始我就遇到了一些问题。我想创建新的 sheet ,其名称与活动单元格中的值相同。我写了这样一个脚本:
Sub Makro1()
Dim country As String
Let country = ActiveCell.Value
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = country
End Sub
调试器点与 ActiveSheet.Name = country
一致。这段代码有什么问题?
命名工作表时,请注意以下限制:
工作表不能与已存在的同名 工作表.
工作表名称不能超过 31 个字符。
不允许使用某些特殊字符:这包括 forward/bacwkard 斜线、方括号、问号、星号和冒号。
工作表名称可能还有其他限制,但我会立即想到这些,因为我过去遇到过。
这是处理工作表名称验证的代码片段:
Function validateWSName(ByVal test_name As String) As String
'if name is blank, give a valid name to continue testing
If test_name = "" Then test_name = "wsName"
'if name is longer than 31 characters, then cut it off
If Len(test_name) > 31 Then test_name = Left(test_name,31)
'if name contains forbidden characters : \ / ? * [ or ] then eliminate them
test_name = Replace(test_name, ":", "")
test_name = Replace(test_name, "/", "")
test_name = Replace(test_name, "\", "")
test_name = Replace(test_name, "?", "")
test_name = Replace(test_name, "*", "")
test_name = Replace(test_name, "[", "")
test_name = Replace(test_name, "]", "")
'if name already exists in the worbook, then add a counter at the end
For Each ws In ThisWorkbook.Sheets
If ws.Name = test_name Then
keyName = ws.Name
counter = 1
test_name = test_name & counter
Do While test_name = keyName
If counter < 10 Then
test_name = Left(test_name, Len(test_name)-1) & counter
Else
test_name = Left(test_name, Len(test_name)-2) & counter
End If
counter = counter + 1
Loop
Exit For
End If
Next ws
validateWSName = test_name
End Function
如果您将此代码段添加到您的项目并按如下方式重写代码,那么您在那里出错的可能性会大大降低:
Sub Makro1()
Dim country As String
Let country = ActiveCell.Value
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = validateWSName(country)
End Sub