簡體   English   中英

如何擺脫這個VBA編譯器錯誤? 無法分配給read_only屬性

[英]How do I get rid of this VBA compiler error? Can't assign to read_only property

因此,我對整個編程工作完全陌生,尤其是在VBA方面。 我正在嘗試制作此excel工作表,該工作表包含具有庫存清單和現有庫存列的零件清單,並在庫存少於或等於時向我(和其他同事)發送需要訂購的電子郵件給我等於現有庫存。 以下是到目前為止我所擁有的(這是通過我研究的其他工作完成的):哦,所以正在發生的事情是我已經可以正常工作了,但是我在消息框中出現錯誤,無法弄清楚該如何處理。修復它。感謝您的幫助。

碼:

Sub SendEmailOnPart()

'Move rows to new sheet for either TRUE or FALSE (needs ordered or does not)
  Dim firstrow, lastrow, r, torow As Integer
  Dim fromsheet, tosheet As Worksheet
  firstrow = 1
  Set fromsheet = ActiveSheet
  lastrow = ActiveSheet.Cells(Rows.Count, "H").End(xlUp).Row
  For r = firstrow To lastrow
    If fromsheet.Cells(r, "H") <> "" Then  'skip rows where column H is empty
      On Error GoTo make_new_sheet
      Set tosheet = Worksheets("" & fromsheet.Cells(r, "H"))
      On Error GoTo 0
      GoTo copy_row
make_new_sheet:
      Set tosheet = Worksheets.Add(After:=Worksheets(Worksheets.Count))
      tosheet.Name = fromsheet.Cells(r, "H")
copy_row:
      torow = tosheet.Cells.SpecialCells(xlCellTypeLastCell).Row + 1
      fromsheet.Cells(r, 1).EntireRow.Copy
      tosheet.Cells(torow, 1).PasteSpecial Paste:=xlPasteValues
    End If
  Next r
  Application.CutCopyMode = False
  fromsheet.Activate

' Go to the false worksheet

 Sheet ["FALSE"].Select

 ' Send Email of parts that need ordered

Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")

    Dim olMail As Outlook.MailItem
    Set olMail = olApp.CreateItem(olMailItemItem)

    olMail.To = "Josh.Emory@techii.com"
    olMail.Subject = "Part Room"
    olMail.Body = ""
    olMail.Send

' Delete sheet after sending email

Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True

Sheet ["TRUE"].Select

Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True

End Sub

:碼

olMailItemItem無效。 使用Option Explicit將幫助您避免出現這種錯字。

這些行將導致錯誤:

Sheet ["FALSE"].Select    
Sheet ["TRUE"].Select    

使用長數據類型而不是整數,工作表行超過了整數類型的限制。

每個變量都需要一個類型分配, Dim i, j, k as Long實際上Dim i, j, k as Long ,相當於Dim i as Variant, j as Variant, k as Long 這可能會產生意想不到的副作用。

從樣式的角度來看,內聯聲明多個變量會使代碼難以辨認,我也建議不要這樣做。

在自己的行上聲明每個var,然后聲明ALL變量。 使用Option Explicit幫助實施后者。

如果您正在使用適當的對象變量,則無需“激活”工作表。 您只能使用兩個工作表: fromsheettosheet 無需激活工作表然后刪除活動表,只需直接刪除工作表即可(例如fromsheet.delete等)。

這至少應該讓您入門 ,但是從上面的文本中可以看到,它充滿了錯誤,毫無疑問,我錯過了其中的一些錯誤。

Option Explicit
Sub SendEmailOnPart()
'1. Declare all variables on their own line
'2. Use Long data type instead of integer for your counter variables
'3. Put all of your declarations at the top of module, also for readability
'4. Declare ALL variables and use Option Explicit
'5. Get rid of "GoTo" spaghetti code & replace with more proper local Error handler
  Dim firstrow As Long
  Dim lastrow As Long
  Dim r as Long
  Dim torow As Long  
  Dim fromsheet As Worksheet
  Dim tosheet As Worksheet
  Dim olApp As Object 'Outlook.Application 
  Dim olMail as Object 'Outlook.MailItem
  Const olMailItem as Long = 0 'In case of late-binding

  firstrow = 1
  Set fromsheet = ActiveSheet

  'You've assigned ActiveSheet to variable fromsheet, so use it correctly:
  lastrow = fromsheet.Cells(fromsheet.Rows.Count, "H").End(xlUp).Row
  For r = firstrow To lastrow
    If fromsheet.Cells(r, "H") <> "" Then  'skip rows where column H is empty
      On Error Resume Next  '## Not ideal, but this is an OK place to use On Error Resume Next
      Set tosheet = Worksheets("" & fromsheet.Cells(r, "H"))
      If Err.Number <> 0 Then
      '# If there was an error, then create the new sheet
          Set tosheet = Worksheets.Add(After:=Worksheets(Worksheets.Count))
          tosheet.Name = fromsheet.Cells(r, "H")
      End If
      On Error GoTo 0

      torow = tosheet.Cells.SpecialCells(xlCellTypeLastCell).Row + 1

      fromsheet.Cells(r, 1).EntireRow.Copy
      tosheet.Cells(torow, 1).PasteSpecial Paste:=xlPasteValues
    End If
  Next r
  Application.CutCopyMode = False

' Go to the false worksheet
 Sheet["FALSE"].Select  '<~~ This line is going to cause an error  ####

 ' Send Email of parts that need ordered
 Set olApp = CreateObject("Outlook.Application")

    Set olMail = olApp.CreateItem(olMailItem)
    olMail.To = "Josh.Emory@techii.com"
    olMail.Subject = "Part Room"
    olMail.Body = ""
    olMail.Send

End Sub

暫無
暫無

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

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