Tooprogram.ru

Компьютерный справочник
0 просмотров
Рейтинг статьи
1 звезда2 звезды3 звезды4 звезды5 звезд
Загрузка...

Эксель ошибка 1004

VBA 1004 Error

Excel VBA Error 1004

VBA 1004 Error is an error we encounter while we execute a code in VBA it is also known as VBA Runtime error. While we work in VBA or in any other programming language or even in our daily work we encounter different kinds of errors. Sometimes even we miss a single character in the code which causes the whole code not to work or maybe the entire code is wrong.

Errors are definitely a part of the code we write. It may be unintentional but they exist. No matter how pro we are in coding, runtime error can occur anywhere. As explained above VBA 1004 Error is an error which occurs during the runtime of the code in excel. It is also called an application defined or object defined error.

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more

There are different types of reasons we get VBA Runtime Error 1004 in excel, let us learn a few of them.

  • VBA Runtime Error 1004: Method ‘Range’ of object ‘_ Global’ failed:

This error occurs when the range value we refer to VBA is incorrect. It is also called as Method “Range” of object’ _ Global’ failed.

  • VBA Run Time Error 1004: That Name is already taken. Try a different One:

We give the same name to a worksheet which is already taken by another worksheet.

  • VBA Runtime Error 1004: Unable to get the select property of Range class:

This is an error when we select a range in another worksheet without activating the worksheet we are referring to.

  • VBA Runtime Error 1004: Method ‘Open’ of object ‘Workbooks’ failed:
Читать еще:  C1 6775 5 ошибка

This error occurs when we try to open a workbook which is already open or the file is used by another program already.

  • VBA Runtime Error 1004: Sorry We Couldn’t Find:

We get this error when we try to open a worksheet which doesn’t exist.

As we have learned there can be various reasons we get a runtime error. Runtime error can occur at any line of code. We need to learn how to learn to handle these errors and it is called VBA Error Handling.

Example of VBA Runtime Error 1004 in Excel

Now as I have described different types of error which can occur during runtime of any VBA code now let us learn them how they appear with examples.

VBA Runtime Error 1004 – Example #1

  • Let us name this table header as DATA.

  • Go to the Developer tab click on Visual Basic to Open VB Editor.

  • Declare the sub-function to start writing the code.

Code:

  • Call the header we named by the following code written below.

Code:

  • When we run the code we can see in the excel that it has been selected as we have called the header correctly.

  • Now we misspell the spelling for the header name.

Code:

  • Run the code again to see the result.

We get excel VBA Runtime Error 1004 because we have misspelled the range name.

VBA Runtime Error 1004 – Example #2

We get this error when we try to rename a worksheet with a name which is already taken. For example, I have renamed sheet 1 as “Anand” and I will try to rename sheet 2 as same then see the result.

Читать еще:  Основные ошибки в презентациях

  • Go to the Developer tab click on Visual Basic to Open VB Editor.
  • Declare a sub-function to start writing the code.

Code:

  • Try to rename sheet 2 as Anand by the following code below,

Code:

  • Run the above code and see the result.

When I try to rename a sheet with the name which is already taken I get an Error.

VBA Runtime Error 1004 – Example #3

I will try to add the value from sheet 2 to a variable in sheet 3. But I will not activate the sheet 2 and see what happens.

  • Go to the Developer tab click on Visual Basic to Open VB Editor.
  • Declare a sub-function to start writing the code.

Code:

  • Declare two variables A and B as an integer.

Code:

  • In Variable B store the value of A in addition to cell A1 of sheet 2.

Code:

  • Let us suppose the code works and use msgbox function to display the value of B.

Code:

  • Run the code to see the result obtained.

We get this Error because we have not activated sheet 2 but we are trying to use a value of sheet 2.

VBA Runtime Error 1004 – Example #4

We encounter this runtime error when we have already the same name of workbook open but we try to open it again.

For this example, I have already renamed my workbook as VBA 1004 Error.xlsm and I will try to open it again which is already open and see if I get VBA 1004 Error.

  • Go to the Developer tab click on Visual Basic to Open VB Editor.
  • Declare a sub-function to start writing the code.
Читать еще:  Сетевая карта код ошибки 10

Code:

  • Declare a variable as the workbook.

Code:

Try to open the workbook we have currently already open with the following code.

Code:

Run the above code to see the result.

We get this error because we have already opened the same workbook already.

VBA Runtime Error 1004 – Example #5

We get this error when we try to open a workbook which doesn’t exist. This is somewhat similar to the above error we get as VBA cannot find the workbook.

  • Go to the Developer tab click on Visual Basic to Open VB Editor.
  • Declare a sub-function to start writing the code.

Code:

  • Try Open any workbook with the following code,

Code:

  • I have already deleted the sheet from the location.
  • Run the code to see the result.

As the sheet doesn’t exist at the given location we get this error.

Things to Remember

  • Always check for spelling mistakes.
  • Do not rename multiple worksheets with the same name.
  • Before calling any other reference to be sure to activate the respective worksheet.
  • Before trying to open any other worksheet ensure the path provided is correct.

Recommended Articles

This has been a guide to VBA 1004 Error. Here we discussed Excel VBA Runtime Error 1004 along with practical examples and downloadable excel template. You can also go through our other suggested articles –

All in One Software Development Bundle (600+ Courses, 50+ projects)

Ссылка на основную публикацию
Adblock
detector