PDA

View Full Version : Copy paste



kaja
09-01-2019, 08:54 PM
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

gmayor
09-02-2019, 01:43 AM
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

kaja
09-02-2019, 02:29 AM
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

mana
09-02-2019, 03:51 AM
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



マナ

gmayor
09-02-2019, 03:58 AM
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.

mana
09-02-2019, 04:42 AM
If you want to exclude the header,



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

kaja
09-02-2019, 04:48 AM
Problem Solved
Thnx Alot Gmayor Sir and Mana Sir for giving ur Precious Time and Great Support to this post
Have a Great Day