错误438:对象不支持pr

时间:2017-09-25 23:19:39

标签: vba excel-vba excel

我正在编写一个代码,将列B中的值向上移动到列A值的同一行。当我运行以下代码时,它显示错误438对象不支持此属性或方法。

 Sub dataPreprocessing()

Dim counter As Integer
Dim A As String
Dim i As Long
counter = 0

For i = 1 To 2500
    Sheet1("test1").Range("B" & i).Select
    If IsEmpty((Sheet1("test1").Range("B[i]").Value)) Then
        counter = counter + 1
    Else
       A = Sheet1("test1").Range("B[i]").Value
       Sheet1("test1").Range("B" & i - (counter - 1)).Select
       Set Sheet1("test1").Range("B[i-(counter-1)]").Value = A
       Sheet1("test1").Range("B" & i).Select
       Set Sheet1("test1").Range("B[i]").Value = ""
       counter = 0

    End If
Next i
End Sub

1 个答案:

答案 0 :(得分:0)

我不知道你在逻辑方面做了什么,但下面应该有效。

请注意我在两种情况下如何使用Sheets("test1") V Sheet1

此外,范围始终如“A1”/“B2500”而非“A [1]”/“B [2500]”。

这是我做的两个改变。

您可以使用以下::

Sub dataPreprocessing()

    Dim counter As Integer
    Dim A As String
    Dim i As Long
    counter = 0

    For i = 1 To 2500
        Sheets("test1").Range("B" & i).Select
        If IsEmpty((Sheets("test1").Range("B" & i).Value)) Then
            counter = counter + 1
        Else
           A = Sheets("test1").Range("B" & i).Value
           Sheets("test1").Range("B" & i - (counter - 1)).Select
           Sheets("test1").Range("B" & (i - (counter - 1))).Value = A
           Sheets("test1").Range("B" & i).Select
           Sheets("test1").Range("B" & i).Value = ""
           counter = 0

        End If
    Next i
End Sub

Sub dataPreprocessing()

    Dim counter As Integer
    Dim A As String
    Dim i As Long
    counter = 0

    For i = 1 To 2500
        Sheet1.Range("B" & i).Select
        If IsEmpty((Sheet1.Range("B" & i).Value)) Then
            counter = counter + 1
        Else
           A = Sheet1.Range("B" & i).Value
           Sheet1.Range("B" & i - (counter - 1)).Select
           Sheet1.Range("B" & (i - (counter - 1))).Value = A
           Sheet1.Range("B" & i).Select
           Sheet1.Range("B" & i).Value = ""
           counter = 0

        End If
    Next i
End Sub