0

I'm trying to import from a .csv to an Excel spreadsheet several (20) columns of data whilst cleaning up the data as it is imported.

I am new to using VBA to do anything in Excel. My coding experience is limited to a little VB from back in college so I have a grasp of the idea. I'm more than willing to invest time and effort, even buying a few books (any recommendations?).

The .csv file looks like this:

Job:JS_010815_HEASB,Version:2.40,Units:USSurveyFeet,,,,,,,,,,,,,,,,,
PS1457,17262086.61,711051.298,509.153,CONTROL POINT,,,,,,,,,,,,,,,
JS2924,17262069.42,711898.13,505.726,CKP,CKP:POINT ID,PS7431,CKP:NOTES,,,,,,,,,,,,
PS7431,17262069.36,711898.141,505.705,CP,CP:STYLE,PRIM. CONTROL,CP:TYPE,60D NAIL,CP:SIZE,,CP:CONDITION,UNDISTURBED,CP:PROTECTION,OTHER (SEE NOTES),CP:NOTES,,,,
CD7,17262018.81,711181.868,508,PI,,,,,,,,,,,,,,,
CD8,17262889.87,711158.429,510,PI,,,,,,,,,,,,,,,
PS2337,17258986.57,711490.088,506.345,PI,,,,,,,,,,,,,,,
CD5,17262001.04,711782.507,500,PI,,,,,,,,,,,,,,,
JS2925,17261586.74,711741.759,502.677,WELD,WELD:TYPE,MAIN LINE,WELD:XRAY#,BML-901,WELD:JOINT# AHEAD,1708,WELD:JNT HD HEAT#,M75460,WELD:JOINT # BEHIND,1709,WELD:JNT BK HEAT#,M75460,WELD:STATION,716+59,WELD:NOTES
JS2926,17261586.56,711746.613,507.221,NG,NG:REMARKS,4.5 COV,,,,,,,,,,,,,
JS2927,17261628.59,711745.877,502.167,WELD,WELD:TYPE,TIE IN,WELD:XRAY#,BTI-028,WELD:JOINT# AHEAD,1724,WELD:JNT HD HEAT#,M75455,WELD:JOINT # BEHIND,1708,WELD:JNT BK HEAT#,M75460,WELD:STATION,717+01,WELD:NOTES
JS2928,17261670.4,711749.899,501.692,WELD,WELD:TYPE,MAIN LINE,WELD:XRAY#,BML-926,WELD:JOINT# AHEAD,1725,WELD:JNT HD HEAT#,M75455,WELD:JOINT # BEHIND,1724,WELD:JNT BK HEAT#,M75455,WELD:STATION,717+43,WELD:NOTES

The two things I need to do are:

  1. Remove the ":" and whatever precedes it in each cell.

  2. (Minor) I would like to take the info which would be in cell A1 i.e. Job:JS_010815_HEASB, and insert it at the end of each row.

2
  • Thanks for the help Tom. I was thinking that the posted info added a lot of length to the post. Looks a lot cleaner now. Commented Feb 1, 2015 at 23:18
  • Do you want to move the entirety of Column A to the end, or just copy what ever is in the cell A1 to the last cell on each row? Commented Feb 2, 2015 at 2:16

1 Answer 1

1

I'd suggest the easiest way to do this is to split it into the individual parts:

  1. Import (not sure if you want to automate it or not, but if you do, I've included instructions.)
  2. Filter the cells, removing the ":"
  3. Copy Cell A1 to end (or move column A, not sure)

Import

Importing is pretty easy, and can be done with something like this:

Sub importCSV(file As String, wsName As String)
    Dim connection As String
    connection = "TEXT;" + file
    With Worksheets(wsName).QueryTables.Add(Connection:=connection, Destination:=Worksheets(wsName).Range("A1"))
        .Name = file
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlOverwriteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        ' If you were using a file with some other type of delimeter, you'd set this to false and then that one to true
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .Refresh BackgroundQuery:=False
    End With
End Sub

Where file is the file (including directory) and wsName is the name of the worksheet you'll be importing to.

Filter

To filter the cells, try this:

Sub filter(wsName As String)
    Dim c As Range
    For Each c In Worksheets(wsName).usedRange.Cells
        c.value = Right(c.value, Len(c.value) - InStr(c.value, ":"))
    Next
End Sub

What this does is it loops through each cell in the usedRange for the given worksheet (where wsName is the name of the worksheet) and sets the object c to it. Then we can simply set c.value to whatever new value we want, in our case, everything to the right of any :.

To do this, we use the Right() function which takes a string and an integer for the length and returns that many characters from the right hand side. To work out how many characters we want, we get the length of the entire string with Len(), and subtract from it the number of characters up to and including the : with InStr().

(InStr() returns the position of the character if it's in there, or 0, which means if the character isn't there, we'll be calling Right() with the length of the string, so it'll just return the full input.)

Copy

Not entirely sure what you were going for here. If you were trying to move the entire column, use:

Sub moveColumnToEnd(wsName As String, colNum As Integer)
    Dim columnCount As Integer
    With Worksheets(wsName)
        columnCount = .UsedRange.Columns.Count

        .Columns(colNum).Cut
        .Columns(columnCount + 1).Insert
    End With
End Sub

Where wsName is the name of the work sheet and colNum is the number of the column you want to move (in your case 1). Hopefully the code itself is pretty self explanatory. If not, just ask.

If you were trying to just copy Cell A1, try this:

Sub copyA1ToEnd(wsName As String)
    Dim columnCount As Integer
    Dim rowCount As Integer
    Dim copyRange As Range
    Dim c As Range

    With Worksheets(wsName)
        columnCount = .UsedRange.Columns.Count
        rowCount = .UsedRange.Rows.Count

        ' What we're doing here is getting the range from the first cell of the last column+1
        '  all the way to the bottom most cell of that column. The Cells() command takes it's
        '  arguments (row, col) not (X, Y) like you'd probably expect.
        Set copyRange = .Range(.Cells(1, columnCount + 1), .Cells(rowCount, columnCount + 1))

        ' Again, like in the filter function, we loop through each cell in the range and set
        '  it to what we want it to be
        For Each c In copyRange.Cells
            c.Value = .Range("A1").Value
        Next
    End With
End Sub

Where wsName is the name of the worksheet. Again, hopefully it's pretty self explanatory.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.