+ Reply to Thread
Results 1 to 2 of 2

VBA to paste data into particular rows

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-17-2011
    Location
    PH
    MS-Off Ver
    Excel 2007
    Posts
    183

    VBA to paste data into particular rows

    Hi,

    I have created 3 different select statement (query) with different where clause critera. The first query wil start copying in row 2 until row 199. Second query will dump the data starting row 201 up to 399 while the last query will dump the data from row 401 up to 599. May I ask your help on how to do this in vba excel. your hlp is very much appreciated. thank you.


            Dim sconnect As String
            sconnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                    "Data Source=" & datasource & ";" & _
                    "Extended Properties=""Excel 12.0;HDR=YES"";"
            con.Open sconnect: DoEvents
    
            Dim sqlstr As String
            sqlstr = "SELECT * FROM [SAMPLE1$] e "
            sqlstr = sqlstr & "WHERE e.Supplier = 'XXX' "
    
            rec.Open sqlstr, con, 3, 1: DoEvents
    
            Set wb = ThisWorkbook
            With wb.Sheets("Template")
                SourceRow = .Cells(.Rows.Count, "B").End(xlUp).Row
            End With
            Set ws = wb.Sheets("Original")
            
            'clear data
            ws.Range("A2:BK800").ClearContents
            TargetRow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row
            
            'Evaluate the rows
            varCount = rec.RecordCount
            If varCount > 1 Then
                With ws
                    ' Need this portion to dump in row2
                    .Range("A" & .Rows.Count).End(xlUp).Offset(1, 0).CopyFromRecordset rec
                End With
            Else
                MsgBox "Successfully updated XXX Supplier to Original Worksheet!", vbInformation
                ActiveWorkbook.RefreshAll
                Exit Sub
            End If

  2. #2
    Forum Contributor
    Join Date
    07-17-2011
    Location
    PH
    MS-Off Ver
    Excel 2007
    Posts
    183

    Re: VBA to paste data into particular rows

    Hi, This is the codes i have done, so far the result that i wanted to be was successfull. Is there any simple solution that would give the same result with my requirements? thanks.

    Below is my vba macro code

    first query
            'Evaluate the rows
            If ws.Range("B199").Value <> "" Then
                MsgBox "All rows have been used!", vbExclamation
                Exit Sub
            End If
            
            If TargetRow + SourceRow - 3 > 200 Then
                MsgBox "Not enough free rows!", vbExclamation
                Exit Sub
            End If
            If TargetRow < 1 Then TargetRow = 1
            
            ws.Range("A" & TargetRow).Offset(1, 0).CopyFromRecordset rec
    
    
          second query
            If ws.Range("B399").Value <> "" Then
                MsgBox "All rows have been used!", vbExclamation
                Exit Sub
            End If
            
            If TargetRow + SourceRow - 3 > 399 Then
                MsgBox "Not enough free rows!", vbExclamation
                Exit Sub
            End If
            If TartgetRow < 200 Then TargetRow = 200
            
            ws.Range("A" & TargetRow).Offset(1, 0).CopyFromRecordset rec

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Paste to blank rows in B - that have data in A
    By cmb80 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-14-2014, 10:03 AM
  2. Selection of loop data, move copy and paste data, and exclude blank rows
    By mav02004 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-18-2013, 10:20 AM
  3. Replies: 2
    Last Post: 07-18-2012, 09:11 AM
  4. Copy&Paste Certain Data Contained in Rows
    By Trmoon77 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-17-2011, 03:14 PM
  5. Replies: 2
    Last Post: 02-22-2011, 02:07 AM

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