Exported excel sheet data validation

Nikhil2803

New Member
Joined
Jul 18, 2023
Messages
37
Office Version
  1. 365
Platform
  1. Windows
Hi Team, I am working on a request where an excel sheet has few columns with columns format like(SSN , ZIP code and YYYYMMDD format) data. Now on the same sheet there is a button. If I click on that button, then all the sheet data will be exported to a new excel sheet and that will be saved into user's desktop. The problem I am facing is, I need to get the same value that is provided in the original sheet. Like if SSN is 000999000, it should be same in the exported excel, but now it is deleting the preceding zeroes(same for ZIP also).
Is there any way, where I can add the same data validations in the newly created excel sheet also. Than you for the help.
 
You could also adjust your current code to also paste the data validation.

VBA Code:
        ' Paste it in say Cell A1. Change as applicable
        wsO.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        wsO.Range("A1").PasteSpecial Paste:=xlPasteValidation, Operation:=xlNone, SkipBlanks:=False, Transpose:=False '<-- NEW

This will work unless the validation rule(s) point to a list contained in a range of cells not available in the copied workbook. This goes back to what I said earlier about needing to understand your validation rules.
So, what I am doing is. In my excel sheet, in a worksheet, there are some 15-20 columns. Out of these, there are 4-5 columns that are having Format cells condition. Like, there is a column which can take only SSN format, there is a column which will take only ZIP format, there is a column that is having YYYYMMDD format. suppose I give 07891 in ZIP column. Now in the same sheet, there is a button that will export all the data to a new sheet(create a new excel and save it to desktop). In the new sheet the preceding zero is getting removed. It is making ZIP as 7891. I want the value 07891 in the new sheet as well.
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
So, what I am doing is. In my excel sheet, in a worksheet, there are some 15-20 columns. Out of these, there are 4-5 columns that are having Format cells condition. Like, there is a column which can take only SSN format, there is a column which will take only ZIP format, there is a column that is having YYYYMMDD format. suppose I give 07891 in ZIP column. Now in the same sheet, there is a button that will export all the data to a new sheet(create a new excel and save it to desktop). In the new sheet the preceding zero is getting removed. It is making ZIP as 7891. I want the value 07891 in the new sheet as well.

Then that is probably a cell format issue not a data validation issue. Right now you are pasting only values (xlPasteValues).
Option 1 is to paste everything:
VBA Code:
wsO.Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Option 2 is to paste values, then paste formats:
VBA Code:
        ' Paste it in say Cell A1. Change as applicable
        wsO.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        wsO.Range("A1").PasteSpecial Paste:=xlPasteFormats , Operation:=xlNone, SkipBlanks:=False, Transpose:=False
 
Upvote 0
You could also adjust your current code to also paste the data validation.

VBA Code:
        ' Paste it in say Cell A1. Change as applicable
        wsO.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        wsO.Range("A1").PasteSpecial Paste:=xlPasteValidation, Operation:=xlNone, SkipBlanks:=False, Transpose:=False '<-- NEW

This will work unless the validation rule(s) point to a list contained in a range of cells not available in the copied workbook. This goes back to what I said earlier about needing to understand your validation rules.
You could also adjust your current code to also paste the data validation.

VBA Code:
        ' Paste it in say Cell A1. Change as applicable
        wsO.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        wsO.Range("A1").PasteSpecial Paste:=xlPasteValidation, Operation:=xlNone, SkipBlanks:=False, Transpose:=False '<-- NEW

This will work unless the validation rule(s) point to a list contained in a range of cells not available in the copied workbook. This goes back to what I said earlier about needing to understand your validation rules.

Then that is probably a cell format issue not a data validation issue. Right now you are pasting only values (xlPasteValues).
Option 1 is to paste everything:
VBA Code:
wsO.Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Option 2 is to paste values, then paste formats:
VBA Code:
        ' Paste it in say Cell A1. Change as applicable
        wsO.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        wsO.Range("A1").PasteSpecial Paste:=xlPasteFormats , Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Both the steps are working perfectly. Thank you so much for your valuable inputs. I just need one more help. Is there any we can also copy the same column breadth in the new sheet?
 
Upvote 0
Please do not mark your own posting acknowledging that some other posts by someone else originally contains the solution.
Please mark the original actual post by the person who came up with it.

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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