2015-09-17 178 views
-1

我试图运行下面的代码。但它显示下标超出范围的错误。当我尝试调试它时,它在第5行中显示错误:Range(“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 
+1

我认为你的问题是你正在使用的引号。看起来他们来自Word文档或其他富文本格式编辑器。使用纯文本编辑器(如记事本)将所有引号替换为标准引号。你也应该避免选择表格和单元格,直接引用它们。如果您希望一旦检查报价情况,我可以进一步阐述该问题。 –

+0

非常感谢你的工作。错误只是引号。 – Komal

+0

但是现在我得到的错误是“应用程序定义的或对象定义的错误行”对于i = 1到3“ – Komal

回答

0

引号是古怪,并创建一个错误,但即使是更改为“正常”后quoates有一个下标超出范围的错误:

而不是使用Sheets,尽量Worksheets

Worksheets("Sheet1").Select 
+0

它与报价的事情工作正常。但是现在我得到了错误“应用程序定义的或对象定义的错误行”对于i = 1到3“ – Komal

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 
+0

谢谢但”双“之后的”_“是什么,在”Xname as String“之后, “ – Komal

+0

运行代码后,我仍然收到”应用程序定义的错误或对象定义的错误“错误。:( – Komal

+0

在'Dim'语句中有一个小的错字,'xsalar'应该是'xsalary'。@Komal修正这个错误,它应该可以工作 –

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