用户在范围调用中定义的列字母

时间:2019-04-26 13:15:10

标签: excel vba

向用户询问引用列的字母;然后使用答案来获取此单元格的范围,该单元格在此行的某行中。 但是“范围”拒绝识别输入的字母。

在以下简单的两行中我缺少什么?

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="backWall">
      <div id="modalBox">
          <p>Some text in the Modal..</p>
          <span class="close">&times;</span>
      </div>
    </div>
  <table class="table table-striped">
      <thead class="thead-dark">
        <tr>
          <th scope="col">ID</th>
          <th scope="col">First Name</th>
          <th scope="col">Last Name</th>
          <th scope="col"></th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th scope="row">1</th>
          <td>Furkan</td>
          <td>Gulsen</td>
          <td>
            <button id="more" class="btn btn-dark more">MORE</button>
          </td>
        </tr>
        <tr>
          <th scope="row">2</th>
          <td>Jacob</td>
          <td>Allen</td>
          <td>
            <button id="more" class="btn btn-dark more">MORE</button>
          </td>
        </tr>
        <tr>
          <th scope="row">3</th>
          <td>Larry</td>
          <td>Quenn</td>
          <td>
            <button id="more" class="btn btn-dark more">MORE</button>
          </td>
        </tr>
      </tbody>
    </table>

4 个答案:

答案 0 :(得分:2)

尝试一下:

UserCol = Application.InputBox(" Please enter the column...", Type:=2)
Set Z = Range((UserCol & 5))

我将Type设置为 2 以从用户输入(see her for more)中返回一个字符串

此外,我在Range上加上了括号,因为(UserCol & 5)变成了A5,则需要Range(A5)

答案 1 :(得分:2)

您需要使用Type:=2

使用Type:=0将返回="<input string>",而不仅仅是<input string>

所以,之后:

Dim UserCol As String
UserCol = Application.InputBox(" Please enter the column...", Type:=2)

您可以执行以下操作:

Set Z = Cells(5, UserCol)

OR:

Set Z = Range(UserCol & "5")

我还建议您使用Option Explicit并完全限定范围引用。例如代替Set Z = Range(UserCol & "5"),使用Set Z = Thisworkbook.sheets("MySheetName").Range(UserCol & "5")

答案 2 :(得分:0)

这种方式更简单:

Dim UserCol As String
UserCol = Application.InputBox(" Please enter the column...")
Set Z = Cells(5, UserCol)

我不知道您是如何声明UserCol的,或者甚至还没有声明过它。如果您没有这样做,并且为了避免出现问题,请始终在模块顶部使用Option Explicit

答案 3 :(得分:0)

这是一个确实简单的错误,需要修复:Set Z = Range(UserCol & "5")

为什么?因为当您使用隐式转换时,通过键入UserCol & 5,VBA在UserCol和5之间(以及5之后)包含一个空格。

这是一个测试:

MsgBox "A" & 5 'Outputs "A 5 "
MsgBox "A" & "5" 'Outputs "A5"

(如Gravitate指出,Application.InputBox(" Please enter the column...", Type:=0)是一个公式,因此输入“ A”会给您"=""A"",而不是"A"-因为"=""A""5"不是有效的单元格引用,请为文本使用Type:=2或使用InputBox(" Please enter the column...")而不进行Application.或类型过滤)