0

我有一个宏,它应该连接用户输入的各种字符串,以填充基于用户提供的属性的其他活动目录用户属性的其他单元格。

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

当我调试时没有错误。但是当我尝试运行宏时,我得到了 object required 错误并且它没有说明它在代码中的位置。我认为它在 for each 语句中的某个地方。我正在使用 Excel 2010。

4

1 回答 1

4
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)
于 2012-09-04T19:32:10.483 回答