trying to combine a row into one cell with dates - excel

ok so im trying to create a macro that will take my set cells and insert them into html code...
what i have is cells with my html code and cells with my values that i need... first it ref. the first html J2 and places it in B13 next it ref. my value that i need and places it in C13 and so on... it gets to a file name that is based off of a date so lets say for date is 6/20/2012 it would take that and format the next cell to pull that date but format the text to go to 20120620.mp3... when the ref. code takes that cell it takes the format... but when i run another script to join all the cells together to one it changes the date to the original format of 6/20/2012... i thought maybe it pulling the original formatting but i changed the original format of the first date cell and it still keeps it in 6/20/2012 format.
<div id="messageDate">6/20/2012</div><audio id="audio" preload="none" controls="controls"><source src="sermons_mp3/mp3/6/20/2012" type="audio/mpeg"></audio>
it should look like this
<div id="messageDate">6/20/2012</div><audio id="audio" preload="none" controls="controls"><source src="sermons_mp3/mp3/20120620.mp3" type="audio/mpeg"></audio>
im using this code to combine the cells
Sub JoinText()
myCol = Selection.Columns.Count
For i = 1 To myCol
ActiveCell = ActiveCell.Offset(0, 0) & ActiveCell.Offset(0, i)
ActiveCell.Offset(0, i) = ""
Next i
End Sub
and ive tried this one too
Function ConcatinateAllCellValuesInRange(sourceRange As Excel.Range) As String
Dim finalValue As String
Dim cell As Excel.Range
For Each cell In sourceRange.Cells
finalValue = finalValue + CStr(cell.Value)
Next cell
ConcatinateAllCellValuesInRange = finalValue
End Function
Sub MyMacro()
Range("b14").Select
ActiveCe
ll.FormulaR1C1 = ConcatinateAllCellValuesInRange([b13:r13])
End Sub
and all with the same results... i hope this helps and someone can give me some help on this... i am totally new at VBA and have no idea what im doing... what i have done has been me just playing and getting lucky... lol
thanks

if you concatenate the date from a cell containing date only (e.g. 6/20/2012 as opposed to sermons_mp3/mp3/6/20/2012), you can use following formula to get the src argument:
="sermons_mp3/mp3/" & text(date_value, "yyyymmdd") & ".mp3"

Related

If a cell is formatted as text, the formulas are not calculated

I need to keep the cells formatted as text and at the same time make sure that Excel calculates the formulas within them.
Is there a way to do it?
Embed your excel-formulas in the text-function.
=Text(your function,"#")
more info:
https://support.office.com/en-us/article/text-function-20d5ac4d-7b94-49fd-bb38-93d29371225c
edit: If your formulas are not being evaluated, then there can also be other causes for that than the cell-formatting.
I'm solved it!!!
In a Sheet module:
Option Explicit
Private Busy As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Busy Then
Busy = True
If ActiveSheet.ProtectContents Then ActiveSheet.Protect UserInterfaceOnly:=True
CalculateFormula Target.Row, Target.Column
Busy = False
End If
End Sub
In a standard module:
Option Explicit
Sub CalculateFormula(Row As Long, Column As Long)
Dim Format As String
If Left(Cells(Row, Column).Value, 1) = "=" Then
Format = Cells(Row, Column).NumberFormat
Cells(Row, Column).NumberFormat = "General"
On Error Resume Next
Cells(Row, Column).FormulaLocal = Cells(Row, Column).Value
On Error GoTo 0
Cells(Row, Column).NumberFormat = Format
End If
End Sub
It is unclear if you just need the format of the cell to be text, or if your need the results of a formula in a cell to be converted to a string/text. Excel formulas do no affect cell formatting, nor or are they affected by cell formatting. a perfect example of this is dates. Dates are really an integer counting the number of days since a start point. Day 1 is 1900/01/01 on a pc (On a mac I think its 1905/01/01 but could be wrong). If you enter today's date (2018/09/17) in any default cell, Excel identifies it as a date, changes the formatting of the cell and converts the date to 43360. You will see this if you change the cell format back to general and you will note that the number is right aligned. If you change the format of the cell to text, you will still see 43360 but instead it will be left aligned. More importantly its still a number and you can test it by using ISTEXT(A1) where A1 is the cell in question. It will still return true.
In order to make the contents of a cell a string, you can simply concatenate your formula with "".
=Your_Function&""
=1+1&""
Having said that. If a cell is formatted as a string, the FORMAT of the cell will remain a string despite whatever math calculation goes on inside it. So if you format A2 as text, and then place =1+1 inside it, the result in the cell will be the number 2 and the cell will be formatted as text.

Cell Format of Time being read as General

If I have a cell contents as the text: THYSS
Then I format that cell as a Time and read the format name using:
workSheet.Cells[rowNumber, start.Column].Style.Numberformat.Format
It returns a string "[$-F400]h:mm:ss\ AM/PM"
In the cell the THYSS is displayed.
In the Fx bar THYSS is displayed.
Everything seems reasonable and as expected.
However, If I have a cell contents as: 1.7063
Then I format that cell as a Time and read the format name using:
workSheet.Cells[rowNumber, start.Column].Style.Numberformat.Format
It returns a string "General"
And in the cell 16:57:04 is displayed.
In the Fx bar 01/01/1900 16:57:04 is displayed.
Why is it returning General?!?!?!?!?!?
I am simply trying to ensure that a spreadsheet to be imported has no Formats to any cells, if it does have formatting then I need to inform the user to correct it, I cannot automatically remove formatting for them.
P.S. Incidentally If I now set the format to General the cell contents have now magically become 1.7062962962963
Something like this may explain my comments
Public Function FormatRange(r As Excel.Range) As Variant
Dim x As Variant
On Error GoTo eHandle:
If r.NumberFormat = "m/d/yyyy" Then
x = CDate(r.Text)
Else
End If
Exit Function
eHandle:
FormatRange = CStr(r.Value)
r.Value = FormatRange
r.NumberFormat = "General"
End Function

How would I extract the formatted text from a cell in VBA?

Lets say A1 is a date formatted to "YYMMDD" and I enter 7/7/2014 in the cell.
The cells string changes to "140707" but the function bar still shows up as "7/7/2014"
When I try to write this code:
Dim dateCell As String
dateCell = Cells(1, "A")
dateCell will equal to "7/7/2014" and not "140707"
I want the cell to be formatted that way so that when anyone puts in a date, it'll automatically change it to yymmdd. Now how would I get the text dateCell to equal 140707 and not 7/7/2014?
I would greatly appreciate it!!
Thanks
You can see the three different ways to get at the value in a cell here. Put a date in A1, and format it as you format it.
Public Sub test()
Dim r As Range
Set r = Range("a1")
MsgBox ("Value:[" & r.Value & "] Value2:[" & r.Value2 & "] Text:[" & r.Text & "]")
End Sub
In your case you want the Text.
In VBA, you can declare a variable as type Date instead of String which will solve that part of the problem.
In Excel, you need to specify a custom number format to "yyMMdd". Excel will understand that this cell is a date from the value that is in it and extract the proper year, month and day and display it as you specified.

How can I obtain the displayed value instead of actual value in Excel?

I have a cell containing a date ex. "05/11/09"
It is currently displayed as "11-MAY-09". How do I copy-paste or use VBA to get the string "11-MAY-09" into the cell next to it ( NOT "05/11/09")?
I can't figure it out other than piecing out the date pieces by itself.
Range("B1").Value = Range("A1").Text
Using the cell's .text instead of .value modifier will copy the text formatting instead of the raw date.
Chuck
I believe you can use the TEXT function to format a date value to your liking.
The format string of "dd-mmm-yy" would format "05/11/09" as "11-MAY-09".
Use the Format function.
Format("5/11/2009", "DD-MMM-YY")
This will return:
11-May-09
If case matters:
UCase(Format("5/11/2009", "DD-MMM-YY"))
returns:
11-MAY-09
"The same answer but with a different function (that has worked for me):
Public Function DisplayText(ByVal pRange As Range) As String
DisplayText = pRange.Text
End Function
Just use =DisplayText(A1). If you change the cell format this function will return the displayed text"
cc: alvaroc
How can I get the displayed value of a cell in MS Excel ( for text that was converted to dates)?
Try this:
Sub FormattedText()
Dim r As Range
On Error Resume Next
Set r = Application.InputBox(prompt:="Select cell", Type:=8)
If r.Count <> 1 Or r Is Nothing Then
Exit Sub
End If
On Error GoTo 0
ActiveCell = "'" & r.Text
End Sub
It will put text of a selected cell (prompted) in the active cell.
You should be able to right click on the cell and set the format as General. This will allow you to put something in without it being automatically formatted to something else.
To save yourself from copying and pasting you will also want to start by putting in the date you want and not formatting and then copying.
In VBA you can do this:
Range("B2") = Range("A2")
Range("B2").NumberFormat = "dd-mmm-yyyy hh:mm:ss" 'Date as 10-Jun-2005
If you need to loop it then:
Range("B" & i) = Range("A"& i)
Range("B" & i).NumberFormat = "dd-mmm-yyyy hh:mm:ss" 'Date as 10-Jun-2005
Another way to do it.
Low-tech but very easy way - paste it into Word, then copy back into Excel! Can take a while with a big file, however... buts works great for one-off uses!

Excel VBA Format cell as custom "h"

I have a cell with a time value as 1:23:45 AM and would like to display just the hour value or use it for other calculations. Need to do this using vba. No, I cannot format the column manually.
Thanks,
Sam
To use VBA to format the cell to only display the hour:
With Worksheets("Sheet1").Cells(1, 1)
.NumberFormat = "H"
End With
To read the hour value in VBA (regardless of how the cell is formatted):
Dim hourValue As Integer
With Worksheets("Sheet1").Cells(1, 1)
If (IsDate(Format(.Value, "hh:mm:ss"))) Then
hourValue = Hour(.Value)
Else
// handle the error
End If
End With
If you want to use the displayed value in code without performing the conversion in VBA then use the Text property of the cell (Range) rather than the Value property. You'll need to be sure that the cell is formatted correctly first:
Dim hourValue As Integer
With Worksheets("Sheet1").Cells(1, 1)
If ((.NumberFormat = "H") And _
(IsDate(Format(.Value, "hh:mm:ss")))) Then
hourValue = CInt(.Text)
Else
// handle the error
End If
End With
If you just want to use the hour value in a worksheet formula then use the HOUR() worksheet function - e.g. =HOUR(A1)
Finally, if you cannot format the column manually because the sheet is protected then you won't be able to format it in VBA either
I am not sure whether your cell is a time field or not. If it is, can you do the following?
dim h as integer
h = Hour(Cell(1,1))

Resources