help with the vba code

agog12

Board Regular
Joined
Jan 23, 2018
Messages
104
VBA Code:
Sub STEP9()

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 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\Files\Error.xlsx")
    Set oSheet = oWB.Sheets(1)
    NextRow = oSheet.UsedRange.Rows(oSheet.UsedRange.Rows.Count).Row + 1
    FileName = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\1..csv")
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set MyFile = FSO.OpenTextFile(FileName, 1)
    Arr = Split(MyFile.ReadAll, vbNewLine)
    For lngRow = 0 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
    oWB.Close SaveChanges:=True
End Sub


plz have a look sir i am getting error with this line
VBA Code:
FileName = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\1..csv")
Plz help me in solving this problem
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
variable FileName is a string
Rich (BB code):
FileName = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\1..csv")
FileName = "C:\Users\WolfieeeStyle\Desktop\1..csv"
 
Upvote 0
Thnx Yongle for ur great help the code is working but i am not getting the desired result from this macro
what i need is copy all the data from 1.csv and paste it to error.xlsx

could u help me out in solving this problem plz recheck my vba code
 
Upvote 0
i am not getting the desired result from this macro

That is a very different question to the first question that you asked :eek:
- it may be better if you begin a new thread and explain EXACTLY what you are trying to achieve
The code below
- opens csv(A) and workbook(B) and copies the data from A to the first sheet in B
- If it is VERY close to what you want then we can continue here
- VERY close means that the data simply needs pasting to a different location in B

VBA Code:
Sub OpenCSV_PasteToExcel()
'variables
    Dim csv As String, xl As String, csvWb As Workbook, xlWb As Workbook
'paths to files including names and extensions
    csv = "C:\Test\CSVfolder\Name_Of_CSV_file.csv"
    xl = "C:\Test\Excelfolder\Name_Of_Excel_file.xlsx"
'open both files
    Application.ScreenUpdating = False
    Set xlWb = Workbooks.Open(xl)
    Set csvWb = Workbooks.Open(csv)
'copy data from csv to workbook and save workbook
    csvWb.Sheets(1).UsedRange.Copy xlWb.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Offset(1)
    xlWb.Save
'close csv
    csvWb.Close False
End Sub
 
Upvote 0
While pasting the data it is pasting from 2nd row & i want the data should be pasted from 1st row
can u plz help me in that
 
Upvote 0
Rich (BB code):
csvWb.Sheets(1).UsedRange.Copy xlWb.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Offset(1)

csvWb.Sheets(1).UsedRange.Copy xlWb.Sheets(1).Cells(1,1)
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,659
Members
449,091
Latest member
peppernaut

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