我如何摆脱这个VBA编译器错误? 无法分配给read_only属性

所以对于整个编程的东西,特别是在VBA方面,我是全新的。 我试图制作这个Excel工作表,需要库存清单和库存清单栏的需求清单,并发送给我(和其他同事)一份电子邮件,说明库存清单less于或less于等于手头库存的需要。 以下是我到目前为止(这是脱离我研究的其他工作):哦所以发生了什么事情,我有它的工作types,但我得到错误的消息框,不知道如何修复它。提前感谢您的帮助。

码:

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 

使用长数据types而不是整数,工作表行超出整数types的限制。

每个variables都需要一个types赋值Dim i, j, k as Long实际上等于Dim i as Variant, j as Variant, k as Long 。 这可能会产生意想不到的副作用。

从风格的angular度来看,在内联中声明多个variables会使代码更加清晰,而且我也build议不要这样做。

声明每个var在自己的行,并声明所有variables。 使用Option Explicit有助于强制执行后者。

如果使用适当的对象variables(您),则无需“激活”工作表。 您只能使用两个工作表: fromsheettosheet 。 而不是激活工作表,然后删除活动工作表,直接删除工作表(例如, fromsheet.delete等)

这应该至less让你开始 ,但正如你从上面的文本墙看到的,它充满了错误,无疑我错过了其中的一些。

 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