Hi
have the code below but I am getting run-time error 1004 application-defined or object-defined error and highlight this line:
rngDst.Offset(0, c).Resize(rowsize, 1).Value = rngSrc.Value
Complete code:
Code:
Sub ImportRawData()
Dim c As Long
Dim Col As Variant
Dim Filename As String
Dim Filepath As Variant
Dim rngBeg As Range
Dim rngEnd As Range
Dim rngDst As Range
Dim rngSrc As Range
Dim rowsize As Long
Dim wkbDst As Workbook
Dim wkbSrc As Workbook
Set wkbDst = ThisWorkbook
Set rngDst = wkbDst.Worksheets("STATEMENT").Range("A17:C17, E17")
Filepath = "C:\Users\jose.rossi\Desktop\AP_NCL_VENDOR STATEMENT.xlsm"
Filename = "apvtrn.csv"
On Error Resume Next
Set wkbSrc = Workbooks(Filename)
If Err = 9 Then
If Filepath <> "" Then ChDir Filepath Else ChDir ThisWorkbook.Path
Filename = Application.GetOpenFilename("Excel Workbooks, *.xlsx")
If Filename = "False" Then Exit Sub
Set wkbSrc = Workbooks.Open(Filename)
End If
On Error GoTo 0
' Clear previous data.
'rngDst.Resize(rngDst.Parent.UsedRange.Rows.Count).ClearContents
' Import the data.
With wkbSrc.Worksheets("apvtrn").UsedRange
' Step through the source data columns.
For Each Col In Array("DM", "DI", "DN", "DQ" )
' Data starts on row 1.
Set rngBeg = .Parent.Cells(1, Col)
' Find the row where the data ends in this column.
Set rngEnd = .Parent.Cells(Rows.Count, Col).End(xlUp)
' Number of rows in this column.
rowsize = rngEnd.Row - rngBeg.Row
If rowsize > 0 Then
Set rngSrc = .Parent.Range(rngBeg, rngEnd)
rngDst.Offset(0, c).Resize(rowsize, 1).Value = rngSrc.Value
End If
' Increment the column offset.
c = c + 1
Next Col
End With
End SubSub ImportRawData()
Dim c As Long
Dim Col As Variant
Dim Filename As String
Dim Filepath As Variant
Dim rngBeg As Range
Dim rngEnd As Range
Dim rngDst As Range
Dim rngSrc As Range
Dim rowsize As Long
Dim wkbDst As Workbook
Dim wkbSrc As Workbook
Set wkbDst = ThisWorkbook
Set rngDst = wkbDst.Worksheets("STATEMENT").Range("A17:C17, E17")
Filepath = "C:\Users\jose.rossi\Desktop\AP_NCL_VENDOR STATEMENT.xlsm"
Filename = "apvtrn.csv"
On Error Resume Next
Set wkbSrc = Workbooks(Filename)
If Err = 9 Then
If Filepath <> "" Then ChDir Filepath Else ChDir ThisWorkbook.Path
Filename = Application.GetOpenFilename("Excel Workbooks, *.xlsx")
If Filename = "False" Then Exit Sub
Set wkbSrc = Workbooks.Open(Filename)
End If
On Error GoTo 0
' Clear previous data.
'rngDst.Resize(rngDst.Parent.UsedRange.Rows.Count).ClearContents
' Import the data.
With wkbSrc.Worksheets("apvtrn").UsedRange
' Step through the source data columns.
For Each Col In Array("DM", "DI", "DN", "DQ" )
' Data starts on row 1.
Set rngBeg = .Parent.Cells(1, Col)
' Find the row where the data ends in this column.
Set rngEnd = .Parent.Cells(Rows.Count, Col).End(xlUp)
' Number of rows in this column.
rowsize = rngEnd.Row - rngBeg.Row
If rowsize > 0 Then
Set rngSrc = .Parent.Range(rngBeg, rngEnd)
rngDst.Offset(0, c).Resize(rowsize, 1).Value = rngSrc.Value
End If
' Increment the column offset.
c = c + 1
Next Col
End With
End Sub
Thank you
Bookmarks