Add line to CSV file based on userform input?

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
788
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have a userform with 2 inputs (ID and Name)

When a submit button is pressed I'm wanting this data to be inserted at the last row of column A in CSV file.
So ID in A, Name in B

However if ID inputted already exists it should throw an error and not add it.

Appreciate any help
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I do not think that excel can check a CSV file for a particular record.

what I would do is
1. import the csv into excel sheet.
2. open user form and add records as needed while checking for existing ID.
3. when finished. close excel and create a new csv file.

hth,

Ross
 
Upvote 0
Can you not declare CSV as workbook?
Like
Code:
WB2 = Workbooks.open Filename:="c:\temp\data.csv"

Then do a search in WB2.Sheets("Sheet1") for the ID input
 
Upvote 0
ye this works:

Code:
Private Sub CommandButton1_Click()


Dim File1 As String
Dim WB2 As Workbook


File1 = "c:\temp\data.CSV"
Set WB2 = Workbooks.Open(File1)


UserName = TextBox1.Value
FindString = TextBox2.Value
    
With WB2.Sheets("Sheet1").Range("A:A")
                Set Rng = .Find(What:=FindString, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                If Not Rng Is Nothing Then
                  MsgBox (FindString & " " & "Already Exists")
                Else
                    WB2.Sheets("Sheet1").Range("a1000").End(xlUp).Offset(1, 0).Value = FindString
                    WB2.Sheets("Sheet1").Range("b1000").End(xlUp).Offset(1, 0).Value = UserName
                    WB2.Close savechanges:=True
                End If
            End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top