【问题标题】:Moving row based on Input value in inputbox根据输入框中的输入值移动行
【发布时间】:2015-01-07 13:20:27
【问题描述】:

我需要一些帮助。我要做的是根据输入框中的用户输入移动一行。但是我收到了一个无效的限定符错误。帮帮我,谢谢!突出显示的错误是lSource 行。

Sub MoveRow()

       Dim lDestRow   As Long
       Dim lSourceRow As String
       Dim zMsg       As String

       lSourceRow = InputBox("Please Enter the row you want to move")
       zMsg = "Move Row " & Format(lSourceRow.Row) & " to Row?" & _
              vbCrLf & "Enter 0 to Exit"

       lDestRow = InputBox(zMsg, "Move Entire Row", 0)
       If lDestRow <> 0 And lDestRow <> lSourceRow Then

         ActiveCell.EntireRow.Select
         Selection.Cut
         Rows(lDestRow).Select
         Selection.Insert Shift:=xlDown

       Else

         If lDestRow <> 0 Then

           MsgBox "Source Row and Destination Row" & vbCrLf & _
                  "are the same -NO Action Taken-", _
                  vbOKOnly + vbInformation, _
                  "Invalid Row Move Request"

         End If

       End If

End Sub

【问题讨论】:

  • LSourceRow 是一个字符串而不是一个范围,所以LSourceRow.Row 会给出限定符错误。
  • 哦。所以我必须将 lsourcerow 声明为一个范围?它是这样工作的吗?抱歉,我是 VBA 新手
  • 啊,不,因为您实际上并没有将范围对象传递给它。相反,您不应该使用.Row,因为我相信用户已经输入了行号,对吗?同样,.Row 是 Range 对象属性。
  • 我希望它没有要求太多,但我试过了,但没有用。你能帮我写代码吗?

标签: excel vba


【解决方案1】:

不完全确定您想要实现什么。不过你可以试试看。

With ActiveSheet 'Replace to suit - e.g. Sheets("Sheet1")
    Dim lSourceRow As Long
    Dim lDestRow As Long
    Dim zMsg As String

    lSourceRow = InputBox("Please Enter the row you want to move.")
    zMsg = "Move Row " & Format(lSourceRow) & " to what Row?" & _
          vbCrLf & "Enter 0 to Exit"

    lDestRow = InputBox(zMsg, "Move Entire Row", 0)

    If lDestRow <> 0 And lSourceRow <> 0 Then
        If lDestRow <> lSourceRow Then
            .Rows(lSourceRow).Cut
            .Rows(lDestRow + IIf(lDestRow = 1, 0, 1)).Insert xlDown
        Else
            MsgBox "Source Row and Destination Row" & vbCrLf & _
                "are the same -NO Action Taken-", _
                vbOKOnly + vbInformation, _
                "Invalid Row Move Request."
        End If
    End If
End With

这是最简单的形式。您要做的事情必须包括对输入的大量测试。
就像测试它是否不是负数,不是字符串或小数,或者用户是否按下 CancelClose
这只是你开始的事情。我把剩下的留给你。
这实际上将行移动到目标行的先前位置。 HTH。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-10
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    相关资源
    最近更新 更多