Populating Worksheet in VBA

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,835
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Struck this a few years ago and can't remember what fixed it.

I'm clearing a sheet by Worksheets("Sheet1").Cells.ClearContents then filling parts of it with .Cells(row, column)=
Datatypes are mixed and what shows in the worksheet does not always follow, e.g. "1" may appear as 00 Jan 00.

Is there a way to set up the worksheet so whatever the datatypes are, keep their format?
Or do I need to set every individual cell format prior to assigning a value?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Struck this a few years ago and can't remember what fixed it.

I'm clearing a sheet by Worksheets("Sheet1").Cells.ClearContents then filling parts of it with .Cells(row, column)=
Datatypes are mixed and what shows in the worksheet does not always follow, e.g. "1" may appear as 00 Jan 00.

Is there a way to set up the worksheet so whatever the datatypes are, keep their format?
Or do I need to set every individual cell format prior to assigning a value?
When you clear the contents, include:
Code:
Cells.NumerFormat = "General"
Or you can just do it manually by clicking in the box above the row number "1" which will select all the cells, right click, "Format Cells", "Number Format", "General", "OK"

Excel has some built in features that automatically convert cell number formats to dates if a certain type entry is made into it, and it does not change back unless the user changes the number format or closes the file without saving it.
 
Last edited:
Upvote 0
If you use Worksheets("Sheet1").Cells.Clear instead, it would reset all the formatting to the default Normal cell style as well.
 
Upvote 0
Thanks Rory, have changed the code. One command instead of two, nice.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top