0

I need to create a template (MS Excel) for importing CSV data (error log) in Excel.

  1. I have set the conditional formatting that if a call value is greater than 0, the cell color must automatically change to RED while for value=0, the cell color must be green.

  2. I saved the file as Excel template.

  3. Now I want to import CSV data into a new XLSX document created from the template above (with conditional formatting), in batch-mode, and safe the result automatically.

I am using following command:

"C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE" /e "C:\Work\errors.log" /t "C:\Work\ABFTest.xltx"

Instead of loading the data into a new Excel document created from ABFTest.xltx template, the command open two Excel files, one containing the CSV data, and other file created from the template.

Is there any solution to this problem?

Mubeen Shahid
  • 316
  • 6
  • 18
  • 1
    @pnuts Thanks for the comment, yes I have done that, and have accordingly modified the question (by removing that part of question). – Mubeen Shahid Oct 27 '14 at 15:34

1 Answers1

1

@Mubeen Shahid I wrote the code snippet below based on your original request so it will not now fully meet your new conditions. If it is of no use then no matter just discard it.

The code will read a .csv file and format it in a single column in sheet 1. For this example the .csv file is called "NosToCol.csv" and you must supply your own path where shown thus <>. Parameters can be changed within the code snippet to suit.

Sub ReadCSVFile()

Dim ws As Worksheet
Dim fName As String, Txt1 As String, tmpvar As String
Dim fRow As Long, lRow As Long, Rw As Long
Dim Col As Long, rec As Long
Dim wrng As Range, cl As Range
Dim ifnum As Integer
Dim rearr(), wrarr

Set ws = Sheets("Sheet1")
fName = "<<yourpath>>\NosToCol.csv"
fRow = 2  'Row 2
Col = 1   'Col A
Txt1 = ""
ifnum = 1

    With ws
        lRow = .Cells(Rows.Count, Col).End(xlUp).Row

        'READ DATA FROM FILE
        Open fName For Input Access Read As #ifnum
        rec = 0
            Do While Not EOF(ifnum)
                Line Input #ifnum, tmpvar
                rec = rec + 1
                ReDim Preserve rearr(1 To rec)
                rearr(rec) = tmpvar
            Loop
            Close #ifnum

         'WRITE DATA TO RANGE
            For c = 1 To rec
                wrarr = Split(rearr(c), ",")
                Set wrng = .Range(.Cells(fRow, Col), .Cells(fRow + UBound(wrarr, 1), Col))
                '.Range(.Cells(fRow, Col), .Cells(fRow + UBound(wrarr, 1), Col)).Value = Application.Transpose(wrarr)
                wrng.Value = Application.Transpose(wrarr)
                c = c + 1
                    'MODIFY CELL COLOUR
                    For Each cl In wrng
                        If cl = 0 Then cl.Interior.Color = vbRed Else cl.Interior.Color = vbGreen
                    Next cl
            Next c
    End With
End Sub
barryleajo
  • 1,956
  • 2
  • 12
  • 13
  • Tons of thanks for valuable reply. I copied and modified this code. How can I compile+execute this code? I have Visual Studio, but while trying to get an exe file from this code, I got a plethora of errors :( which I believe is due to lack of experience on my side :( – Mubeen Shahid Oct 28 '14 at 11:40
  • 1
    This is written in VBA and not VB.Net. It will run from within an Excel workbook itself. In Excel select the Developer Tab and click the Visual Basic button. This will open the IDE. From the Insert menu in the IDE select Module to add a new general code module. Cut and paste this code into that module and run it by clicking the small green triangle on the IDE toolbar menu. – barryleajo Oct 28 '14 at 11:54
  • Again, a million thanks for this valuable advice! I am trying to understand the working principle, and will extend/modify it to create conditional formatting for multiple columns (if possible). Is it possible, anyway, to run this code in batch mode (i.e. specifying the VBA code file and CSV in *.bat script, running the *.bat script and saving result somewhere) ? – Mubeen Shahid Oct 28 '14 at 15:16
  • I think you would need to convert this code into a .net language to achieve this. Did you run it within Excel and did it work for you? – barryleajo Oct 28 '14 at 19:10
  • Yes, this code works within Excel. I tried to use another [VBS code](http://stackoverflow.com/a/2056066/1916047) for running your VBA macro using Excel in batch mode, but the batch mode VBS does not work. Perhaps the following line in the VBS is not working? `Set xlApp = CreateObject("Excel.Application")` – Mubeen Shahid Oct 29 '14 at 09:21
  • 1
    Each language will have its differences so conversion using the algorithm of this code seems the way forward. I think you need to decide if this answer meets your needs within the scope of your original Q and perhaps start another post with your follow-on queries. This should ensure you attract the appropriate expertise to help you. – barryleajo Oct 29 '14 at 09:22