copying in the data to always choose the next column to paste data. Need VBA help here. Thanks!

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi, I tried to solve this myself but cant think on how to at the moment....first time data in copied in i want it to be pasted to A1, however the next time it brings in the data i want it to keep on pasting it in +1 column...

Thanks in advance for helping!!!:)
current code. I have hilighted in BOLD Option Explicit
Code:
[FONT=Courier New]Option Explicit[/FONT]
[FONT=Courier New]Sub BringinDesiredData_col()[/FONT]
[FONT=Courier New]Dim thislr, lr As Long[/FONT]
[FONT=Courier New]Dim colHead As Long[/FONT]
[FONT=Courier New]Dim myfound As Range[/FONT]
[FONT=Courier New]Dim wb As Workbook[/FONT]
[FONT=Courier New]Dim tofind As String[/FONT]
[FONT=Courier New]thislr = Sheet1.Range("A" & Rows.Count).End(xlUp).Row[/FONT]
[FONT=Courier New]myfile = "C:\Users\pediez\Desktop\Data.xlsm"[/FONT]
[FONT=Courier New]Set wb = Workbooks.Open(Filename:=myfile)[/FONT]
[FONT=Courier New]ThisWorkbook.Activate[/FONT]
[FONT=Courier New]For Each colHead In Sheet1.Range("A1:A" & thislr)[/FONT]
[FONT=Courier New]       If colHead.Value <> "" Then[/FONT]
[FONT=Courier New]       tofind = colHead.Text[/FONT]
[FONT=Courier New]       wb.Activate[/FONT]
[FONT=Courier New]       Sheets("alldata").Activate[/FONT]
[FONT=Courier New]       lr = Sheets("alldata").Range("A" & Rows.Count).End(xlUp).Row[/FONT]
[FONT=Courier New]           With Sheets("alldata").Rows("1:1")[/FONT]
[FONT=Courier New]           Set myfound = .Find(What:=tofind, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole)[/FONT]
[FONT=Courier New]           If myfound Is Nothing Then[/FONT]
[FONT=Courier New]           MsgBox myfound & "not found"[/FONT]
[FONT=Courier New]           Exit Sub[/FONT]
[FONT=Courier New]           Else[/FONT]
[FONT=Courier New]           [B][U][COLOR=blue]myfound.Resize(lr).copy thisworkbook.Sheets("Sheet1").[/COLOR][/U][/B][/FONT]
[FONT=Courier New]           End If[/FONT]
[FONT=Courier New]           End With[/FONT]
[FONT=Courier New]       End If[/FONT]
[FONT=Courier New]Next colHead[/FONT]
[FONT=Courier New]End Sub[/FONT]
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi Pedie. Maybe

Code:
 myfound.Resize(LR).Copy ThisWorkbook.Sheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Offset(, 1)
 
Upvote 0
Hi Peter, i tried that line and it start pasting data from COL B
Improved code....please validate...

Code:
[/FONT]
[FONT=Courier New]Option Explicit
Sub BringinDesiredData_col()
Dim thislr, lr As Long
Dim colHead As Range
Dim myfound As Range
Dim wb As Workbook
Dim tofind As String
Dim col As String
Dim myfile As String
Dim mycol As Integer[/FONT]
[FONT=Courier New]mycol = 1
thislr = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
myfile = "C:\Users\Pediez\Desktop\Data.xlsm"
Set wb = Workbooks.Open(Filename:=myfile)
ThisWorkbook.Activate
For Each colHead In ThisWorkbook.Sheets("Sheet1").Range("A1:A" & thislr)
        If colHead.Value <> "" Then
        tofind = colHead.Text
        wb.Activate
        Sheets("alldata").Activate
        lr = Sheets("alldata").Range("A" & Rows.Count).End(xlUp).Row
            With Sheets("alldata").Rows("1:1")
            Set myfound = .Find(What:=tofind, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole)
            If myfound Is Nothing Then
            MsgBox tofind & "not found"
            GoTo closewb
            Exit Sub
            Else
            myfound.Resize(lr).Copy ThisWorkbook.Sheets("Here").Columns(0 + mycol)
          
            End If
            End With
            
            mycol = mycol + 1
        End If
Next colHead
GoTo closewb
End
closewb:
wb.Close False
End
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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