[英]“<Application-defined or object-defined error> Error 1004”
[英]Runtime error '1004': Application-defined or object-defined error
我有一個 VBA 宏代碼來對其他文檔進行數據清理。 在代碼的這個特定部分中,過濾然后在過濾后選擇列 header 下的特定單元格,如下所示,以便我可以更改所選特定單元格的值。
Dim myDB as Range
Set myDB = .Range("A1:" & ColumnLetter(lCol) & "1").Resize(.Cells(.Rows.Count,1).End(xlUp).Row) 'To set the variable as the whole worksheet
With myDB
.AutoFilter field:=policycolNo, Criteria1:="..."
.AutoFilter field:=customerTypeColNo, Criteria1:="..."
End With
Activesheet.Autofilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, customertypeColNo).Select
ActiveCell.Value = "..."
在輸入下面的循環之前,上面的代碼運行沒有問題。 但是,當引入此循環時,運行到下面的代碼行將返回運行時錯誤。
Activesheet.Autofilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, customertypeColNo).Select
這是更改列 A 中的值的循環,如下所示,因為我想將一些整數轉換為文本,因為它們是 vlookup 到另一個工作簿的唯一標識符。 這個循環將在上面的主要代碼段之前執行。
Dim r as Range
For Each r in Range("A:A")
r.NumberFormat = "@"
Next r
下面是一個表格,它證明了為什么我需要將整數更改為文本。
客戶ID | 姓名 | 會員類型 |
---|---|---|
00123 | 史黛西 | 優質的 |
00234 | 大流士 | 常規的 |
011545 | 傑羅姆 | 常規的 |
錯誤 Runtime error '1004': Application-defined or object-defined error 會阻止我的宏繼續進行。 任何想法為什么?
這比遍歷 A 列中的所有單元格更有效:
ActiveSheet.Columns(1).NumberFormat = "@"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.