Consulting

Results 1 to 6 of 6

Thread: Solved: Transferring data

  1. #1

    Solved: Transferring data

    Dear sir,with best regards,i have in enclosed file a (very nice ) code for transferring data from main sheet to (12) sheets being the (12) months of the year ,only one problem caused some disturbance to me .
    When i press the button for transferring ,everything is ok except the data after (5)th month which appears in (12)th month as mentioned in the attached file.
    I need your help please by some modification for the code from our experts to work well .
    Thanks in advance.
    Note: i have written about this problem to my wonderful forum (excel forum) but i think our experts there are so busy .

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try
    [VBA]
    LastRow = .Range("B" & Rows.Count).End(xlUp).Row
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    [Edit - mdmackillop's suggestion above is better.]

    The problem is actually that your 'very nice' code appears to copy the last invoice in the list to the last sheet irrespective of what month it is in.

    The problem stems from your treatment of blank cells to find the last cell, and that if you get the Month(BlankCell) it returns 12, hence why it was copied to the last sheet.

    Your use of [vba]LastRow = .Range("A" & Rows.Count).End(xlUp).Row[/vba] was not giving you the 'lastrow' you wanted, it counts rows with formulas in even if they equal "", you want the last non blank row.

    See amended code below;

    [vba]
    Option Explicit

    Sub transferdata()

    Dim MyMonth As String
    Dim ws As Worksheet
    Dim i As Long
    Dim FirstRow As Long
    Dim LastRow As Long
    Dim NewRow As Long
    Dim Invoice
    Dim c As Object

    '// Delete all rows from 5 down in all sheets apart from the main sheet.
    For Each ws In Sheets
    If ws.Name <> "main" Then
    ws.Rows("5:" & Rows.Count).Delete
    'Debug.Print ws.Name & " deleted"
    End If
    Next ws

    With Sheets("Main")

    FirstRow = 5 '// FirstRow row
    LastRow = .Range("A" & Rows.Count).End(xlUp).Row '// find last used row in column A

    'Debug.Print "LastRow"; LastRow; "'// This isn't the last row you really want."

    For i = LastRow To FirstRow Step -1
    If .Range("A" & i).Value = "" Then
    LastRow = LastRow - 1
    Else
    Exit For
    End If
    Next i

    'Debug.Print "New LastRow"; LastRow; "'// This is the last row you actually want."

    For i = FirstRow To LastRow

    If .Range("C" & i) <> "" Then
    Invoice = .Range("C" & i)
    End If

    If i = LastRow Or .Range("C" & (i + 1)) <> "" Then '// if last row or cell below not blank

    If .Range("B" & i).Value <> "" Then
    MyMonth = Month(.Range("B" & i))
    End If

    With Sheets(MyMonth)

    Set c = .Columns("C").Find(what:=Invoice, LookIn:=xlValues, lookat:=xlWhole)

    If c Is Nothing Then
    NewRow = .Range("B" & Rows.Count).End(xlUp).Row + 1

    If NewRow = 4 Then
    NewRow = 5
    End If

    Sheets("main").Rows(FirstRow & ":" & i).Copy _
    Destination:=.Rows(NewRow)
    FirstRow = i + 1
    End If

    End With
    End If
    Next i

    End With

    Set c = Nothing

    End Sub[/vba]

  4. #4
    Dear sir,many thanks for your interest,i am so happy to reach the target at last.
    My best regards.

  5. #5

    solved

    Quote Originally Posted by tofimoon4
    Dear sir,with best regards,i have in enclosed file a (very nice ) code for transferring data from main sheet to (12) sheets being the (12) months of the year ,only one problem caused some disturbance to me .
    When i press the button for transferring ,everything is ok except the data after (5)th month which appears in (12)th month as mentioned in the attached file.
    I need your help please by some modification for the code from our experts to work well .
    Thanks in advance.
    Note: i have written about this problem to my wonderful forum (excel forum) but i think our experts there are so busy .

  6. #6
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Hi tofimoon4 - I've marked this thread as "solved". You can do this under "Thread Tools" at the top of the page.

Posting Permissions

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