You need to open the VBEditor (Alt+f11), select your workbook in the Project Explorer window (top left, looks a bit like a windows explorer window), right-click and choose Insert module. Then paste this code in - I have added comments so that you can hopefully see what you need to alter for your own groups:
Option Explicit
Sub OutputGroupMembership()
Dim varGroups, varItem
Dim wksOut As Worksheet
Dim rngOut As Range
Dim varMembers
Dim lngIndex As Long
Dim strMemberlist As String
' this is the worksheet where the list will go
' adjust as required
Set wksOut = ActiveSheet
With wksOut
' add titles
.Range("A1").Value = "Group name"
.Range("B1").Value = "Member ID"
' first output cell
Set rngOut = .Range("A2")
End With
' this is the list of groups you want to get the members for
varGroups = Array("Group 1", "Group 2")
For Each varItem In varGroups
' output group name
With rngOut
.Value = varItem
.Font.Bold = True
End With
' move down a row
Set rngOut = rngOut.Offset(1)
' get member list
' returned as a comma separated list
strMemberlist = GetGroupUsers(varItem)
' split list into an array
varMembers = Split(strMemberlist, ",")
' loop and output member IDs
For lngIndex = LBound(varMembers) To UBound(varMembers)
rngOut.Offset(, 1).Value = varMembers(lngIndex)
Set rngOut = rngOut.Offset(1)
Next lngIndex
Next varItem
End Sub
Function GetGroupUsers(ByVal strGroupName As String) As String
Dim objGroup, objDomain, objMember
Dim strMemberlist As String, strDomain As String
On Error Resume Next
Set objDomain = GetObject("LDAP://rootDse")
strDomain = objDomain.Get("dnsHostName")
Set objGroup = GetObject("WinNT://" & strDomain & "/" & strGroupName & ",group")
For Each objMember In objGroup.Members
strMemberlist = strMemberlist & "," & objMember.Name
Next objMember
' strip off the leading comma
GetGroupUsers = Mid$(strMemberlist, 2)
End Function
Bookmarks