Hey everyone!
I am using a bit of VBA code to help me re-order the columns of my data (code below). However some of the data that is pure numbers (eg.1) is getting formatted into a date: 1/1/1900 0:00 . I am not sure if this is being caused by data the previously occupied those columns, eg the Time_stamp field, or if it is just a weird fluke in excel.
The macro first opens a set of data (csv), and copies it to a new workbook. It then sorts the columns. I am wondering if there is coding where by I can have it autoformat those fields to text only prior to copying and if that will fix the solution?
I am using a bit of VBA code to help me re-order the columns of my data (code below). However some of the data that is pure numbers (eg.1) is getting formatted into a date: 1/1/1900 0:00 . I am not sure if this is being caused by data the previously occupied those columns, eg the Time_stamp field, or if it is just a weird fluke in excel.
The macro first opens a set of data (csv), and copies it to a new workbook. It then sorts the columns. I am wondering if there is coding where by I can have it autoformat those fields to text only prior to copying and if that will fix the solution?
Code:
Sub Edit()
Call OpenBook
Call Order
End Sub
'
'
'
'
Sub OpenBook()
'
Workbooks.Add
Workbooks.Open Filename:=ThisWorkbook.Path & "\Data.csv"
ActiveSheet.UsedRange.Copy
Windows("Book1").Activate
ActiveSheet.Paste
'
End Sub
Sub Order()
'
'
' This Macro places the columns in the correct order
'
Dim rng As Range
Dim i As Integer
Dim J As Integer
Dim Temp
Dim nams As Variant
Dim F
Dim Dex As Integernams = Array("ItemID", "FirstName", "LastName", "Address", "ValueA11", "ValueB11", "ValueC11", "ValueD11", "ValueE11", "ValueA21", "ValueB21", "ValueC21", "ValueD21", "ValueE21", "ValueA31", "ValueB31", "ValueC31", "ValueD31", "ValueE31", "ValueA41", "ValueB41", "ValueC41", "ValueD41", "ValueE41", "ValueA51", "ValueB51", "ValueC51", "ValueD51", "ValueE51", "Time_Stamp", "Week", "Month", "Year")
Set rng = Range("A1").CurrentRegion
For i = 1 To rng.Columns.Count
For J = i To rng.Columns.Count
For F = 0 To UBound(nams)
If nams(F) = rng(J) Then Dex = F: Exit For
Next F
If F < i Then
Temp = rng.Columns(i).Value
rng(i).Resize(rng.Rows.Count) = rng.Columns(J).Value
rng(J).Resize(rng.Rows.Count) = Temp
End If
Next J
Next i
End Sub
Last edited: