简体   繁体   中英

Error 1004 "application defined or object defined error"

I have been trying to made a command button to automatically fill a table on a summary sheet by the user specifying a date and then the code will check through each sheet (which have identical tables and data on them) and copy rows with matching date entries into the summary table.

I keep getting a 1004 error at the line "tst=cells(x,A)" and it says it fails to record range. I have tried setting different variable types and specifying the location with "Worksheets(sheet).Range("Ax")" but that still doesn't work.

Any help to fix this issue or improve the code would be great thanks.

this is my code:

Private Sub UpTbl_Click()
UpTbl.FontSize = 8
  
' Variables
Dim WS_Count As Integer
Dim I As Integer
Dim indSUM As Integer
Dim x As Integer
Dim Dte As Date
Dim tst As Date
Dim sheet As String
   
indSUM = 10
Dte = Worksheets("summary").Range("C4")
WS_Count = ActiveWorkbook.Worksheets.Count

    ' Begin the loop.
    For I = 3 To WS_Count
        sheet = Worksheets(I).Name
        ind = 5
        Worksheets(sheet).Activate
        
        
        For x = 6 To 900
        tst = Range("Ax")

             If Dte = tst Then
                Worksheets(sheet).Range("Ax:Jx").Copy
                Worksheets("SUMMARY").Range("NindSUM:WindSUM").PasteSpecial Paste:=xlPasteValue
                indSUM = indSUM + 1
                
            End If
            
        Next x
        
    Next I
    
  Sheets("SUMMARY").Activate
  End Sub

I believe you want to reference the variable x meaning you need:

tst = Range("A" & x)

To reference you larger range with variables you can use something like

With Worksheets(sheet)
     .Range(.Range("A" & x), .Range("J" & x))
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM