Hi,
I have a macro on a button that writes various different cells from 1 workbook (main) into a csv string in another workbook (csv file).
For every new button click a new row is created in the csv file.
Ex. value1, value2, value3, ...
Now i would like to add this code:
* When button is clicked then
* IF value1 in csv file already exist replace whole csv string with exisiting data from workbook(main)
* IF value1 does nog exist in the csv file then continue the normal code
Here is the original code but i'm completely lost to replace the existing data from the csv.
I have a macro on a button that writes various different cells from 1 workbook (main) into a csv string in another workbook (csv file).
For every new button click a new row is created in the csv file.
Ex. value1, value2, value3, ...
Now i would like to add this code:
* When button is clicked then
* IF value1 in csv file already exist replace whole csv string with exisiting data from workbook(main)
* IF value1 does nog exist in the csv file then continue the normal code
Here is the original code but i'm completely lost to replace the existing data from the csv.
VBA Code:
Sub Verkocht()
Dim inputmsg As Integer
inputmsg = MsgBox("Project verkocht?", vbYesNo, "Double check")
If inputmsg = vbYes Then
Call Append2CSV
Else
End If
End Sub
Sub Append2CSV()
Dim tmpCSV1, tmpCSV2, tmpCSV3, tmpCSV4 As String
Dim f As Integer
Dim savestring, dealnr, plannr, klant, beurs, plaats, jaar, calculator As String
Const CSVFile As String = "Z:\Voorcalculatie.csv"
f = FreeFile
Open CSVFile For Append As #f
tmpCSV1 = Range2CSV(Sheets(1).Range("C5"))
tmpCSV2 = Range2CSV(Sheets(2).Range("C21:D23"))
tmpCSV3 = Range2CSV(Sheets(2).Range("C26:D30"))
tmpCSV4 = Range2CSV(Sheets(2).Range("C33:D38"))
Print #f, tmpCSV1; ","; tmpCSV2; ","; tmpCSV3; ","; tmpCSV4
Close #f
dealnr = Sheets(1).Range("C5")
plannr = Sheets(1).Range("C6")
klant = Sheets(1).Range("C7")
beurs = Sheets(1).Range("F5")
plaats = Sheets(1).Range("F7")
jaar = Sheets(1).Range("I5")
calculator = Sheets(1).Range("C10")
savestring = ThisWorkbook.Path & "\" & dealnr & "_" & plannr & "_" & klant & "_" & beurs & "_" & plaats & "_" & jaar & "_" & calculator & "_Calcul_VERKOCHT"
ActiveWorkbook.SaveAs Filename:=savestring
End Sub
Private Function Range2CSV(ByVal list As Range) As String
Dim tmp As String
Dim r As Range
Dim rowNum As Long
rowNum = list.Cells(0, 1).Row
For Each r In list.Cells
If r.Row <> rowNum Then
rowNum = r.Row
'tmp = Left(tmp, Len(tmp) - 1) & vbCrLf 'remove last comma and start new line
End If
tmp = tmp & r.Value & ","
Next
tmp = Left(tmp, Len(tmp) - 1) 'remove final comma
Range2CSV = tmp
End Function