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

Extract values from text file into sharepoint excel sheet(.xlsx) through VBA

0

I have a text file locally saved with data like

Date,RunbookBCompletionTime 

20201123,06:15 AM

20201124,05:49 AM

20201125,05:56 AM

20201126,06:15 AM

20201127,05:44 AM

20201128,05:54 AM

20201129,06:31 AM

Date,ActiveProducts 

20201123,24

20201124,24 

20201125,24

20201126,25

20201127,252

20201128,2

20201128,25

Date,ActiveSKUs  

20201123,137

20201124,136

20201125,137

20201126,137

20201127,137

20201128,13

20201129,136

Date,CompletedOrderCount,TotalDemandSales_TY 

20201123,64,29

20201124,8,300

20201125,5,30

20201126,5,36

20201127,8,110

20201128,3,424

20201129,42,31

20201130,9,7

Date,CompletedOrdersPeakHour,OrderCount 

20201123,10,3

20201124,0,34

20201125,4,37

20201126,0,36

20201127,3,65

20201128,14,43

20201129,1,34

20201130,0,5

Date,PendingOrderCount 

20201123,92

20201124,93

20201125,89

20201126,101

20201127,162

20201128,11

20201129,100

20201130,35

Date,StartedCartsPeakhour,StartedCartsCount 

20201123,7,64

20201124,4,67

20201125,8,58

20201126,9,68

20201127,16,11

20201128,8,835

20201129,1,76

20201130,0,9

And I have an excel file Sample.xlsx in a SharePoint folder.The data in text file should be pasted to Sample.xlsx without dates but only values that matches corresponding heading in text file using macro.

Also, Only first 7 days(eg: 23-29) data must be extracted as you can see there are sections that contain 8 days of data since this is run once in every week.

Answer
Discuss

Discussion

This file looks somewhat similar to one in the forum question "Import certain data from comma seperated text file to excel using macro" posted by Dr Liss recently.
John_Ru (rep: 6142) Nov 30, '20 at 12:44 pm
Add to Discussion

Answers

0
Selected Answer

This code will give some shape to your project. Create a new workbook, paste the code to a standard code module (by default Module1 but I suggest you rename it TeachExcel), save in xlsm format, and run the code.

Sub ReadTXTfile()
    ' 127

    Dim SourceName  As String           ' name of file to be opened
    Dim Source      As Integer          ' ID is used by the computer
    Dim TxtLine     As String           ' one line read from the source
    Dim Txt()       As String           ' copy of Source as array
    Dim n           As Long             ' index to Txt()

    ReDim Txt(1 To 1000)                ' set a number much larger than you need
    SourceName = Application.GetOpenFilename()
    Source = FreeFile()
    Open SourceName For Input As Source
    Do Until EOF(Source)
        Line Input #Source, TxtLine
        n = n + 1
        Txt(n) = TxtLine
    Loop
    Close #Source

    If n Then
        ReDim Preserve Txt(1 To n)

        For n = 1 To UBound(Txt)
            Debug.Print n, Txt(n)
        Next n
    End If
End Sub

The code will ask you to open one of your text files, read its content into an array, close the source file without modifying it in any way, and then print the array. You will see an exact copy of your source file in the Immediate window.

The point is that you now have all of that file in memory to do with as you please. Of course, your intention is to distribute the data to the shared workbook and that thought gives you the structure first mentioned.

  1. The code must be in a location other than the destination workbook.
  2. It must open the text file and retrieve the data from it.
  3. It must then open the destination workbook and write the data to it.

You can build all of this around and into the above procedure. Please don't overload this forum with requests that amount to finish the coding for you. You absolutely need to take ownership yourself and the only way to do that is to build everything with your own hands. So, divide the tasks into bite-sized questions and ask away. Then put the answers to work in and for your project.

One subject that shouldn't be brought up too late is access to the share. To be precise, what happens if another user accesses - and possibly modifies - the target workbook while your code is running? To write the data will take less than a second but if you have no provision for the unlikely occurrence just described it may bring your entire project down when it happens.

Discuss

Discussion

Good answer Variatus. On the last point, the SharePoint CheckOut/ CheckIn regime can help to rprevent that. Guess that can be done in VBA when the scheduled upload runs.
John_Ru (rep: 6142) Dec 1, '20 at 6:57 am
Thanks Variatus for your input and for the code snippet to start off with.
Cartus (rep: 2) Dec 1, '20 at 9:28 am
If my code helped youi please mark the answer as Selected. I need the reputation points that action entails. John has been starving me recently :-)
Variatus (rep: 4889) Dec 1, '20 at 6:58 pm
Cartus, please mark the Answer as Selected if it helped.

@Variatus- sorry for "starving" you! Your reputation, like you knowledge, is immense but I've been enjoying getting back into Excel after a year of doing little with it. 
John_Ru (rep: 6142) Dec 2, '20 at 1:41 am
Keep at it John. Competition is good for the economy lol: And, I've been lonely for too long. I do enjoy your company.
Variatus (rep: 4889) Dec 2, '20 at 3:09 am
Will do! 
John_Ru (rep: 6142) Dec 2, '20 at 4:32 am
Add to Discussion


Answer the Question

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