简体   繁体   中英

Run-Time Error '1004' on VBA Excel

I'm getting a Run-Time error '1004' (Application-defined or object-defined error). It's happening on the second to last line of code.

Also, does anyone know how to define the answer I get on the same line of code?

Private Sub selectTable()

Dim startingRow As Integer
Dim endingRow As Integer
Dim startingColumn As Integer
Dim endingColumn As Integer
Dim selectRow As Integer
Dim selectColumn As Integer

startingRow = 13 'This is the row where the table you are sihing to select starts
startingColumn = 5 'This is the row where the table you are wishing to select starts

endingRow = 0 'Variable to hold count of how many rows of data there are
endingColumn = 0 'Variable to hold count of how many columns of data there are

While Cells(startingRow, startingColumn) <> "" 'Counts number of rows that have data
    endingRow = endingRow + 1
    startingRow = startingRow + 1
    Wend

startingRow = 13 'This is the row where the table you are sihing to select starts
startingColumn = 5 'This is the row where the table you are wishing to select starts

While Cells(startingRow, startingColumn) <> "" 'Counts number of columns that have data
    endingColumn = endingColumn + 1
    startingColumn = startingColumn + 1
    Wend

startingRow = 13 'This is the row where the table you are sihing to select starts
startingColumn = 5 'This is the row where the table you are wishing to select starts

selectRow = startingRow + endingRow - 1
selectColumn = startingColumn + endingColumn - 1

Worksheets("General Information").Range(Cells(Row1, Col1), Cells(Row3, Col3)).Select 'Selects the entire range of the desired table

End Sub
With Worksheets("General Information")
    .Range(.Cells(Row1, Col1), .Cells(Row3, Col3)).Select
End with

See: What is the default scope of worksheets and cells and range?

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