Consulting

Results 1 to 7 of 7

Thread: Copy paste

  1. #1

    Copy paste

    Macro will be placed in school.xlsm, my all file are in same place, Open the file Completely by macro Copy the first sheet data of anu.csv(here sheet name can be anything) and paste it to sheet1 of anu1.xlsx(here sheet name can be anything, and also look if there is data in first sheet of anu1.xlsx if yes then paste the data below that below the existing data example- if it has data till A12 then start pasting the copied data from A13 or u can consider row also if it has data till row 12 then start pasting data from row 13 and if not then paste the data from starting )and save the changes made by this process by vba
    Last edited by kaja; 09-01-2019 at 09:10 PM.

  2. #2
    Perhaps something like

    Sub Macro1()
    
    Dim oWB As Workbook
    Dim oSheet As Worksheet
    Dim FSO As Object, MyFile As Object
    Dim FileName As String
    Dim Arr As Variant, vRow As Variant
    Dim NextRow As Long, lngRow As Long, lngCol As Long
        Set oWB = ActiveWorkbook
        Set oSheet = oWB.Sheets(1)
        NextRow = oSheet.UsedRange.Rows(oSheet.UsedRange.Rows.Count).Row + 1
        FileName = oWB.path & "\anu.csv"
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Set MyFile = FSO.OpenTextFile(FileName, 1)
        Arr = Split(MyFile.ReadAll, vbNewLine)
        For lngRow = 1 To UBound(Arr)
            vRow = Split(Arr(lngRow), ",")
            For lngCol = 0 To UBound(vRow)
                oSheet.Cells(NextRow, lngCol + 1) = vRow(lngCol)
            Next lngCol
            NextRow = NextRow + 1
        Next lngRow
        oWB.Save
        Set FSO = Nothing
        Set oSheet = Nothing
        Set MyFile = Nothing
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Sir plz have a relook to this code and my first post
    we have to paste the data to a file name anu1.xlsx
    Plz relook my first post and this code Sir
    Only macro containing file is opened no other file is opened we have to open it vba

  4. #4
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Try this.

    Sub test()
        Dim p As String
    
    
        p = ThisWorkbook.Path & "\anu.csv"
        
        With Workbooks.Open(ThisWorkbook.Path & "\anu1.xlsx").Sheets(1)
            With .QueryTables.Add("TEXT;" & p, .Cells(Rows.Count, 1).End(xlUp).Offset(1))
                .TextFileParseType = xlDelimited
                .TextFileTabDelimiter = True
                .TextFileCommaDelimiter = True
                .Refresh BackgroundQuery:=False
                .Delete
            End With
            .Parent.Close True
        End With
    
    End Sub

    マナ

  5. #5
    The macro assumes anu1.xlsx is already open if you want to open it change the line

    Set oWB = ActiveWorkbook
    to
    Set oWB = Workbooks.Open(ThisWorkbook.path & "\anu1.xlsx")
    The code reads the CSV file into the workbook. It assumes the CSV has a header row.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    If you want to exclude the header,


           With .QueryTables.Add("TEXT;" & p, .Cells(Rows.Count, 1).End(xlUp).Offset(1))
    '            .TextFileStartRow = 2	'<---add

  7. #7
    Problem Solved
    Thnx Alot Gmayor Sir and Mana Sir for giving ur Precious Time and Great Support to this post
    Have a Great Day

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •