VBA format cells to General - excel

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

Related

Delimit Date to remove Time stamp

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

Macro for text-to-columns

I'm trying to write a code to do the following:
Using text-to-columns,the data should get divided in different columns.
The data in Cells A1-A8 is like this:
This data should appear in different columns.
Like this?
Sub Macro()
Selection.TextToColumns Destination:=Range("A1"), 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
You can build the FieldInfo parameter dynamically by building out the string. In the example below, there are 10 resultant columns. The For loop builds the sFieldInfo variable, and specifies all the columns in Text format.
sComma = ""
sFieldInfo = ""
For x = 1 To 10
sFieldInfo = sFieldInfo & sComma & " Array(" & x & ", xlTextFormat)"
sComma = ","
Next x
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.TextToColumns Destination:=Range("J5"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar:="/", _
TrailingMinusNumbers:=True, _
FieldInfo:=Array(sFieldInfo)

Loop Text to Columns macro in Excel for multiple selected columns

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

Make Text to columns refresh happen using VBA excel 2010

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

Excel macro loop code for more than one sheet

I have code for delimited columns for one sheet. How do I make this code for more than sheets with looping?
Sub Macro1()
Selection.TextToColumns Destination:=ActiveCell, 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)), TrailingMinusNumbers:=True
ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
Selection.NumberFormat = "dd-mm-yyyy;#"
End Sub
Sub t()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Sheets
'do what you want, for example,
Range("A1:B10").TextToColumns Destination:=ActiveCell, 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)), TrailingMinusNumbers:=True
ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
Selection.NumberFormat = "dd-mm-yyyy;#"
Next
End Sub
However, you should not use Selection. You should use a range instead, for e.g. Range("A1:B10")
Try it like this
Sub forEachWs()
Dim x As Long
Dim ws As Sheets
For x = 1 To 12'<--to as how many is required
ThisWorkbook.Sheets(x).Activate
Call Macro1 '<---the name of your macro
Range("A2").Select
End Sub

Resources