对象需要错误vba

我有一个macros,应该连接用户放入的各种string,以填充其他单元格为基于用户提供的attribs其他活动目录用户attribs。

Option Explicit Sub SubStringConcatenate() 'variables Dim strFirstName As String Dim strSurName As String Dim strDomain As String Dim strOU As String Dim strChildOU As String Dim intRowCount As Integer Dim rngLastRow As Range Set rngLastRow = Worksheets("NewADUserAttribs").Cells.End(xlUp) 'set first row to be processed intRowCount = 2 'loop until all used rows have been processed For Each rngLastRow In Sheets("NewADUserAttribs").UsedRange.Rows.Count 'define name variables strFirstName = Cells(intRowCount, 1).Value strSurName = Cells(intRowCount, 2).Value 'define initials cell location and concatenate Cells(intRowCount, 3).Value = Left(strFirstName, 1) & Left(strSurName, 1) ' define fullname cell location and concatenate Cells(intRowCount, 4).Value = strFirstName & " " & strSurName 'define SAM cell location and concatenate Cells(intRowCount, 5).Value = Left(strFirstName, 2) & Left(strSurName, 4) 'define domain string variable and logon range and concatenate Cells(intRowCount, 7).Value = strFirstName & "." & strSurName 'add 1 to row count to prepare for next row in table intRowCount = intRowCount + 1 Next End Sub 

当我debugging时没有错误。 但是,当我尝试运行macros,我得到的对象所需的错误,并没有说在代码中的位置。 我认为这是每个陈述的某处。 我正在使用Excel 2010。

 For Each rngLastRow In Sheets("NewADUserAttribs").UsedRange.Rows.Count 

应该

 For Each rngLastRow In Sheets("NewADUserAttribs").UsedRange.Rows 

但是你不在循环中使用rngLastRow 。 你可以像这样使用(例如):

 rngLastRow.Cells(3).Value = Left(strFirstName, 1) & Left(strSurName, 1)