Below VBA copies data from file Report.xlsx and paste in workbook Dash at sheet "RawDataBher". I want to paste the data at column A to E but it paste at Column A & B and leave Columns C & D blank and other data is pasted at column e,f,g as shown in attached picture. Please Help
Attached are the workbooks
Public Sub CommandButton1_Click()
Dim fn As String
Dim Rng As Range
Dim lastrow As Long
ThisWorkbook.Worksheets("RawDataBHer").Activate
Const wsName As String = "Table 1"
fn = "C:\Users\Xahid\Desktop\sample\Report.xlsx"
If fn = "False" Then Exit Sub
If Not IsSheetExistsIn(wsName, fn) Then
MsgBox Chr(34) & wsName & Chr(34) & " not found in " & fn: Exit Sub
End If
Dim cn As Object, rs As Object
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
With cn
.Provider = "Microsoft.Ace.OLEDB.12.0"
.Properties("Extended Properties") = "Excel 12.0;HDR=Yes;"
.Open fn
End With
rs.Open "Select * From `" & wsName & "$`;", cn
Sheets("rawdatabher").Range("a" & Rows.Count).End(xlUp)(2).CopyFromRecordset rs
Set cn = Nothing: Set rs = Nothing
End Sub
Function IsSheetExistsIn(ByVal wsName As String, fn As String) As Boolean
Dim temp, x
temp = Application.Replace(fn, InStrRev(fn, "\") + 1, , "[") & "]"
On Error Resume Next
x = ExecuteExcel4Macro("'" & temp & wsName & "'!r1c1")
On Error GoTo 0
IsSheetExistsIn = Not IsError(x)
End Function
Bookmarks