Convert to number error in excel after copy paste - excel

I am facing issue after copy and paste from export file from SAP it ask to convert to number option which is like error without converting to number formulas do not work. also cell value is number only.
I tried to make macro but it works on single column only. how to make it work on multiple column together and make it faster also as it stuck exel for long time.
code for converting to number
Columns("A:A").Select
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
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
Columns("G:G").Select
Selection.TextToColumns Destination:=Range("G1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
How shorter code and combine for multiple columns in single line.

Use an xlFixedWidth in your Range.TextToColumns method.
Dim c As Long, vCOLs As Variant
vCOLs = Array(1, 6, 7) 'columns A, F and G
With Worksheets("Sheet1")
For c = LBound(vCOLs) To UBound(vCOLs)
With .Column(c)
.TextToColumns Destination:=.Cells(1), DataType:=xlFixedWidth, _
FieldInfo:=Array(0, 1), TrailingMinusNumbers:=True
End With
Next c
End With
The variant array allow you to quickly specify the columns to be processed. Even with a large (~250K) number of rows, this should be fairly quick to cycle through.

Related

I want to eliminate exact range (G1) & make this Text to column code dynamic to convert column values to date

ActiveCell.EntireColumn.Select
Selection.TextToColumns Destination:=Range("G1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 4), TrailingMinusNumbers:=True
Selection.NumberFormat = "dd-mmm-yy"
I found the solution , Thanks for support , Modified code should be
ActiveCell.EntireColumn.Select
Dim myRange As Range
Set myRange = Selection
myRange.TextToColumns Destination:=myRange, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 4), TrailingMinusNumbers:=True
Selection.NumberFormat = "dd-mmm-yy"

Issue converting UK date into text (returns US format)?

I have recorded a macro that will convert a column of dates in UK format to text by using Text to Columns when recording it, but the text it returns is in US format. Text to Columns does the job when its outside of a macro so not sure why it wouldn't work when used in a macro.
Is there a way to have the text in UK format and not have it converted to US format? I believe the below is the relevant part:
Selection.TextToColumns Destination:=Range("E1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 2), TrailingMinusNumbers:=True
Columns("F:F").Select
Selection.TextToColumns Destination:=Range("F1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 2), TrailingMinusNumbers:=True
Columns("A:P").Select
Selection.Copy
Windows("Xero Upload Template V2.xlsx").Activate
Range("A4").Select
ActiveSheet.Paste```
If I understand correctly then your data is in column 5 and 6 which you need to convert. Please try below code.
Dim c As Long
For c = 5 To 6
Columns(c).TextToColumns DataType:=xlDelimited, Tab:=False, Semicolon:=False,
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 5)
Columns(c).NumberFormat = "dd-mm-yyyy"
Next c

Format doesnt change automatically

I want to change format from Date to Number and It doesnt changes. I have to click two times on every cell to change it.
I found a solution that text to column works here. Unfortunatelly the recorded code doesn't work.
Worksheets("Data for LN").Range("i2:i" & datarange).Select
Selection.NumberFormat = "0.00"
Selection.TextToColumns Destination:=Range("I2"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Any ideas how to change it?

VBA TextToColumns - setting destination range from a variabe

Having trouble using TextToColumns with variables.
This works:
IDCol = Rows(8).Find("ID", LookAt:=xlWhole).Column
Columns(IDCol).Select
Selection.TextToColumns Destination:=Range("J1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
This doesn't though, and since I don't always know where the column is going to be. The issue seems to be setting the Destination.
IDCol = Rows(8).Find("ID", LookAt:=xlWhole).Column
Columns(IDCol).Select
Selection.TextToColumns Destination:=Range(Cells(1, IDCol)), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Thanks if anyone can help.
Try this:
Sub test()
Dim id_col As Long
id_col = Rows(8).Find("ID", LookAt:=xlWhole).Column
Dim target_col As Range
If id_col <> vbNullString Then
Set target_col = Columns(id_col)
With target_col
.TextToColumns , DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
End With
End If
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)

Resources