vba列出共享工作簿中的所有活动用户?

时间:2017-01-26 13:31:52

标签: excel vba

我正在使用以下代码尝试插入当前在我的共享工作簿中处于活动状态的用户列表:

Sub CurUserNames()
    uSize = UBound(ThisWorkbook.UserStatus)
    For i = 1 To uSize
    Range("F2").Value = "Users currently online:" & Chr(10) & ThisWorkbook.UserStatus(i, 1)
    Next
    End Sub

我希望它显示为一个列表,由逗号分隔如下:

Users currently online:
James Smith, Adam Black, Marie Rayner

有人可以告诉我如何做到这一点吗?

编辑:

使用@Jordan建议的代码:

Sub CurUserNames()

Dim str as String    

str = "Users currently online:" & Chr(10)

For i = 1 To UBound(ThisWorkbook.UserStatus)
     str = str & ThisWorkbook.UserStatus(i, 1) & ", "
Next

Range("F2").Value = Mid(str, 1, Len(str) - 2)

End Sub

我在工作簿打开/关闭事件中调用此代码,如下所示:

Private Sub Workbook_Open()
Call CurUserNames
ThisWorkbook.Save
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call CurUserNames
ThisWorkbook.Save
End Sub

每次我希望共享工作簿中的其他用户也会看到活动用户实时更新单元格F2时保存工作簿。

这似乎不起作用。它要求每个用户在查看单元格中更新的用户之前保存其工作簿的会话。请有人告诉我一种方法让它起作用吗?

1 个答案:

答案 0 :(得分:1)

首先尝试构建字符串,然后在循环之后设置单元格的值:

Sub CurUserNames()

Dim str as String    

str = "Users currently online:" & Chr(10)

For i = 1 To UBound(ThisWorkbook.UserStatus)
     str = str & ThisWorkbook.UserStatus(i, 1) & ", "
Next

Range("F2").Value = Mid(str, 1, Len(str) - 2)

End Sub