簡體   English   中英

With 語句產生運行時錯誤 438

[英]With statement produces Run-Time Error 438

我得到

運行時錯誤“438”
“對象不支持此屬性或方法。”

我正在使用激活另一個工作簿 (Changes_Database Workbook)的代碼,然后(在 Changes_Database Workbook 內部有一個名為Changes 的工作表)代碼插入一行並將其他行向下移動,復制下面單元格的格式,然后輸入日期和時間旁邊的密鑰、部件和過程名稱(基本上是描述,不重要)。

下面的代碼很慢:

Sub NewPart2()

'Sets Changes_Database as active contents and unprotects

    Set Cd = Workbooks.Open(Filename:="\\FILEPATH\Technology_Changes\Changes_Database_IRR_200-2S_New.xlsm", Password:="Swarf")
    Set Changes = Cd.Sheets("Changes")

Changes.Activate
ActiveSheet.Unprotect "Swarf"

'Selects the 2nd row of the database, which is the row after the headings
ActiveSheet.Rows("2:2").Select

'Inserts a new row and shifts the other rows down
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow

'Inputs the key that is being added to the new row
ActiveSheet.Range("A2").Value = Sheet1.Range("H4").Value

'Inputs the part and process name to the new row
ActiveSheet.Range("D2").Value = UCase(Sheet1.Range("E4").Value)
ActiveSheet.Range("E2").Value = Sheet1.Range("E5").Value

'Inputs the date and time for when it was added
ActiveSheet.Range("B2").Value = Now
ActiveSheet.Range("C2").Value = Now
ActiveSheet.Range("C2").NumberFormat = "h:mm:ss AM/PM"
ActiveSheet.Range("B2").NumberFormat = "dd/mm/yyyy"

'On Error Resume Next

            ActiveSheet.Protect "Swarf"
            ActiveWorkbook.Save
            ActiveWorkbook.Close SaveChanges:=True

On Error Resume Next

End Sub

激活另一個工作表需要很長時間才能讓這個模塊執行它的功能,所以我嘗試了一個With 語句,但我得到了那個錯誤。

我正在嘗試使用我的第二個代碼來提高此代碼的速度:(兩種代碼的屏幕截圖都可以在下面找到

Sub NewPart2()

Application.ScreenUpdating = False

Set y = Workbooks.Open(Filename:="\\FILEPATH\Technology_Changes\Changes_Database_IRR_200-2S_New.xlsm", Password:="Swarf")

    With y

      Sheets("Changes").Unprotect "Swarf"

        .Sheets("Changes").Rows("2:2").Select
        'Inserts a new row and shifts the other rows down
        .Sheets("Changes").Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow

        'Inputs the key that is being added to the new row
        .Sheets("Changes").Range("A2").Value = Sheet1.Range("H4").Value

        'Inputs the part and process name to the new row
        .Sheets("Changes").Range("D2").Value = UCase(Sheet1.Range("E4").Value)
        .Sheets("Changes").Range("E2").Value = Sheet1.Range("E5").Value

        'Inputs the date and time for when it was added
        .Sheets("Changes").Range("B2").Value = Now
        .Sheets("Changes").Range("C2").Value = Now
        .Sheets("Changes").Range("C2").NumberFormat = "h:mm:ss AM/PM"
        .Sheets("Changes").Range("B2").NumberFormat = "dd/mm/yyyy"

      Password = "Swarf"

        .Save
        .Close False

    End With

Application.ScreenUpdating = True

End Sub

在此處輸入圖片說明

在此處輸入圖片說明

不要使用(或嘗試使用) Selection WorksheetWorksheet Sheet沒有Selection屬性。

改變

.Sheets("Changes").Rows("2:2").Select
'Inserts a new row and shifts the other rows down
.Sheets("Changes").Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow

.Sheets("Changes").Rows("2:2").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow

旁注,您使用Sheets("Changes")如此頻繁,以至於您可以將它與您的With...End With並節省大量輸入。

Set y = Workbooks.Open(Filename:="\\FILEPATH\Technology_Changes\Changes_Database_IRR_200-2S_New.xlsm", Password:="Swarf")

With y.Sheets("Changes")
    .Rows("2:2").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
    ' and so on
End With

y.Save
y.Close False

非常重要:確保通過添加句點來限定With...End WithRowsRange調用. 預先。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM