这段代码有时成功执行,有时报错“类Range的Select方法无效”
搞不懂啊!请大侠指点!
xlSheet.Range("A" & L).Select
             Selection.EntireRow.Insert

解决方案 »

  1.   

    比方说后面添加下面这段代码,它就报错:“类Range的Select方法无效”
    然后无论是把添加的代码删除还是保存关闭,都不能正常运行了。
    换台机器好像就可以运行了,真奇怪!xlSheet.Range("A" & L).Select
                 Selection.EntireRow.Insert
    xlSheet.Range(Cells(L, 1), Cells(L, 34)).Select
                 Selection.Interior.ColorIndex = 0
                 Selection.Copy
      

  2.   

    昨天晚上把Selection那行向左移三格,然后再移回来……它也能正常运行了
    我昏倒!这玩意儿整人的?
      

  3.   

    今早我把代码写完了,它又报错了……
    高手啊!提点提点小弟吧!!!
      L = 3
      Do
      Select Case xlSheet.Cells(L, 1)
        Case Is <> ""
          L = L + 1
      End Select
      Loop Until xlSheet.Cells(L, 1).Value = ""
    xlSheet.Range("A" & L).Select
                 Selection.EntireRow.Insert
    For R = 1 To 6
      xlSheet.Cells(L, ZR(R)) = "●"
    Next R
    xlSheet.Cells(L, 1) = Text2.Text
    xlSheet.Range(Cells(L, 1), Cells(L, 34)).Select
                 Selection.Interior.ColorIndex = 0
                 Selection.Copy
    xlBook.Sheets("直连").Select
                 Range("A" & L).Select
                      Selection.Insert Shift:=xlDown
                      Selection.Copy
    xlBook.Sheets("左斜连1").Select
                 Range("A" & L).Select
                      Selection.Insert Shift:=xlDown
                      Selection.Copy
    xlBook.Sheets("左斜连2").Select
                 Range("A" & L).Select
                      Selection.Insert Shift:=xlDown
                      Selection.Copy
    xlBook.Sheets("左斜连3").Select
                 Range("A" & L).Select
                      Selection.Insert Shift:=xlDown
                      Selection.Copy
    xlBook.Sheets("左斜连4").Select
                 Range("A" & L).Select
                      Selection.Insert Shift:=xlDown
                      Selection.Copy
    xlBook.Sheets("横连").Select
                 Range("A" & L).Select
                      Selection.Insert Shift:=xlDown
                      Selection.Copy
    xlBook.Sheets("右斜连1").Select
                 Range("A" & L).Select
                      Selection.Insert Shift:=xlDown
                      Selection.Copy
    xlBook.Sheets("右斜连2").Select
                 Range("A" & L).Select
                      Selection.Insert Shift:=xlDown
                      Selection.Copy
    xlBook.Sheets("右斜连3").Select
                 Range("A" & L).Select
                      Selection.Insert Shift:=xlDown
                      Selection.Copy
    xlBook.Sheets("右斜连4").Select
                 Range("A" & L).Select
                      Selection.Insert Shift:=xlDown
    xlBook.Sheets("Sheet1").Select
    xlSheet.Range(Cells(L - 10, 1), Cells(1, 34)).Select
                 Selection.Interior.ColorIndex = 0
    xlSheet.Range(Cells(L - 5, 1), Cells(L - 9, 34)).Select
                 Selection.Interior.ColorIndex = 35
    xlSheet.Range(Cells(L, 1), Cells(L - 4, 34)).Select
                 Selection.Interior.ColorIndex = 36
      

  4.   

    你最好是把你的Excel表格传上来,在VBA中,最好少用select方法,会加大程序运行的时间,其实有很多的语句都可以精简
    比如: *.select
          selection.* = *
    像这样的语句,你可以直接写成* . * = *,把select和selection都去掉!
      

  5.   

    去掉select和selection后,报错:“类Range的_worksheet方法错误”
      

  6.   

    刚才我也遇到这个问题,请教高手了,原来设置某个单元格时,必须保证该单元格所属的Worksheet是活动的(workSheet1.Activate())