473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Error 1004 on second line trying to set value or formatting of cell or range

gnawoncents
214 100+
I seem to be missing an unstated rule here and am looking for some insight.

I am running Excel 2016 on Windows 10 Enterprise and am getting an error when I try to set the value or formatting of a cell (or cells). The error itself (Run-time error '1004': Application-defined or object-defined error) doesn't tell me much.

Key notes:
- I get the error always on the SECOND line of code. The first works fine.
- The line of code works fine when the preceding line is "commented out" or deleted.
- I get the error whether I am calling the VBA from a command button or using a Worksheet_Change event.
- I get the error when using Cells or Range and whether setting the value or formatting.
- The code is all stored in and targeted to the same worksheet from which it is triggered.

Sample Code
Expand|Select|Wrap|Line Numbers
  1. Range(Cells(17, 2), Cells(22, 2)).ClearContents
  2. Range(Cells(14, 2), Cells(21, 2)).Font.ColorIndex = 0
  3.  
**Again, the error occurs on the second line, but I get no error if I don't run the first line. If I switch the order of the two lines, I still get the error on the second of the two lines. What am I missing?
Jul 17 '19 #1
1 3683
gnawoncents
214 100+
Thank you to everyone who took the time to look at this. I tried running the code on a new, blank worksheet and didn't get an error, so I did some extra troubleshooting and figured out what I was missing.

I apologize--my original question didn't have all the information anyone would have needed to figure out the problem.

My worksheet is protected and VBA unprotects it to make changes then re-protects it before closing. What I didn't realize was that assigning a value from VBA would trigger the Worksheet_Change event. Consequently, after assigning the first value, I was inadvertently calling the procedure and re-protecting the sheet. This drove the error the next time (2nd line) I tried to assign a value or make a change.

I fixed the issue by disabling events at the start of my procedure and re-enabling at the end (see below). Rookie mistake! Thanks again all. Hopefully my oversight helps someone else out.

Expand|Select|Wrap|Line Numbers
  1. Application.EnableEvents = False
  2. 'Assign values and formats to cells and ranges here
  3. '
  4. '
  5. Application.EnableEvents = True
Jul 18 '19 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Mark C | last post by:
All, I have used Excel automation many times in the past without error until now. My machine has both Access 97 and XP installed but this code is written in 97 and I am using Excel XP. The code...
2
by: dailem | last post by:
I have a bit of code tied to a command button in Access that runs a query then transfers it to a new Excel worksheet (& new file). It works fine EVERY OTHER TIME THAT I RUN IT....what the 'heck...
4
by: kscdavefl | last post by:
I ahve a datagrid on a web form. I need to change the value in column 3 as follows. If the value in column 3 reads 0, I want to change it to read YES. How can I accomplish this task. ...
3
by: Maverick | last post by:
I'm new to this, but I have received so much great information here, I had to join. Here is a problem am encountering with the following code in MS Access: objSht.Activate ...
0
by: Jono | last post by:
Hello, I've been getting this message when closing excel (not necessarily when closing the workbook by itself, but when closing Excel and the workbook at the same time): ...
2
by: amolbehl | last post by:
I use VB6.0 and when I execute the code I have given below, I get runtime Error 1004 - Specified value is out of range. Set oXL = CreateObject("Excel.Application") oXL.Visible = True ...
4
by: ielamrani | last post by:
Hi, I am getting this error when I try to export to an excel sheet. When I click on a button to export the first time it's fine, I rename the exported excel sheet and I try to export it again and I...
0
by: grego9 | last post by:
When I run the following macro in Excel (using VBA) I get the run time error 1004 application defined or object defined error. I get the usual debug message and when I click end I actually get the...
1
by: TonyRandom | last post by:
I hope I haven't posted this in the wrong section, but it is a VBA problem, so please forgive me if it should be elsewhere. I have two tabs in a spreadsheet, both containing lists of parts codes. I...
5
by: scrapcode | last post by:
Hi everyone. I'm trying to write a macro to copy data from one Excel sheet to another in the same workbook when a certain criteria is met. My data table is an import from an Access DB. After the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.