input框vba显示单元号而不是值

我有以下代码提示用户单击单元格值。

Dim sDate As Range On Error Resume Next Application.DisplayAlerts = False Set sDate = Application.InputBox(Prompt:= _ "Please select start date.", _ Title:="Start Date", Type:=8) On Error GoTo 0 Application.DisplayAlerts = True If sDate Is Nothing Then Exit Sub Else sDate.Font.Bold = True End If End Sub 

然而,一旦一个值被选中,input框可以说例如我点击b3显示$ b $ 3。 我想显示$ b $ 3内的值。 例如,如果17-Jun在$ b $ 3以内,它应该在input框中显示17-Jun而不是$ b $ 3。

另一个答案是使用UserForm。

创build一个用户窗体,如下所示:

在这里输入图像说明

注:“确定”button名为“确定”,白色文本框为“date框”

对于表格代码,请使用:

 Private Sub Ok_Click() ActiveCell.Font.Bold = True UserForm1.Hide End Sub 

然后在工作表模块中,把这个:

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Cells.Count = 1 Then UserForm1.dateBox.Value = Target.Value End Sub Sub bold_Date2() UserForm1.Show vbModeless End Sub 

然后运行bold_date2()

在这里输入图像说明

我知道你需要从用户那里得到input,并且假定只提供单个单元。 你需要检索选定单元格的值,并将其字体样式设置为粗体。

您可以通过首先获取选定的单元格引用来实现此目的,使用Font属性来设置任何字体样式并读取其Value属性以获取其内容。

  Set sDate = Application.InputBox(Prompt:= _ "Please select start date.", _ Title:="Start Date", Type:=8) sDate.Font.Bold = True MsgBox ("Selected cell's value is: " & sDate.Value) 

这对你如何工作?

 Sub bold_Date() Dim sDate As Range On Error Resume Next Application.DisplayAlerts = False Set sDate = Application.InputBox(Prompt:="Please select start date.", Title:="Start Date", Type:=8) If sDate.Cells.Count > 1 Then Set sDate = sDate.Cells(1, 1) MsgBox ("Date is: " & sDate.Text) Application.DisplayAlerts = True If sDate Is Nothing Then Exit Sub Else sDate.Font.Bold = True End If End Sub 

它会在select一个范围后,把一个消息框与date。 此外,它有一个检查多个单元格。 如果select了多个单元格,则会使用该范围内的第一个单元格作为新的sDate