Data Transfer Loop

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
574
Office Version
  1. 365
Platform
  1. Windows
I have two tabs in my worksheet. Sheet 1 is my Master sheet with several columns of data. I would like to select a column and then starting at row 2, go to the first non blank cell below row 2, and then transfer the data in column A to sheet 2 (Chart page) to cell A3, then transfer the current column of data in the active row on the Master page to B3 and the first column to the right of my active column on the Master page to C3. Then I want to go to the next non blank cell on the Master page, and do the same transfer to A4, B4, and C4 on the chart page. I would like to continue all the way down to the last row on the Master sheet.

In my code below the macro crashes on the "StartRow = Sheets("Chart").Rows(3)" with a Run-Time Error 13, Type Mismatch. What do I need to do to fix this. Secondly will the rest of the code do what I describe above?

Thanks for the help.

VBA Code:
Sub Group_Transfer()

Application.ScreenUpdating = False

Dim LastRow As Long
Dim sht As Worksheet
Dim MyCol As Long
Dim iCounter As Long
Dim MyRange As Range
Dim StartRow As Long
Dim n As Long

MyCol = ActiveCell.Column
Set sht = ActiveSheet
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
Set MyRange = Rows("3:" & LastRow)
StartRow = Sheets("Chart").Rows(3)

'   Column Heading Transfer
    Sheets("Chart").Range("K2").Value = Sheets("Master").Range(ActiveCell.Column & "1").Value
    Sheets("Master").Range(MyCol & "2").Select

'   Start looping through the range.
    For iCounter = MyRange.Rows.Count To LastRow Step ActiveCell.End(xlDown).Row
    
    For n = 0 To LastRow
'   Data transfer
        Sheets("Chart").Range("A" & StartRow + n).Value = Sheets("Master").Range("A" & ActiveCell.Row).Value
        Sheets("Chart").Range("B" & StartRow + n).Value = Sheets("Master").Range(MyCol & ActiveCell.Row).Value
        Sheets("Chart").Range("C" & StartRow + n).Value = Sheets("Master").Range(MyCol + 1 & ActiveCell.Row).Value
    Next n
     
    Next iCounter
    

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I'm afraid your macro has other problems.
But I explain the first error that appears:
You declared the variable StartRow as Long and you are setting a range, so it sends you the "Type Mismatch " error.

Rich (BB code):
Dim StartRow As Long
StartRow = Sheets("Chart").Rows(3)
'To avoid the error it could be like this:
StartRow = Sheets("Chart").Rows(3).Row

_______________________________________________________________________
Try this:

You must run the macro on the "master" sheet.
The "chart" sheet should be empty.

VBA Code:
Sub Group_Transfer()
  Dim myCol As Long, lr As Long
  myCol = ActiveCell.Column
  lr = Range("A" & Rows.Count).End(3).Row
  With Sheets("Chart")
    'Heading Transfer
    .Range("A2").Value = Range("A1")
    .Range("B2").Value = Cells(1, myCol)
    .Range("C2").Value = Cells(1, myCol + 1)
    
    'Data Transfer
    .Range("A3").Resize(lr - 1).Value = Range("A2:A" & lr).Value
    .Range("B3").Resize(lr - 1).Value = Range(Cells(2, myCol), Cells(lr, myCol)).Value
    .Range("C3").Resize(lr - 1).Value = Range(Cells(2, myCol + 1), Cells(lr, myCol + 1)).Value
  End With
End Sub
 
Upvote 0
Dante,

Thanks for your code rework. It works but my data set has gaps. So when I am in myCol, each row may or may not have data in it. What I need the code to do is end(xlDown) from the current row with data to the next row with data and then transfer the data to the chart page. In the end I hope to have my chart display only the rows with data in them for that column. How do I adjust your data transfer block to accomplish this?
 
Upvote 0
What I need the code to do is end(xlDown) from the current row with data to the next row with data and then transfer the data to the chart page.

I did not understand.
You can put an example with generic data. Where you are going to put the cursor and then with some color show me what data you want to copy.
Use XL2BB tool.
 
