首页 > 解决方案 > 提示用户选择一个单元格,从该单元格获取和存储字符串

问题描述

基本上我正在尝试从用户选择的单元格中获取一个字符串并存储该字符串以供以后使用。

目前,我在现有循环中多次使用此功能。它第一次工作,然后就失败了。我想将用户选择的单元格中包含的字符串存储为单独的字符串变量 sCell。

Dim vendor As String, sCell As String

vendor = "New Test Vendor 8675309"

pInput = vendor & " does not appear in Vendor Database." & vbNewLine & vbNewLine & "Does " & vendor & " already exist?" & vbNewLine & vbNewLine & _
     "If yes, please select the Vendor Number  that " & vendor & " belongs to." & vbNewLine & vbNewLine & _
     "Otherwise, please select a cell that says " & """" & "Does not Exist" & """" & "."

sCell = Application.InputBox(pInput)

执行宏后,第二次出现运行时错误类型不匹配。有小费吗?

标签: excelvbastringinput

解决方案


如果你想要一个范围,你应该Dim把你的变量作为一个范围

尝试:

Dim vendor As String
Dim sCell As Range

vendor = "New Test Vendor 8675309"

pInput = vendor & " does not appear in Vendor Database." & vbNewLine & vbNewLine & "Does " & vendor & " already exist?" & vbNewLine & vbNewLine & _
     "If yes, please select the Vendor Number  that " & vendor & " belongs to." & vbNewLine & vbNewLine & _
     "Otherwise, please select a cell that says " & """" & "Does not Exist" & """" & "."

Set sCell = Application.InputBox(Prompt:=pInput, Type:=8)

要获取地址,请使用sCell.Address,对于值,请使用sCell.Value


推荐阅读