[英]Excel - Unicode named cell range
我編寫了一個小函數來加速命名單元格:
Public Sub InputSelectedNames()
For Each Cell In Selection
On Error Resume Next
Dim strName As String
strName = InputBox("Enter name for cell " & Cell.Address(False, False) & ":", "Name cell")
If Len(strName) = 0 Then
Exit Sub
End If
ActiveSheet.Names.Add Name:=strName, RefersTo:=Cell
Next Cell
End Sub
在我嘗試輸入unicode字符串作為名稱之前,此方法非常有用。 如果輸入“αβγ”之類的內容,則顯示為“aß?”。 在輸入框中。
我已經閱讀了有關使用ANSI編碼的VBA的信息,所以我理解為什么會這樣。 該教程中給出的示例解決方案可用於顯示unicode(通過傳遞指針而不是字符串對象),但是我無法將相同的想法用於InputBox。
如果我通過“公式”選項卡上Excel的“定義名稱”按鈕輸入這些字符,或者通過引用另一個單元格中的字符來輸入這些字符,則可以正常工作,因此,誤翻譯肯定出現在InputBox中。
我的猜測是,除非有人知道如何使InputBox與Unicode完美配合,否則我將必須制作一個Userform來處理輸入?
創建一個自定義InputBox確實解決了這個問題。
frmInputBox : UserForm
lblOutput : Label providing prompt
txtInput : TextBox for input
cmdOK : CommandButton for accepting input
cmdCancel : CommandButton for aborting prompt
VBA表單代碼:
Private Sub cmdOK_Click()
Accept
End Sub
Private Sub cmdCancel_Click()
Cancel
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Cancel
End Sub
Private Sub txtInput_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Then
Accept
ElseIf KeyCode = vbKeyEscape Then
Cancel
End If
End Sub
Private Sub Accept()
Me.Hide
End Sub
Private Sub Cancel()
txtInput.Text = ""
Me.Hide
End Sub
Public Function Prompt(strPrompt As String, Optional strTitle As String = "Input", Optional strDefault As String = "")
With Me
.Caption = strTitle
.lblOutput.Caption = strPrompt
.txtInput.Text = strDefault
.StartUpPosition = 0
.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
.Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
.Show
End With
If txtInput.Text <> "" Then
Prompt = txtInput.Text
Else
Prompt = ""
End If
Unload Me
End Function
像這樣稱呼:
strMyName = frmInputBox.Prompt("What's your name?", "Name Prompt")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.