Chuỗi hồ sơ cá nhân thường được sử dụng để lưu trữ thông tin cụ thể của người dùng bên ngoài ứng dụng / tài liệu để sử dụng sau này.

Ví dụ, bạn có thể lưu trữ thông tin về nội dung mới nhất trong hộp thoại / UserForm, số lần một sổ làm việc đã được mở hoặc số hóa đơn được sử dụng gần đây nhất cho một mẫu hóa đơn.

Chuỗi hồ sơ cá nhân cho mỗi người dùng có thể được lưu trữ trong Sổ đăng ký. Bạn cũng có thể sử dụng tệp INI, trên đĩa cứng cục bộ hoặc trên thư mục mạng chia sẻ.

Dưới đây là các macro ví dụ để ghi và đọc từ Chuỗi hồ sơ cá nhân trong Sổ đăng ký.

' the examples below assumes that the range B3:B5 in the active sheet contains

' information about Lastname, Firstname and Birthdate

Sub WriteUserInfoToRegistry()

' saves information in the Registry to

' HKEY_CURRENT_USER\Software\VB and VBA Program Settings\TESTAPPLICATION

On Error Resume Next

SaveSetting "TESTAPPLICATION", "Personal", "Lastname", Range("B3").Value

SaveSetting "TESTAPPLICATION", "Personal", "Firstname", Range("B4").Value

SaveSetting "TESTAPPLICATION", "Personal", "Birthdate", Range("B5").Value

On Error GoTo 0

End Sub

Sub ReadUserInfoFromRegistry()

' reads information in the Registry from

' HKEY_CURRENT_USER\Software\VB and VBA Program Settings\TESTAPPLICATION

Range("B3").Formula = GetSetting("TESTAPPLICATION", "Personal", "Lastname", "")

Range("B4").Formula = GetSetting("TESTAPPLICATION", "Personal", "Firstname", "")

Range("B5").Formula = GetSetting("TESTAPPLICATION", "Personal", "Birthdate", "")

End Sub

' the example below assumes that the range D4 in the active sheet contains

' information about the unique number

Sub GetNewUniqueNumberFromRegistry()

Dim UniqueNumber As Long

UniqueNumber = 0

On Error Resume Next

UniqueNumber = CLng(GetSetting("TESTAPPLICATION", "Personal", "UniqueNumber", ""))

On Error GoTo 0

Range("D4").Formula = UniqueNumber + 1

SaveSetting "TESTAPPLICATION", "Personal", "UniqueNumber", Range("D4").Value

End Sub

Sub DeleteUserInfoFromRegistry()

' deletes information in the Registry from

' HKEY_CURRENT_USER\Software\VB and VBA Program Settings\TESTAPPLICATION

On Error Resume Next

DeleteSetting "TESTAPPLICATION" ' delete all information

'DeleteSetting "TESTAPPLICATION", "Personal" ' delete one section

'DeleteSetting "TESTAPPLICATION", "Personal", "Birthdate" ' delete one key

On Error GoTo 0

End Sub