Results 1 to 15 of 15

VBA Copies but Paste data in another columns(not in required)

Threaded View

  1. #1
    Forum Contributor
    Join Date
    08-10-2016
    Location
    Pakistan
    MS-Off Ver
    365
    Posts
    242

    VBA Copies but Paste data in another columns(not in required)

    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
    Attached Images Attached Images
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro that copies a formula to another sheet and copies data at same time
    By Newbienewbster0932 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-26-2017, 02:49 PM
  2. Replies: 3
    Last Post: 06-29-2016, 01:38 PM
  3. Required VBAcoding to paste data
    By thermax1829 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-23-2014, 08:28 AM
  4. Macro that copies data from one sheet and pastes it to another x columns to the right
    By Stevebarnes in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-30-2013, 12:26 PM
  5. macro that copies data in cell and paste it to cell x columns to right in shhet
    By Stevebarnes in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-27-2013, 12:14 PM
  6. [SOLVED] Macro that copies every 600 rows and paste data in new spreadsheet
    By Bealey in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-27-2012, 09:38 AM
  7. [SOLVED] HOW SHOULD I IMPORT & PASTE ONLY THE REQUIRED DATA
    By amar kayala in forum Excel General
    Replies: 0
    Last Post: 01-06-2006, 09:50 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1