Adding data to the end of existing data.

SteynBS

Board Regular
Joined
Jun 27, 2022
Messages
104
Office Version
  1. 365
Platform
  1. Windows
Hi Guys,

So I use VBA to add the file names in a folder to excel. Everything works fine, but now I added a folder with other files. I now need the other folder file names to be added to the end of my existing data.

This is the VBA I currently use.

Sub ListAllFileNames1()

Dim strTargetFolder As String, strFileName As String, nCountItem As Integer

' Initialization
nCountItem = 1
strTargetFolder = "Z:\TC Reporting & Certification\Certificates Consolidated\House Brands Foods" & "\"
strFileName = Dir(strTargetFolder, vbDirectory)

' Get the file name
Do While strFileName <> ""
If strFileName <> "." And strFileName <> ".." Then
Cells(nCountItem + 3, 2) = strFileName
nCountItem = nCountItem + 3
End If
strFileName = Dir
Loop

End Sub

this is the other folder directory "Z:\TC Reporting & Certification\Certificates Consolidated\Appliances Medical Devices Cosmetics" & "\"

So basically I need the file names to be combined. The one following after the other.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I haven't been able to test this but give this a try:

VBA Code:
Sub ListAllFileNames1()

Dim strTargetFolder As String, strFileName As String, nCountItem As Integer
Dim i As Long, arrFolders As Variant

' Initialization
nCountItem = 1

arrFolders = Array("Z:\TC Reporting & Certification\Certificates Consolidated\House Brands Foods" & "\", _
                    "Z:\TC Reporting & Certification\Certificates Consolidated\Appliances Medical Devices Cosmetics" & "\")

For i = LBound(arrFolders) To UBound(arrFolders)
    strTargetFolder = arrFolders(i)
    strFileName = Dir(strTargetFolder, vbDirectory)
    
    ' Get the file name
    Do While strFileName <> ""
        If strFileName <> "." And strFileName <> ".." Then
            Cells(nCountItem + 3, 2) = strFileName
            nCountItem = nCountItem + 3
        End If
        strFileName = Dir
    Loop

Next i

End Sub
 
Upvote 0
I haven't been able to test this but give this a try:

VBA Code:
Sub ListAllFileNames1()

Dim strTargetFolder As String, strFileName As String, nCountItem As Integer
Dim i As Long, arrFolders As Variant

' Initialization
nCountItem = 1

arrFolders = Array("Z:\TC Reporting & Certification\Certificates Consolidated\House Brands Foods" & "\", _
                    "Z:\TC Reporting & Certification\Certificates Consolidated\Appliances Medical Devices Cosmetics" & "\")

For i = LBound(arrFolders) To UBound(arrFolders)
    strTargetFolder = arrFolders(i)
    strFileName = Dir(strTargetFolder, vbDirectory)
   
    ' Get the file name
    Do While strFileName <> ""
        If strFileName <> "." And strFileName <> ".." Then
            Cells(nCountItem + 3, 2) = strFileName
            nCountItem = nCountItem + 3
        End If
        strFileName = Dir
    Loop

Next i

End Sub
I haven't been able to test this but give this a try:

VBA Code:
Sub ListAllFileNames1()

Dim strTargetFolder As String, strFileName As String, nCountItem As Integer
Dim i As Long, arrFolders As Variant

' Initialization
nCountItem = 1

arrFolders = Array("Z:\TC Reporting & Certification\Certificates Consolidated\House Brands Foods" & "\", _
                    "Z:\TC Reporting & Certification\Certificates Consolidated\Appliances Medical Devices Cosmetics" & "\")

For i = LBound(arrFolders) To UBound(arrFolders)
    strTargetFolder = arrFolders(i)
    strFileName = Dir(strTargetFolder, vbDirectory)
   
    ' Get the file name
    Do While strFileName <> ""
        If strFileName <> "." And strFileName <> ".." Then
            Cells(nCountItem + 3, 2) = strFileName
            nCountItem = nCountItem + 3
        End If
        strFileName = Dir
    Loop

Next i

End Sub

@Alex Blakenburg, you are a genius. It works perfectly, thank you

 
Upvote 0
Thanks for letting me know. Glad I could help.
@Alex Blakenburg just for interest sake. in those folders, there are not only file names, there are other folders in it as well. So this formula copies all the file names in that folder including the folder names. Is there any way to avoid that or should I just AutoFilter the folder names and delete it afterward?
 
Upvote 0
Yes i
Can you try it without the vbDirectory first.
Yes, indeed it works. Thank you so much. Can't thank you enough.

I have posted this question, not received any feedback as yet. But, what do you know about importing/copying data from a text document pasting into excel, but its done with multiple text documents with the same name all the ext documents data should be pasted in excel but it should not override the existing data. it should keep adding to the data. If you can assist me, I will send you more info.
 
Upvote 0
Send me the link. I can take a look. No promises though ;)

Sub ImportTextFileToExcel()
Dim textFileNum, rowNum, colNum As Integer
Dim textFileLocation, textDelimiter, textData As String
Dim tArray() As String
Dim sArray() As String
textFileLocation = "C:\Users\200327\Desktop\CIPHER.OUT.txt"
textDelimiter = "|"
textFileNum = FreeFile
Open textFileLocation For Input As textFileNum
textData = Input(LOF(textFileNum), textFileNum)
Close textFileNum
tArray() = Split(textData, vbLf)
For rowNum = LBound(tArray) To UBound(tArray) - 1
If Len(Trim(tArray(rowNum))) <> 0 Then
sArray = Split(tArray(rowNum), textDelimiter)
For colNum = LBound(sArray) To UBound(sArray)
ActiveSheet.Cells(rowNum + 2, colNum + 1) = sArray(colNum)
Next colNum
End If
Next rowNum

End Sub

I need to somehow change (in red) so the data is added at the bottom of the existing data something like this - Range("A" & Rows.Count).End(xlUp).Offset(1)
 
Upvote 0
I have responded to the new topic on the new thread. You might want to close this one out, assuming that you have a solution that works for you,
 
Upvote 0
Solution

Forum statistics

Threads
1,215,208
Messages
6,123,644
Members
449,111
Latest member
ghennedy

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