+ Reply to Thread
Results 1 to 5 of 5

Vba To Copy Data From Wkbk1 To Wkbk2

Hybrid View

timbo1957 Vba To Copy Data From Wkbk1... 03-21-2011, 06:23 AM
venkat1926 Re: Vba To Copy Data From... 03-21-2011, 08:40 AM
timbo1957 Re: Vba To Copy Data From... 03-21-2011, 10:28 AM
timbo1957 Re: Vba To Copy Data From... 03-31-2011, 07:12 AM
timbo1957 Macro Picking Up Wrong Range 05-11-2011, 07:09 AM
  1. #1
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Vba To Copy Data From Wkbk1 To Wkbk2

    I need a macro that will do the following on the attached example.

    Run down Column B and for every instance of 01|13 copy the adjacent isbn in column C.

    For 01|13 entries paste the copied data as values into another workbook called Workbook 2 in cell A10, then A11 etc.

    For 02|13 entries paste the copied data as values into another workbook called Workbook 2 in cell A60, then A61 etc.

    For 03|13 entries paste the copied data as values into another workbook called Workbook 2 in cell A110, then A111 etc.

    i.e. every time the month changes by 1 jump down to A60 then A110 then A160 etc.

    Sometimes these ranges change so it would be helpful if the first cell for each month to be pasted into it is defined in the macro i.e. Range("A10").Select for example. Rather than just assuming the data will be 50 rows apart.

    This is one sheet of a set and some sheets don't have 01|13 throught to 04|13 and there are other variations so the macro needs to be able to work out that if the number in B6 is 05|13 it needs to paste into A210.

    I have given an example on sheet 2 of the attached example.

    Many thanks.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    06-17-2009
    Location
    Chennai,India
    MS-Off Ver
    Excel 2003,excel 2007
    Posts
    678

    Re: Vba To Copy Data From Wkbk1 To Wkbk2

    KEEP BOTH THE WORKBOOKS WKBK1 AND WKBK2 OPEN
    park these macros in the vbeditor of workbook wkbk1

    now try this macro "test"(macro undo is to clear wkbk2 so that you can retest)

    for redesigning the various parameters see the comments agianst the various variables carefully.

    Sub test()
    Dim j As Integer, k As Integer, r As Range, x As String
    Dim m As Integer, n As Integer
    
    k = 11
    'k is larget number with /13 and if necessary change it
    m = 10 'the first row in which to be copied.if necessary change
    n = 50 'difference between te rows in your case it is 50. if necessary change
    Workbooks("wkbk1.xls").Activate
    Worksheets("test").Activate
    Set r = Range(Range("A1"), Cells(Rows.Count, "c").End(xlUp))
    'MsgBox r.Address
    For j = 1 To k
    If j < 10 Then
    x = "0" & j & "|13"
    Else
    x = j & "|13"
    End If
    'MsgBox x
    r.AutoFilter Field:=2, Criteria1:=x
    r.Offset(1, 0).Resize(r.Rows.Count - 1, r.Columns.Count).SpecialCells(xlCellTypeVisible).Copy
    With Workbooks("wkbk2.xls").Worksheets("sheet1")
    'MsgBox .Range("A" & m).Offset((j - 1) * n, 0).Address
    .Range("A" & m).Offset(m + (j - 1) * n, 0).PasteSpecial Paste:=xlPasteValues
    End With
    'Workbooks("wkbk1.xls").Activate
    'Worksheets("test").Activate
    
    
    r.AutoFilter
    Next j
    End Sub

    Sub undo()
    With Workbooks("wkbk2.xls").Worksheets("sheet1")
    .Cells.Clear
    End With
    End Sub
    Last edited by venkat1926; 03-21-2011 at 08:43 AM.

  3. #3
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Re: Vba To Copy Data From Wkbk1 To Wkbk2

    This part of the macro is picking up the range A:C instead of just C.

    r.Offset(1, 0).Resize(r.Rows.Count - 1, r.Columns.Count).SpecialCells(xlCellTypeVisible).Copy

  4. #4
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Re: Vba To Copy Data From Wkbk1 To Wkbk2

    If anyone can help with my last post re this problem I would be grateful.

  5. #5
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Macro Picking Up Wrong Range

    Can someone help me with this please?

    The macro was kinldy supplied by a member of the forum. I think that this part of the macro is picking up columns A:C rather than just column C.

    Can anyone advise on how to change it please?

    r.Offset(1, 0).Resize(r.Rows.Count - 1, r.Columns.Count).SpecialCells(xlCellTypeVisible).Copy
    Sub test()
    Dim j As Integer, k As Integer, r As Range, x As String
    Dim m As Integer, n As Integer
    
    k = 11
    'k is larget number with /13 and if necessary change it
    m = 10 'the first row in which to be copied.if necessary change
    n = 50 'difference between te rows in your case it is 50. if necessary change
    Workbooks("wkbk1.xls").Activate
    Worksheets("test").Activate
    Set r = Range(Range("A1"), Cells(Rows.Count, "c").End(xlUp))
    'MsgBox r.Address
    For j = 1 To k
    If j < 10 Then
    x = "0" & j & "|13"
    Else
    x = j & "|13"
    End If
    'MsgBox x
    r.AutoFilter Field:=2, Criteria1:=x
    r.Offset(1, 0).Resize(r.Rows.Count - 1, r.Columns.Count).SpecialCells(xlCellTypeVisible).Copy
    With Workbooks("wkbk2.xls").Worksheets("sheet1")
    'MsgBox .Range("A" & m).Offset((j - 1) * n, 0).Address
    .Range("A" & m).Offset(m + (j - 1) * n, 0).PasteSpecial Paste:=xlPasteValues
    End With
    'Workbooks("wkbk1.xls").Activate
    'Worksheets("test").Activate
    r.AutoFilter
    Next j
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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