簡體   English   中英

運行時錯誤'1004':應用程序定義或對象定義的錯誤VBA

[英]Run-time error '1004': Application-defined or object-defined error VBA

我已經尋找解決方案並嘗試了許多選項,但是似乎沒有任何效果。

我對VBA也很陌生,您可能認為我的代碼需要做一些工作。 盡一切可能,請更改它。 如果您可以具體說明並解釋您的更改,那就太好了。

我在以下行中遇到了麻煩:

count = Application.WorksheetFunction.CountIf(Range(Cells(4, c), Cells(14, c)), Workbooks("Testing").Worksheets("Barrel MASTER").Range(BarrelSheet.Cells(r, "A")))

據我所知,我將所有變量定義為最佳。 在調試模式下並將鼠標懸停在上一行的最后一個范圍內時,我得到了預期的結果。 上一行在BarrelSheet.Cells(r, "A")沒有行變量的情況下工作。 它是Cells(“ A15”)時起作用。

這是其余的代碼:

 Sub Item_Location() Dim c As Integer 'Counter to cycle through columns in cage inventory Dim r As Integer 'Counter to cycle through Rows on MASTER sheets Dim Finalcolumn As Integer Dim ItemNum As Integer Dim count As Integer Dim multi As Integer 'Is there a repeat code in the same cage Dim multioverall As Integer 'Is there a repeat code within 2 different cages Dim ItemCode As String Dim BarrelMasterB As Workbook Dim BarrelSheet As Worksheet Dim CodeCage As Worksheet Set BarrelMasterB = Workbooks("Testing") Set BarrelSheet = Workbooks("Testing").Worksheets("Barrel MASTER") 'Application.ScreenUpdating = False 'MsgBox ("This may take some time. When completed you will be prompted") For r = 15 To 18 Workbooks("Testing").Activate 'ItemCode = Workbooks("Testing").Worksheets("Barrel MASTER").Range(BarrelSheet.Cells(r, "A")).Value Finalcolumn = Range("L1").Column ItemNum = Workbooks("Testing").Sheets(1).Range("B1000").End(xlUp).Row multioverall = 0 '------------ Start of Searching for 1 Item code and 1 Cage ------------- multi = 0 'Resets to 0 for different cage Workbooks.Open ("C:\\Users\\me\\Desktop\\Camp Spider Web\\Inventory\\Cage Inventory\\Cage 1.xlsm") 'Opens Cage file to search for specified Code Set CodeCage = Workbooks("Cage 1").Worksheets("Cage Inventory Coded") For c = 5 To Finalcolumn count = Application.WorksheetFunction.CountIf(Range(Cells(4, c), Cells(14, c)), Workbooks("Testing").Worksheets("Barrel MASTER").Range(BarrelSheet.Cells(r, "A"))) 'Counts how many If count = 1 And multi = 1 Then 'If there was a double code in the same item row MsgBox ("Repeated Code. Check Item Codes on Cage Inventory") c = 12 'Ending the search ElseIf count = 1 Then Workbooks("Testing").Worksheets("Barrel MASTER").Range(Cells(r, "E")) = Workbooks("Cage 1").Worksheets("Cage Inventory Coded").Range("M4") 'returning cage # multi = multi + 1 'Keep track of how many codes have been found multioverall = multioverall + 1 If multioverall = 2 Then MsgBox ("There as been a second Item code found in Cage 1") GoTo Reset End If ElseIf count > 1 Then 'if there is a double item code in the same column MsgBox ("Repeated Code. Check Item Codes on Cage 1 Inventory") c = 12 'Ending the search for next column End If Next c Workbooks("Cage 1").Close SaveChanges:=True 'Saves and closes above cage '-------- Start of Searching for 1 Item code and 1 Cage ------------ Next r 'Application.ScreenUpdating = True 'MsgBox ("Now Complete") End Sub 

有些行的前面有一個',只是為了在我測試它時不會運行所有內容。 可能還有一些未使用的已定義變量。 這是因為我嘗試了很多事情,之后將刪除它們。 有些可能在以后使用。

該代碼將在“桶主”工作簿處於活動狀態時運行,但是會打開框架1工作簿以使其處於活動狀態。 搜索部分(位於-----之間的代碼)將被復制32次,因為將有32種不同的信息工作簿,所有這些工作簿都將以相同的格式進行搜索。 提前致謝! 干杯

嘗試這個:

count = Application.WorksheetFunction.CountIf(Range(Cells(4, c), Cells(14, c)), _
                                              BarrelSheet.Cells(r, "A").Value)

盡管您應該真正將Range()Cells()每次使用限定為一個特定的工作表,否則您將依賴於在特定時間處於活動狀態的特定工作表,這可能會導致許多難以調試的問題。

暫無
暫無

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

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