Convert to Text and Value

Mr80s

New Member
Joined
Sep 1, 2019
Messages
20
Hello Board!

Ok, I'm trying to muddle my way through a large bit of code I'm cobbling together. It does several things but it's not very efficient or clean.. Here's what I'd like to do:

-Prompt to open an xlsx file. (this works)
-Determine the range of data and copy it to a new workbook. The data contains many rows and columns some columns have numbers of varying lengths with leading zeros.

Last NameFirst NameStudent IDRoleEmail
SmithSally123456Studentsomewhere@something.com
VaderDarth00443Teachersomplace@somewhere.com
LeeBruce00011111Kung Foomartial@arts.com
GatesBill994422Programmerbill@microsoft.com
HappyJoe0123student

<tbody>
</tbody>

<tbody>
</tbody>

I then parse this into a csv file with headings of:
sN,givenName,OtherID,Template,mail

I have a couple of problems.
1) I can copy the data and parse and change the headings but when I try to save as a .csv the original file must of formulas in it (the file can be from many sources and hundreds of rows long). So when I save as .csv it must be seeing formulas and converting to NULL as the end of the csv contains many rows of commas:
,,,,
,,,,
,,,,

So if I convert to value
Code:
With sourceBook.Sheets("Access Request Spreadsheet").UsedRange ThisWorkbook.Sheets("temp").Range("A1").Resize( _
        .Rows.Count, .Columns.Count) = .value

It removes the commas at the end of the file, BUT it also strips the leading zeros of my ID numbers which I need (and the numbers could be different lengths so I can't pre-format )

If I convert the range to text via
Code:
[COLOR=#303336][FONT=inherit]ThisWorkbook[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Worksheets[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"temp"[/FONT][/COLOR][COLOR=#303336][FONT=inherit]).[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Range[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"A1"[/FONT][/COLOR][COLOR=#303336][FONT=inherit]).[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Resize[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit] _
[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Rows[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Count[/FONT][/COLOR][COLOR=#303336][FONT=inherit],[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Columns[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Count[/FONT][/COLOR][COLOR=#303336][FONT=inherit]).[/FONT][/COLOR][COLOR=#303336][FONT=inherit]NumberFormat [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"@"[/FONT][/COLOR]

It doesn't strip the leading zeros which is good, BUT now it doesn't convert to values and so I'm getting the rows at the end of the csv with commas.

So I've tried to first convert to text, then to value. It doesn't strip the zeros, but it doesn't remove formulas and thus my csv is full of commas at the end.

Here's what I've tried:
Code:
  Set sourceBook = Workbooks.Open(fileName)With ThisWorkbook
        .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "Temp"
    End With
ThisWorkbook.Worksheets("temp").Range("A1").Resize( _
        .Rows.Count, .Columns.Count).NumberFormat = "@"
 With sourceBook.Worksheets("Access Request Spreadsheet").UsedRange
 ThisWorkbook.Sheets("temp").Range("A1").Resize( _
        .Rows.Count, .Columns.Count) = .value
 End With
 sourceBook.Close


I'd like to know how to:
-Open the workbook,
-find the range of data (could be hundreds of rows)
-strip out only the columns (via column headings) I want (I have this part working)
-write the rows with only the columns I want into a new file, rename the header row and save as .csv
-Keeping leading zeros and no rows of commas at the end of the file.

Anyone got any ideas?

Many thanks in advance!!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Well, I think I solved by issues.

Before saving to CSV, I clear all formatting within the usedrange. No more commas at the end of my CSV :)

Code:
sh2.UsedRange.SpecialCells(xlCellTypeBlanks).ClearFormats

I really should clean up this code though as it could be way more efficient.
 
Upvote 0

Forum statistics

Threads
1,214,575
Messages
6,120,344
Members
448,956
Latest member
Adamsxl

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