简体   繁体   English

运行时错误1004-对象范围失败-VBA 2013

[英]Run-time error 1004 - Range of object failed - VBA 2013

I'm getting a run time error 1004, Method 'Range' of object '_Worksheet' failed. 我收到运行时错误1004,对象'_Worksheet'的方法'Range'失败。

This code worked before on a draft of this project, but when imported into new project file, i get this.... none of the named ranges or worksheet names have changed from previous version. 这段代码以前曾在该项目的草稿上起作用,但是当导入到新的项目文件中时,我得到了...。命名的范围或工作表名称均未从以前的版本更改。 Please help! 请帮忙!

Private Sub UserForm_Initialize()
'Set Variables to populate combobox
Dim rngProjects As Range
Dim ws1 As Worksheet

Set ws1 = Worksheets("Validation")

For Each rngProjects In ws1.Range("Projects")

Me.cboProject.AddItem rngProjects.Value
Me.cboAccount.AddItem rngProjects.Value

Next rngProjects

'Add static data for combobox
Me.cboTransactionType.AddItem "Income"
Me.cboTransactionType.AddItem "Expense"

It seems to me that either the sheet name changed (sometimes a leading or trailing space is not recognizable) or the name for the named range changed. 在我看来,工作表名称已更改(有时无法识别前导或尾随空格)或已命名范围的名称已更改。 Furthermore, it could also be that the named range moved to another sheet (other than the validation sheet). 此外,也可能是命名范围移动到了另一个工作表(验证工作表除外)。

If you check all of the above and furthermore use explicit references as suggested in the comments then you should be able to find the problem. 如果您检查了以上所有内容,并且进一步使用了注释中建议的显式引用,则应该可以找到问题所在。 Here is your code with some additional checks. 这是带有一些其他检查的代码。 Give it a try and let us know if this works or made you aware of the problem. 尝试一下,让我们知道是否可行或使您意识到问题所在。

Option Explicit

Private Sub UserForm_Initialize()
'Set Variables to populate combobox
Dim rngProjects As Range
Dim ws1 As Worksheet
Dim bolFound As Boolean
Dim nm As Name

For Each ws1 In ThisWorkbook.Worksheets
    If ws1.Name = "Validation" Then bolFound = True
Next ws1
If bolFound = False Then
    MsgBox "Required sheet not found."
    Exit Sub
End If

Set ws1 = Worksheets("Validation")

For Each rngProjects In ws1.Range(ws1.Cells(8, 1), ws1.Cells(ws1.Rows.Count, 1).End(xlUp))
    frmAddTransaction.cboProject.AddItem rngProjects.Value
    frmAddTransaction.cboAccount.AddItem rngProjects.Value
Next rngProjects

'Add static data for combobox
frmAddTransaction.cboTransactionType.AddItem "Income"
frmAddTransaction.cboTransactionType.AddItem "Expense"

End Sub

As commented below, the indirect named range has been removed. 如下所述,间接命名范围已被删除。 Instead, the sheet is now directly referenced in the code. 而是,该工作表现在直接在代码中引用。 Like this any change of the named range will have no impact on the code. 这样,对命名范围的任何更改都不会对代码产生影响。 Instead, the offset of 8 rows is written in the VBA where it is slightly more secure. 相反,8行的偏移是写在VBA它是稍微更安全。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA运行时错误1004:尝试在Excel 2013中创建表时,对象_Global的方法范围失败 - VBA Run-time error 1004: Method Range of object _Global failed when trying to create tables in Excel 2013 将工作表复制到 csv - VBA 时出错:object“范围”的方法“值”失败(运行时错误“1004”) - Error while copying sheet to csv - VBA: Method 'Value' of object 'Range' failed (Run-time error '1004') VBA 运行时错误“1004”:object“_Worksheet”的方法“范围” - VBA Run-time error '1004': Method 'Range' of object '_Worksheet' 运行时错误'1004'-VBA中对象'_Global'的方法'Range'失败 - Run-time error '1004' - Method 'Range' of object'_Global' failed in VBA Excel VBA 运行时错误'1004'对象'_Global'的方法'范围'失败 - Excel VBA Run-Time Error '1004' Method 'Range' of object'_Global' Failed VBA-运行时错误'1004'-对象'_Global'的方法'Range'失败 - VBA - Run-time error '1004' - Method 'Range' of object'_Global' failed VBA运行时错误'1004':对象'_Global'的方法'Range'失败 - VBA Run-time error '1004': Method 'Range' of object '_Global' Failed 损坏的Excel VBA宏运行时错误'1004':对象'_Global'的方法'Range'失败 - Broken Excel VBA Macro Run-time error '1004': Method 'Range' of object '_Global' failed 运行时错误 '1004' 'Range' of object'_Global' 失败 - Run-time error '1004' 'Range' of object'_Global' failed 运行时错误'1004':对象'_Global'的方法'Range'失败 - Run-time error '1004' : Method 'Range' of object'_Global' failed
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM