I have tried creating a macro that automatically formats a column value to change date and time just to date, however when the macro is run the cell values dont automatically update.
I currently need to make the following function selection happen using VBA for excel 2010.
Data/Text to Columns/Next button (keep default "Delimited")/Next button (keep default "tab")/Next button (keep default "General"/Finish button
I have tried recording functions but the VBA doesn't work. HELPPPPPPPPPPPPPPPPPPPPP!!!!!
Here is my code so far.
' Autochange_Extractdata_Date_Format Macro
'
Sheets("Data Extract").Select
Columns("AK:AK").Select
Selection.NumberFormat = "m/d/yyyy"
Columns("AK:AK").Select
Selection.TextToColumns Destination:=Range("AK1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
End Sub
From the symptoms you described it sounds like your field is initially in TEXT format, that's why changing it to number format doesn't do anything. When you click into the cell it automatically converts it to a number.
Try this
Columns("AK:AK").NumberFormat = "m/d/yyyy"
Columns("AK:AK").Value = Columns("AK:AK").Value '//This will convert each properly
Columns("AK:AK").Select
Selection.TextToColumns Destination:=Range("AK1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Because i couldnt work out how to update the cell value i managed to work this code to change the cell value instead from Date time just to Date.
works a treat!!!
' Autochange_Extractdata_Date_Format Macro
Sheets("Data Extract").Select
Columns("AK:AK").Select
Selection.NumberFormat = "m/d/yyyy"
Range("AK4:AK999999").Select
Dim c As Range
For Each c In Selection
Dim v As String
v = c.value
If c.value <> Empty Then
c.value = DateValue(c.value)
Else
Exit For
End If
Next
Range("A1").Select
End Sub
Related
I recorded a macro that is intended to autofill column B with a comma that coincides with the list of data in column A. The list in column A changes daily. Sometimes it ends at A24, sometimes A10, etc... I am wanting to run the macro and the data in column B autofill to end on the same last row as column A. Any suggestions on how I can tweak my code?
Below is the code I have :
Sub List()
Columns("A:A").Select
Application.CutCopyMode = False
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Range("B1").Select
ActiveCell.FormulaR1C1 = ","
Selection.AutoFill Destination:=Range("B1:B20")
Range("B1:B20").Select
End Sub
I want to create a vba macro that transforms text to columns, but this command is only capable of doing column by column.
Columns("F:F").Select
Selection.TextToColumns Destination:=Range("F1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
This code above works whell for one column, but i would like to do the command for the columns F to BL.
How can i do a For Loop to iterate over all the columns between F and BL?
Some sort of For Columns in Range Do?
Something like this should give you an idea of what you can do.
Note that you don't need to select. And most of those option are probably set to their default value, making the code unecessary verbose. The macro recorder is nice but you might want to rework the result.
Sub quick_and_dirty()
Dim cell As Range
For Each cell In Range("F1:G1")
cell.EntireColumn.TextToColumns Destination:=cell, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Next cell
End Sub
You are going to run into an issue with regard to overwriting existing data, so the route you want to follow is dependent on how you want to address that.
If you use AugustinLopez' method above, you'd want to make a handful of adjustments either way.
If you want to copy each row to a new sheet and then expand it, just add a "cell.EntireColumn copy xxx" step before the TextToColumns step and copy it to a new range.
If you want to add the necessary number of empty columns first, and presuming you have a variable number of commas through out the columns, it gets trickier and you'd need something like this (depending on your table size, some optimization for speed may be needed):
Sub DummyCode()
Dim cell As Range
Dim ColumnCount As Integer
Dim RangeOfInterest As Range
Dim CellStart As Integer
Dim i As Integer
Dim j As Integer
Dim CommaCount As Integer
Set RangeOfInterest = Range("F1:BL1")
ColumnCount = RangeOfInterest.Columns.Count
CellStart = RangeOfInterest.Range("A1").Column
For i = CellStart + ColumnCount - 1 To CellStart Step -1 'you gotta go backwards to not mess up your count
CommaCount = 0
For Each cell In Intersect(Cells(1, i).EntireColumn, ActiveSheet.UsedRange)
If InStr(1, cell.Value, ",") > 0 Then
If CommaCount < (Len(cell.Value) - Len(Replace(cell.Value, ",", ""))) Then
CommaCount = (Len(cell.Value) - Len(Replace(cell.Value, ",", "")))
'credit for the above bit of cleverness goes to here:
'http://www.ozgrid.com/forum/showthread.php?t=45651
'via here
'https://stackoverflow.com/questions/9260982/how-to-find-number-of-occurences-of-slash-from-a-strings
End If
End If
For j = 1 To CommaCount
Cells(1, i).EntireColumn.Insert
Next j
Cells(1, i).EntireColumn.TextToColumns Destination:=Cells(1, i), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Next i
End Sub
good luck!
I am currently making a report based off of Stats in my company. The raw data that I receive has date with a Time Stamp (2 Date Columns - Created/Received). To Remove the time stamp, I set the a custom format to m/d/yyyy and then Delimit each column separately which gives me the correct date. Once I assign my Macro to a button and add new Raw Data I receive this error ----
Does anyone have any tips or understand why I am getting this error even though I am only Delimiting one column at a time?
Sub ComplianceOTRSMacro()
' ComplianceOTRSMacro Macro
' Format Date - Refresh Pivot Tables
Columns("D:E").Select
Selection.NumberFormat = "m/d/yyyy"
Columns("D:D").Select
Selection.TextToColumns Destination:=Range("OTRSData[[#Headers],[Created]]") _
, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, Comma:=False _
, Space:=False, Other:=False, FieldInfo:=Array(1, 1), _
TrailingMinusNumbers:=True
Columns("E:E").Select
Selection.TextToColumns Destination:=Range( _
"OTRSData[[#Headers],[Close Time]]"), DataType:=xlDelimited, TextQualifier _
:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:= _
False, Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1) _
, TrailingMinusNumbers:=True
Range("OTRSData[[#Headers],[Ticket'#]]").Select
Sheets("Dashboard").Select
ActiveWorkbook.RefreshAll
End Sub
I would like to format an Excel file to "General", but it's not working for all cells (some are custom, percentage...).
Here is my code:
With ActiveSheet.Range("A1").Resize(LineIndex, 1)
.Value = WorksheetFunction.Transpose(strLine)
.NumberFormat = "General"
'DEFINE THE OPERATION FULLY!!!!
.TextToColumns Destination:=.Cells(1), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, Comma:=False, Space:=False, _
Other:=True, OtherChar:="|"
End With
If you want to format all cells to General, then use something like:
Sub dural()
ActiveSheet.Cells.NumberFormat = "General"
End Sub
I am trying to perform the "Text to Columns" feature on several columns of data in Excel. How can I modify the VBA script of my macro to do this? Right now, I can only select one column for my macro, but I'd like to select multiple columns, and have this loop through somehow.
Also, is there any way to write script that clicks "okay" for me, when it asks if I can overwrite the data in the next column?
Sub text2col()
'
' text2col Macro
'
' Keyboard Shortcut: Option+Cmd+k
'
Selection.TextToColumns Destination:=Range(ActiveCell.Address), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="-", FieldInfo:=Array(Array(1, 1), Array(2, 1))
End Sub
change 1 to the column number of the 1st column you need to change
change 10 to the column number of the last column you need to change
columns are numbered from the left (A) starting with 1
Make sure you pay attention to Mark Wickett's warning in his comment.
Sub text2col()
'
' text2col Macro
'
' Keyboard Shortcut: Option+Cmd+k
'
Dim Col as Integer
Application.DisplayAlerts = False
For Col = 1 to 10
Selection.TextToColumns Destination:=Range(ActiveCell.Address), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="-", FieldInfo:=Array(Array(1, 1), Array(2, 1))
Next
Application.DisplayAlerts = True
End Sub