Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Read .csv data without opening excel & Compare

0

Hi,

I am having problem with the below condition

Vba Code will be placed in macro.xlsm

Vba Code will not Open co.csv in Excel

Hardcode the  path of co.csv in the code 
BasketOrder.csv is Already Opened in Excel
If co.csv has rejected in Status And co.csv symbol = coloumn C of basketorder.csv And co.csv Buy sell = coloumn J of basketorder.csv Then 
Copy L data Of basketOrder.csv to coloumn G of basketorder.csv
And In Coloumn K of basketorder.csv put SL 
And In Coloumn T of basketorder.csv put NA
Else delete the entire Row Data

End If

Above is the condition & i am unable to solve this Problem,So plz Help

Thnx For the Help

plz download the file from below link

https://drive.google.com/file/d/1mv--BJJx8-yJA3rFenmanoXmp2l804Hx/view?usp=sharing

https://drive.google.com/file/d/19PyMh5d7yWacuj4jnDpNeBwnJikjz89m/view?usp=sharing

Answer
Discuss

Discussion

Hi Style

Please edit your question and attach your files (so people don't have to folllw links which may be deleted in future or seem suspicious) 
John_Ru (rep: 6142) Jan 31, '23 at 1:39 am
Hi,
I tried to.upload the file 5-6 times but it was not uploaded & I don't know why
style36 (rep: 24) Jan 31, '23 at 1:41 am
One user had a similar problem but another succeeeded. Try a short file name perhaps. 
John_Ru (rep: 6142) Jan 31, '23 at 2:47 am
File is .csv file It takes .csv file?
style36 (rep: 24) Jan 31, '23 at 2:54 am
No, only Excel. You could embed your files in one... 
John_Ru (rep: 6142) Jan 31, '23 at 3:20 am
Means?
style36 (rep: 24) Jan 31, '23 at 6:04 am
Create a new Excel file, embed your. csv files (as icons), save Excel then upload that
John_Ru (rep: 6142) Jan 31, '23 at 6:09 am
I tried but excel got closed
style36 (rep: 24) Jan 31, '23 at 9:41 am
Did you try my Answer /file? 
John_Ru (rep: 6142) Feb 5, '23 at 10:20 am
Style. Did you get notified of my Answer below (or did I just waste my time creating that)?
John_Ru (rep: 6142) Mar 1, '23 at 7:19 am
Add to Discussion

Answers

0

Style

Your requirement to not open the csv in Excel made this more complex than necessary perhaps but the attached file does what your question asks for, provided you follow the instructions in the file, storing and opening one of the .csv files (included in the Excel file for the benefit of other users).

Ensure that you have the Microsoft Scripting Runtime library installed (so the co.csv can be read) and that you have replaced the bold bit in the line:

CoCSVLoc = "C:\Users\John\Downloads\co.csv"

with the full path to the stored .csv file.

Then, with the other file BasketOrder.csv open in Excel, when the green button (labelled "Check Status in co.csv") is clicked, the macro below runs (with comments for your guidance):

Private Sub CompareCsv()

' NOTE: needs Microsoft Scripting Runtime enabled (via Tools/Referemces)
Dim FSO As Object, CoCVS As TextStream, CoTest As String
Dim CoCSVLoc As String, CoLine As String, CoLineNo As Long, CoArray() As String
Dim n As Long, m As Long, CoRej As Boolean


 'state where the source .csv file is located and what will be tested
CoCSVLoc = "C:\Users\John\Downloads\co.csv"
CoTest = "rejected"

Set FSO = CreateObject("Scripting.FilesystemObject")
Set CoCVS = FSO.OpenTextFile(CoCSVLoc)

' gather the data from the source .csv file
With CoCVS
    While Not .AtEndOfStream  ' read the csv file line by line
        CoLine = .ReadLine ' get next line
         'apart from first (title) line
        If CoLineNo <> 0 Then
            ReDim Preserve CoArray(1 To 2, 1 To CoLineNo)   ' resize ("horizontal") array
            CoArray(1, CoLineNo) = Left(CoLine, InStr(2, CoLine, ",") - 1) ' put Symbol in array
            CoArray(2, CoLineNo) = Right(CoLine, Len(CoLine) - InStrRev(CoLine, ",")) '' put Status in array
        End If
        CoLineNo = CoLineNo + 1 ' increment line number
    Wend
    .Close
End With

' check against open .csv file rows
With Workbooks("BasketOrder.csv").Sheets(1)
   'loop backwards from last used row in column C
   For n = .Cells(.Rows.Count, "C").End(xlUp).Row To 1 Step -1
        'loop through array to check
        CoRej = False ' set flag
        For m = LBound(CoArray, 2) To UBound(CoArray, 2)
            ' set variable if match on both Symbol and test value
                If CoArray(1, m) = .Cells(n, 3).Value And CoArray(2, m) = CoTest Then CoRej = True
        Next m

        If CoRej = True Then ' check flag
            'found so make replacements
            .Cells(n, "G").Value = .Cells(n, "L").Value
            .Cells(n, "K").Value = "SL"
            .Cells(n, "T").Value = "NA"
            Else
            .Rows(n).EntireRow.Delete 'otherwise remove row
        End If
   Next n
End With

' tell user
MsgBox "Done! (" & Now & ")"

End Sub

Note that I declared several variables which hopefully make sense. Also that I run backwards through BasketOrder.csv since some rows are deleted (and would mess up a forward running counter).

Hope this fixes your problem. If so, please  remember to mark this Answer as Sdelected.

Discuss

Discussion

What's your response please? 
John_Ru (rep: 6142) Feb 16, '23 at 3:49 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login