vba customize cells format - vba

I am needing to customize cells with simple thousands format, like 1000, without any separator or decimal.
However, I wish to remove text fonts other than a number when they are input.
For example, I want to input 120118, however in my paper from which I am copying that figures, it is formatted as a date, thereby 12/01/18. I am needing Excel to simply keep it as 120118 after typing, removing the slash (/). I have seen similar settings in access queries.

Have you tried simply pasting only the cell value with:
Selection.PasteSpecial Paste:=xlPasteValues
Or just clear the cell format and format it again with your desired format.

Try:
Selecting the range
Home > Number > Number Format (or Ctrl+1 I think) > Custom
Enter ddmmyy
Okay
Can be done programmatically e.g.
Thisworkbook.worksheets("Sheet1").range("A1:A50").numberformat = "ddmmyy"
The above would only be a visual/cosmetic change and the internal value of each cell would still be a date (technically a number) for calculation purposes.
However, if I've misunderstood and you instead want to go from the date 21 Jan 2018 to the number 210118, I think you would need to get the range's value(s), format as DDMMYY string, then clng() - or maybe (DD*10000) + (MM*100) + (YY) might work, then format as "000000" to preserve leading zeros.

Related

How to remove the ' for Cstr(combox1.value)

I use this to change the combox value in string type and paste on cells.
cells(i,1)=Cstr(combox1.value)
On Excel column A ,I have set the datatype as String .
For example , I input 017``123 098 065
However , i find that the exact value put on cells(i,1) is '017 '098 '056 123 .
The code starting with 0 contains ' at the beginning .
* Any way to improve my code without appearing the ' on exact cells ,for those codes starting with 0 *
The ' is probably a formatting character, which is a hangover from Lotus 1-2-3 where we used a leading ' to signify left-aligned and a leading " to signify right-aligned. (I think I remember that correctly.)
If it is just the formatting character (if it is, it won't be included in the character count returned by LEN) then you can safely just ignore it.
If you want to get rid of it for aesthetic reasons, you will need to delete the current contents of the cell, format the cell as General format, then format the cell as Text format, then insert new values into the cell.
Note: You can't just format the cell as Text, delete the contents, then insert a new value. The ' will still appear in that situation. The step to change it to General format, if it isn't already in that format, is important! (Maybe setting it to some other non-Text format will work just as well as setting it to General, but I know that General will work.)
Luckily, a .Clear will delete the contents of the cell and set the format back to General, so programatically we can do two things at once.
I therefore believe the following code should do what you want:
Cells(i, 1).Clear
Cells(i, 1).NumberFormat = "#"
Cells(i, 1).Value = combox1.Value
The first line is there simply to get rid of the issue that currently exists. If you were working with a completely new workbook, it wouldn't be needed.
convert the cell's number format to Text before setting numeric values.
cells(i,1).NumberFormat = "#"
cells(i,1)=combox1.value

How to format data copied from the Web and pasted into an Excel sheet without altering the string itself?

Let us say I need to copy a list of names from a webpage. Since I don't want to retain the formatting that is there in the webpage, I currently use Paste Special→"Match Destination format". This is simple and clean.
But Excel also removes any extra spaces present within the string.
e.g. ABC DEF will become ABC DEF.
This is also true for leading and trailing spaces. I believe this is the TRIM function at work which is part of the default settings.
What can I do to apply new formatting without applying the TRIM function? (Other than pasting with source formatting and changing the format manually?)
I think the question can be extended to other parts of the default settings. e.g. the default number formatting in Excel may not be what you want. I looked at the settings under Excel→Options but could not find the settings that specified number/text formatting.
Try to set that column formatting as Text (Manually) and performing Paste Special Values will consider the value as Text without changing anything in the pasted data.

vba treat time as string

I am storing some values in my excel using vba and them comparing with other results. The results can be whatever: name, surname, address, time...
The problem that I have is that when I store a string that has a date format it takes it as time automatically. For example if I enter 8:15 it automatically convert it to 08:15 and then when I am comparing it with other value that is the same (8:15) it returns me false because it is not 08:15.
So basically the question is... how can I introduce a value like 8:15 without being converted to 08:15?
Two methods:
Put an apostraphe at the front of the value to tell Excel it's a string. Example: The value '8:15 will be interpreted as a string of value "8:15".
Use a formula to store the value as a string. Example: ="8:15" is also interpreted as a string of value "8:15".
I'm assuming this is when you're entering the values in Cells. the format of the cells is probably general, thus it formats things like numbers or dates different. Change it to text.
select all (or specific columns) right click - format cells - number tab - text option. it says there "text format cells are treated as text even when a number is in the cell. the cell is displayed exactly as entered"
edit
another option, in VBA use .FormulaR1C1 instead of .value when comparing. .value is the actual value of the cell (like a formula result) where as .formulaR1C1 will give you the actual entered text. so .formulaR1C1 might get you the actual entered 8:15 instead of the corrected value: 08:15.

VBA texttocolumns ignores one column

I have data that was previously improted into Excel and want to have that data automatically put into the proper format.
Right now my dates look like this: 28122012 which should be 28.12.2012
My code, which I've put together with some sources I've found online, works well except for one snag... it ignores column V:
Dim rngS As Range
For Each rngS In .Range("F:F,U:W").Columns
rngS.TextToColumns Destination:=rngS.Cells(1, 1), _
DataType:=xlFixedWidth, FieldInfo:=Array(0, 4)
Next
I'm not certain why it does this. I've experimented by writing out each column, but no go. Column V remains as it is, while everything around it gets properly formatted.
Any ideas why this could be?
Thanks!
The problem is the field it pastes to is treating the value as numerical.
The only real remedy is to set the type to Text for that and other columns that might have such a date value. Then 01122012 will not lose its starting zero.
Alternatively you could let your follow up code or formula anticipate the missing zeroes and work back to date values using LEFT(), RIGHT() and like functions.

Excel - Copy the displayed value not the actual value

I am a pilot, and use a logbook program called Logten Pro. I have the ability to take excel spreadsheets saved from my work flight management software, and import them into Logten Pro using the CSV format.
My problem however, is that the work flight management software, exports the date and time of take-off of a flight into one cell in the following excel format: DD/MM/YYYY H:MM:SS PM.
This is handled fine by Excel, and is formatted by default to DD/MM/YY even though the actual value is more specific, comprising of the full length date and time group.
This is a problem because Logten Pro will only auto-import the date if it is in DD/MM/YY format, and there is no way to pull out just the displayed DD/MM/YY date rather than the full date time group actual value, unless you manually go through and delete the extra text from the function box.
My question is: Is there a VBA macro that can automatically copy the actual displayed text, and paste it into another cell, changing the actual value as it does, to just the DD/MM/YY value? Additionally, can this be made to work down a whole column rather than individual cells at a time?
Note I have no VBA experience so the perfect answer would just be a complete VBA string I could copy and paste.
Thank You.
As pointed out in the comments, you'd better not use VBA but formulas instead.
This formula:
TEXT(A1,"dd-mm-yyy")
will return the formated date in a text way. You can drag and drop the formula in the whole range of your cells and Copy/Paste Special > Values so that you will only have the needed values to get imported in Logten Pro.
There are three options using formulas.
Rounddown
Excel stores the date time as a number and uses formatting to display it as a date.
The format is date.time, where the integer is the date and the fraction is the time.
As an example
01/01/2012 10:30:00 PM is stored as 40909.9375
All the values after the decimal place relate to the hours and minutes
So a formula could be used to round the number down to a whole number.
=ROUNDDOWN(A1,0)
Then format the value as a short date.
It will then display as 01/01/2012
INT
As above, but using a different formula to get rid of the fraction (time)
=INT(A1)
Text
Alternately the date only could be extracted as text using this formula
=TEXT(A1,"dd/mm/yyyy")
It will then display as 01/01/2012
I'm a bit late to the party on this one, but recently came across this as was searching for answers to a similar problem.
Here is the answer I finally came up with.
Option Explicit
Sub ValuesToDisplayValues()
Dim ThisRange As Range, ThisCell As Range
Set ThisRange = Selection
For Each ThisCell In ThisRange
ThisCell.Value = WorksheetFunction.Text(ThisCell.Value, ThisCell.NumberFormat)
Next ThisCell
End Sub
This answers the question as asked, apart from the new values are pasted over the existing ones, not into a new cell, as there is no simple way to know where you would want the new values to be pasted. It will work on the whole range of selected cells, so you can do a whole column if needed.