0

当代码运行到userNames = .Range("A1:A100") . 不知道如何解决这个问题,有什么建议吗?

Sub AddPix()

Dim userNames() As String
Dim userPixNames() As String
Dim currentUser As String
Dim myPassword As String
myPassword = "trade2013"
Sheets("Collection Slip").Unprotect Password:=myPassword

With Worksheets("User Management")
    'Assign the user names to an array (much faster than looping through range)
    userNames = .Range("A1:A100")
    userPixNames = .Range("B1:B100")
End With
For i = 1 To 100
    'The 1 in userNames(i,1) is needed because of the way
        'VBA creates an array assigned directly from a range
        '(as we did above)
    If Environ$("UserName") = userNames(i, 1) Then
        'Have I missed where the picture will be inserted?
        With Sheets("Collection Slip").Pictures.Insert _
            ("G:\ITS\Shared\Signature\" & userPixNames(i, 1) & ".jpg")
            .Top = Range("B31").Top
            .Left = Range("B31").Left
            .Width = 250
            .Height = 58
        End With
        'short-circuit the For loop when current user's name is found
        i = 100
    End If
 Next i
 'Sheets("Collection Slip").Protect Password:=myPassword
End Sub

此外,我已经厌倦了 Dim userNames() 作为 Variant 而不是 String。但是“类型不匹配”的错误仍然发生在 'userNames = .Range("A1:A100")

任何帮助,将不胜感激

4

1 回答 1

0

你应该Dim userNames() as Variant代替as String. 范围内的值数组默认为 Variant 类型。

于 2013-08-13T00:38:07.183 回答