Upvote 0
Dante,

My worksheet is included below. In this example, myCol would be Column H.

Code Test WS.xlsm
ABCDEFGHIJKLM
1JOB #Phase 1Phase 2Phase 3Phase 4Phase 5Phase 6
2Start DateEnd DateStart DateEnd DateStart DateEnd DateStart DateEnd DateStart DateEnd DateStart DateEnd Date
311116-Mar-20208-Apr-202016-Mar-202018-Mar-202023-Mar-202027-Mar-20206-Apr-202028-Jul-202030-Mar-20203-Apr-2020
422223-Mar-202020-May-20206-Apr-202016-Apr-202020-Apr-202025-Apr-202027-Apr-202025-Nov-202011-May-202015-May-2020
5333
6444
7555
8666
9777
10888
11999
12AAA
13BBB
14CCC5-Dec-202021-Jan-202116-Dec-202020-Dec-202026-Dec-20203-Jan-20216-Jan-202010-Jun-202013-Jan-202017-Jan-2020
15DDD
16EEE9-Jan-20204-Mar-202020-Jan-202028-Jan-202030-Jan-20207-Feb-202017-Feb-202014-Jul-202024-Feb-202028-Feb-2020
17FFF8-Jan-202011-Feb-202015-Jan-202117-Jan-202120-Jan-202024-Jan-20203-Feb-202028-May-20203-Feb-20207-Feb-2020
18GGG26-Feb-20208-Apr-20209-Mar-202013-Mar-202016-Mar-202020-Mar-202030-Mar-20209-Sep-20206-Apr-202010-Apr-2020
19HHH
20III
21JJJ
22KKK
23LLL
24MMM
25NNN
26OOO
27PPP
28QQQ
29RRR9-Sep-20205-Nov-202016-Sep-202025-Sep-20207-Oct-202011-Oct-202021-Oct-20198-Apr-202010/28-11/1
30SSS
31TTT
32UUU
33VVV5-Feb-202025-Mar-202017-Feb-202021-Feb-202025-Feb-202028-Feb-20209-Mar-20205-Aug-20203/16-3/20
34WWW12-Feb-202027-May-202011-Mar-20208-Apr-202012-Mar-202024-Mar-20208-Apr-202015-Apr-202013-Apr-202013-Jan-20215/18-5/22
35XXX
36YYY
37ZZZ
Master



I want to have it pasted into the chart page like this (but have the start and end date added in of course.


Code Test WS.xlsm
ABC
2JOB #Start DateEnd Date
3111
4222
5ccc
6eee
7fff
8ggg
9rrr
10vvv
11www
12
13
14
15
16
17
18
19
20
Chart



Thanks.
 
Upvote 0
Try this

VBA Code:
Sub Group_Transfer()
  Dim sh As Worksheet, myCol As Long, lr As Long, lr2 As Long
  Application.ScreenUpdating = False
  Set sh = ActiveSheet
  myCol = ActiveCell.Column
  lr = sh.Range("A" & Rows.Count).End(3).Row
  With Sheets("Chart")
    'Heading Transfer
    .Range("A2").Value = sh.Range("A1")
    .Range("B2").Value = sh.Cells(2, myCol)
    .Range("C2").Value = sh.Cells(2, myCol + 1)
    '
    'Data Transfer
    If sh.AutoFilterMode Then sh.AutoFilterMode = False
    If .AutoFilterMode Then .AutoFilterMode = False
    lr2 = .Range("A" & Rows.Count).End(3).Row + 1
    '
    sh.Range("A2", Cells(lr, myCol)).AutoFilter myCol, "<>"
    sh.AutoFilter.Range.Range("A2:A" & lr).Copy
    .Range("A" & lr2).PasteSpecial xlPasteValues
    sh.AutoFilter.Range.Range(sh.Cells(2, myCol), sh.Cells(lr, myCol + 1)).Copy
    .Range("B" & lr2).PasteSpecial xlPasteValues
    sh.ShowAllData
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Dante,

Thank you for the updated code. It works great. I had to do a little modification to complete the output, and it looks awesome.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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