ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   File Append Row by Cell value (https://www.excelbanter.com/excel-programming/337841-file-append-row-cell-value.html)

Djmask[_8_]

File Append Row by Cell value
 

I have a single csv file that has only one row of information. I would
like to append that row to a new file checking first to see if A1 is in
the first row of any previously appened rows. If it is skip appending.

I have the following VBA macro that forum member nikos provided to
another memeber. I tried to modify it to copy a row based on the cell
value A1 but I couldn't get it to work. Is there a way I could declare
vNewline as row 1? I tried vNewLine = Rows("1:1").Value but didnt'
work. Can you append rows and can they be comma seperated? Thanks for
any help.

Sub Append_Line()
Dim vFile As String
Dim vLine As String
Dim vNewLine As String
Dim vVerifyLine As String
Dim vFound As Boolean
vFile = "C:\Records\CustomerDatabase.csv"
vNewLine = Range("A1").Value
vVerifyLine = Range("A1").Value
vFound = False
Open vFile For Input As #1
Do Until EOF(1)
Line Input #1, vLine
If vLine = vNewLine Then
Close #1
Exit Sub
End If
Loop
Close #1
Open vFile For Append As #1
Print #1, vNewLine
Close #1
End Sub


Thanks again

Scott


--
Djmask
------------------------------------------------------------------------
Djmask's Profile: http://www.excelforum.com/member.php...o&userid=24414
View this thread: http://www.excelforum.com/showthread...hreadid=397519


Djmask[_9_]

File Append Row by Cell value
 

Bump thread.


--
Djmask
------------------------------------------------------------------------
Djmask's Profile: http://www.excelforum.com/member.php...o&userid=24414
View this thread: http://www.excelforum.com/showthread...hreadid=397519


Dave Peterson

File Append Row by Cell value
 
So after you do this appending, that CSV file will have more than one line of
data???

And later, you'll want to check all those lines???

if yes to both, then maybe:

Option Explicit
Sub Append_Line()

Dim vFile As String
Dim vLine As String
Dim vNewLine As String
Dim vFound As Boolean

vFile = "C:\Records\CustomerDatabase.csv"

vNewLine = Range("A1").Value

vFound = False
Open vFile For Input As #1
Do Until EOF(1)
Line Input #1, vLine
If vLine = vNewLine Then
vFound = True
Exit Do
End If
Loop
Close #1

If vFound = True Then
'do nothing, it's already in the .csv
Else
Open vFile For Append As #1
Print #1, vNewLine
Close #1
End If
End Sub

Djmask wrote:

I have a single csv file that has only one row of information. I would
like to append that row to a new file checking first to see if A1 is in
the first row of any previously appened rows. If it is skip appending.

I have the following VBA macro that forum member nikos provided to
another memeber. I tried to modify it to copy a row based on the cell
value A1 but I couldn't get it to work. Is there a way I could declare
vNewline as row 1? I tried vNewLine = Rows("1:1").Value but didnt'
work. Can you append rows and can they be comma seperated? Thanks for
any help.

Sub Append_Line()
Dim vFile As String
Dim vLine As String
Dim vNewLine As String
Dim vVerifyLine As String
Dim vFound As Boolean
vFile = "C:\Records\CustomerDatabase.csv"
vNewLine = Range("A1").Value
vVerifyLine = Range("A1").Value
vFound = False
Open vFile For Input As #1
Do Until EOF(1)
Line Input #1, vLine
If vLine = vNewLine Then
Close #1
Exit Sub
End If
Loop
Close #1
Open vFile For Append As #1
Print #1, vNewLine
Close #1
End Sub

Thanks again

Scott

--
Djmask
------------------------------------------------------------------------
Djmask's Profile: http://www.excelforum.com/member.php...o&userid=24414
View this thread: http://www.excelforum.com/showthread...hreadid=397519


--

Dave Peterson

Djmask[_10_]

File Append Row by Cell value
 

Thanks for your help dave!


--
Djmask
------------------------------------------------------------------------
Djmask's Profile: http://www.excelforum.com/member.php...o&userid=24414
View this thread: http://www.excelforum.com/showthread...hreadid=397519



All times are GMT +1. The time now is 07:14 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com