“我的代码无效”

时间:2015-09-17 13:59:14

标签: excel vba excel-vba

我正在尝试运行以下代码。但是它显示了下标超出范围的错误。当我尝试调试时,它在5行中显示错误:范围(“A1”)。选择 在调试时,当我将第4行的Sheet1作为Sheet2时,它不会在Sheet2上进行。 请帮我正确运行代码。

Sub excelmacro()
Application.ScreenUpdating = False

Sheets(“Sheet1”).Select
Range(“A1”).Select

Sheets(“Sheet2”).Select
Range(“A2”).Select

For i = 1 To 3
Sheets(“Sheet1”).Select
If Len(ActiveCell.Value) > 1 Then

Sheets(“Sheet1”).Select
Xname = Right(ActiveCell.Value, Len(ActiveCell.Value) - 6)
Xdesig = Right(ActiveCell.Offset(1, 0).Value, Len(ActiveCell.Offset(1, 0).Value) - 13)
Xsalary = Right(ActiveCell.Offset(2, 0).Value, Len(ActiveCell.Offset(2, 0).Value) - 8)


Sheets(“Sheet2”).Select
ActiveCell.Value = Xname
ActiveCell.Offset(0, 1).Value = Xdesig
ActiveCell.Offset(0, 2).Value = Xsalary
ActiveCell.Offset(1, 0).Select
Sheets(“Sheet1”).Select
ActiveCell.Offset(3, 0).Select

Else
i = 10
End If
i = i - 1
Next
Application.ScreenUpdating = True
End Sub

3 个答案:

答案 0 :(得分:0)

引号是奇怪的并且会产生错误,但即使在更改为“正常”之后,也会出现下标范围错误:

不要使用Sheets,请尝试Worksheets

Worksheets("Sheet1").Select

答案 1 :(得分:0)

总结我的评论:

原始代码中的双引号格式奇怪。使用记事本或VBA IDE将它们替换为适当的纯文本双引号。

如果启用Option Explicit,请务必在使用变量前声明变量。即使它没有开启,也是一个很好的做法。

(今晚我有更多时间时要更新)避免选择并使用ActiveCell / ActiveSheet引用。

对代码进行细微更改后,它应如下所示:

Sub excelmacro()
Dim i As Double, _
    Xname As String, _
    Xdesig As String, _
    Xsalary As String

Application.ScreenUpdating = False

Sheets("Sheet1").Select
Range("A1").Select

Sheets("Sheet2").Select
Range("A2").Select

For i = 1 To 3
Sheets("Sheet1").Select
If Len(ActiveCell.Value) > 1 Then

Sheets("Sheet1").Select
Xname = Right(ActiveCell.Value, Len(ActiveCell.Value) - 6)
Xdesig = Right(ActiveCell.Offset(1, 0).Value, Len(ActiveCell.Offset(1, 0).Value) - 13)
Xsalary = Right(ActiveCell.Offset(2, 0).Value, Len(ActiveCell.Offset(2, 0).Value) - 8)


Sheets("Sheet2").Select
ActiveCell.Value = Xname
ActiveCell.Offset(0, 1).Value = Xdesig
ActiveCell.Offset(0, 2).Value = Xsalary
ActiveCell.Offset(1, 0).Select
Sheets("Sheet1").Select
ActiveCell.Offset(3, 0).Select

Else
i = 10
End If
i = i - 1
Next
Application.ScreenUpdating = True
End Sub

答案 2 :(得分:0)

我认为这就是你要做的事情:

Sub excelmacro()

    Dim lastrowinSheet1 As Long
    Dim cellinSheet2 As Range
    Dim rCell As Range
    Dim x As Long

    With ThisWorkbook
        'Set a reference to cell A1 on Sheet2.
        Set cellinSheet2 = .Worksheets("Sheet2").Range("A1")

        With .Worksheets("Sheet1")
            'This will return the last row number containing data in column A.
            lastrowinSheet1 = .Cells(Rows.Count, 1).End(xlUp).Row

            'Now loop through each cell in column A of sheet1.
            For x = 1 To lastrowinSheet1
                If Len(.Cells(x, 1)) > 1 Then
                    cellinSheet2.Value = Right(.Cells(x, 1).Value, Len(.Cells(x, 1).Value) - 6)
                    cellinSheet2.Offset(, 1) = Right(.Cells(x, 1).Offset(1).Value, Len(.Cells(x, 1).Offset(1).Value) - 13)
                    cellinSheet2.Offset(, 2) = Right(.Cells(x, 1).Offset(2).Value, Len(.Cells(x, 1).Offset(2).Value) - 8)
                    Set cellinSheet2 = cellinSheet2.Offset(1)
                    x = x + 2
                End If
            Next x

        End With
    End With

End Sub

我试图拆开你的代码 - 我认为这就是它正在做的事情:

Sub excelmacro1()

    'Stop the screen flicker.
    Application.ScreenUpdating = False

    'Select cell A1 on Sheet1.
    Sheets(“Sheet1”).Select
    Range(“A1”).Select

    'Select cell A2 on sheet 2.
    Sheets(“Sheet2”).Select
    Range(“A2”).Select

    For i = 1 To 3

        'Select Sheet1 again.
        Sheets(“Sheet1”).Select

        'If the length of text in the ActiveCell is greater than 1 character then
        'execute the lines up to ELSE.
        If Len(ActiveCell.Value) > 1 Then

            'Select Sheet1 yet again.
            Sheets(“Sheet1”).Select

            'Hope the value in the ActiveCell isn't longer than 6 digits, or it will error out.
            'Take all characters from the ActiveCell except the last 6.
            Xname = Right(ActiveCell.Value, Len(ActiveCell.Value) - 6)
            'Take all characters from the ActiveCell except the last 13.
            Xdesig = Right(ActiveCell.Offset(1, 0).Value, Len(ActiveCell.Offset(1, 0).Value) - 13)
            'Take all characters from the ActiveCell except the last 8.
            Xsalary = Right(ActiveCell.Offset(2, 0).Value, Len(ActiveCell.Offset(2, 0).Value) - 8)

            'Select Sheet2.
            Sheets(“Sheet2”).Select
            'Place the values in ActiveCell and the two columns to the right.
            ActiveCell.Value = Xname
            ActiveCell.Offset(0, 1).Value = Xdesig
            ActiveCell.Offset(0, 2).Value = Xsalary
            'Select the next row down.
            ActiveCell.Offset(1, 0).Select
            'Active Sheet1 again.
            Sheets(“Sheet1”).Select
            'Select the cell 3 rows down from the previous row.
            ActiveCell.Offset(3, 0).Select
        Else
        'If the lengh of text in the ActiveCell is 1 character or less then set the value of i to 10.
        i = 10
        End If
        'Remove 1 from i.
        i = i - 1
    Next
    Application.ScreenUpdating = True
End Sub