+ Reply to Thread
Results 1 to 11 of 11

Time Saving Techniques?

Hybrid View

  1. #1
    Registered User
    Join Date
    04-20-2007
    Posts
    8

    Time Saving Techniques?

    I wonder if anyone can help me, I think part of the solution involves using active cell.

    I import lots of data into a spreadsheet then have to manually format it which takes hours, I want code to automate it.

    Example: I have data such as:

    No-----Name-----Date---Price----Cost
    10-----Dino------etc-----1--------2
    10-----Dino------etc-----3-------5
    12-----Bino------etc-----4--------6
    13-----Rino------etc------5-------5
    13-----Rino-----etc-------6--------6


    I want it looking like this:

    No-----Name-----Date---Price----Cost
    10-----Dino------etc-----1--------2
    10-----Dino------etc-----3-------5
    shaded cells to here ishh 4-------7

    No-----Name-----Date---Price----Cost
    12-----Bino------etc-----4--------6
    shaded cells to here ishh 4---------6

    No-----Name-----Date---Price----Cost
    13-----Rino------etc------5-------5
    13-----Rino-----etc-------6--------6
    shaded cells to here ishh 11-------11

    The difficulty is due to the fact that the number in No column varies, for example there may be 2 rows of 10 or 4 rows of 10. What is the quickest way to format it all too, so have a border and font size etc all the same?

    At present I have to manually insert blank rows between the different numbers in the No column, copy column headings, then manually sum up Price and Cost, then insert borders. But for hundreds of records.

    Any ideas?

    I found this code to insert 3 blank rows manually for me when the numbers in No column are different:
    Sub Insert Rows
    
    myRow = 3
    
    Do Until Cells(myRow, 1) = ""
    
    If Cells(myRow, 1) <> Cells(myRow - 1,1) Then
    Rows(myRow).EntireRow.Insert
    Rows(myRow).EntireRow.Insert
    Rows(myRow).EntireRow.Insert
    myRow=myRow + 3
    End if
    myRow=myRow + 1
    
    Loop
    
    End Sub
    Thanks for any suggestions inc web sites or books, if this is possible in a macro or VBA.
    Last edited by mudraker; 04-20-2007 at 08:00 PM.

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Xelpme

    Welcome to excel forum

    Please read forum rules - see blue link below

    Your message broke the rule about wrapping VBA code - see red link below.
    I have fixed you message this time.

    Now to your problem

    Try this macro

    Note:- I have not added any shading as I did not know what you wanted shaded.

    Sub ffff()
       Dim l4R As Long
       Dim lRow As Long
       Dim lLR As Long
       Dim l1stRow As Long
       Dim lSTrow As Long
       Dim lBR(1 To 2) As Long
       Dim sNo As String
       Dim vHeader As Variant
       
       vHeader = Array("No", "Name", "Date", "Price", "Cost")
       lLR = Cells(Rows.Count, "a").End(xlUp).Row
       sNo = Cells(lLR, "a").Value
       lRow = lLR
       lSTrow = lLR + 1
       For l4R = lLR - 1 To 1 Step -1
          If Not Cells(l4R, "a").Value = sNo Then
             l1stRow = l4R + 1
             Range("d" & lSTrow & ":e" & lSTrow).Formula = "=sum(d" & l1stRow & ":d" & lRow & ")"
             If l4R > 1 Then
                lBR(1) = l4R + 1
                lBR(2) = l4R + 2
                Rows(lBR(1) & ":" & lBR(2)).EntireRow.Insert
                With Range("a" & lBR(2) & ":e" & lBR(2))
                   .Value = vHeader
                   .Font.Bold = True
                   .HorizontalAlignment = xlCenter
                End With
                lRow = l4R
                lSTrow = l4R + 1
                sNo = Cells(l4R, "a").Value
             End If
          End If
       Next l4R
       With Range("a1:e1")
          .Font.Bold = True
          .HorizontalAlignment = xlCenter
       End With
    End Sub
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Xelpme,

    This macro sorts the data first to group all the same numbers together. It creates a table for each set of numbers and their totals (Price and Cost). The totals are in bold, and each table is seperated by 2 blank lines. See the attachment for how the table is formatted. After you insert a Standard VBA Module into your workbook, copy and paste the macro code into it. You can then assign it to a command button or run it using ALT+F8. The worksheet names in the macro Sheet1 and Sheet2. Sheet1 has the data, and Sheet2 is were the tables are created. Things can be changed to the names you are using. It looks long because of all the formatting code, but it works quite fast.
    'Author: Leith Ross
    
    Option Explicit
    
    Sub CreateTotalTables()
    
      Dim HeaderRng As Range
      Dim LastRowSrc As Long
      Dim NewRowDst As Long
      Dim R As Long
      Dim TopRowDst As Long
      Dim TotalCost As Variant
      Dim TotalPrice As Variant
      Dim WksSrc As Worksheet
      Dim WksDst As Worksheet
         
        Set WksSrc = Worksheets("Sheet1")
        Set WksDst = Worksheets("Sheet2")
        
        WksDst.UsedRange.Clear
            
        With WksSrc
          .Activate
          Set HeaderRng = .Range("A1:E1")
          LastRowSrc = .Cells(.Rows.Count, "A").End(xlUp).Row
            With .Range(.Cells(2, "A"), .Cells(LastRowSrc, "E"))
              .Sort Key1:=.Cells(1, "A")
            End With
        End With
         
        R = 1
        NewRowDst = 1
        GoTo AddHeader
        
        Do While R < LastRowSrc + 1
          NewRowDst = NewRowDst + 1
          WksSrc.Range("A" & R).Resize(1, 5).Copy Destination:=WksDst.Range("A" & NewRowDst)
           'Sum the totals for Price and Cost
            TotalPrice = TotalPrice + WksSrc.Cells(R, "D")
            TotalCost = TotalCost + WksSrc.Cells(R, "E")
              If WksSrc.Cells(R, "A") <> WksSrc.Cells(R + 1, "A") Then
                 WksSrc.Range("A" & R).Resize(1, 5).Copy Destination:=WksDst.Range("A" & NewRowDst)
                 NewRowDst = NewRowDst + 1
                  'Add inside column lines
                     With WksDst.Range("A" & TopRowDst & ":E" & NewRowDst - 1).Borders(xlInsideVertical)
                       .Weight = xlThin
                       .LineStyle = xlContinuous
                     End With
                   'Add border around table
                     With WksDst.Range("A" & TopRowDst & ":E" & NewRowDst)
                       .BorderAround LineStyle:=xlContinuous, Weight:=xlMedium
                     End With
                   'Add double line above totals
                     With WksDst.Range("A" & NewRowDst).Resize(1, 5)
                       With .Borders(xlEdgeTop)
                         .Weight = xlThin
                         .LineStyle = xlDouble
                       End With
                     'Display totals in Bold
                       .Font.Bold = True
                       .Cells(1, 2).Value = "Totals"
                       .Cells(1, 4).Value = Format(TotalPrice, "currency")
                       .Cells(1, 5).Value = Format(TotalCost, "currency")
                     End With
                   'Add separator lines between total columns
                     With WksDst.Range("D" & NewRowDst)
                       With .Borders(xlEdgeLeft)
                         .Weight = xlThin
                         .LineStyle = xlContinuous
                       End With
                       With .Borders(xlEdgeRight)
                         .Weight = xlThin
                         .LineStyle = xlContinuous
                       End With
                     End With
                   TotalCost = 0
                   TotalPrice = 0
                 NewRowDst = NewRowDst + 3
    AddHeader:
               'Add bottom border to header labels
                HeaderRng.Copy Destination:=WksDst.Range("A" & NewRowDst)
                  With WksDst.Range("A" & NewRowDst).Resize(1, 5).Borders(xlEdgeBottom)
                    .Weight = xlMedium
                    .LineStyle = xlContinuous
                  End With
                TopRowDst = NewRowDst
              End If
          R = R + 1
        Loop
        
       'Delete last header
        WksDst.Range("A" & NewRowDst).Resize(1, 5).Clear
        
    End Sub
    Sincerely,
    Leith Ross
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    04-20-2007
    Posts
    8
    mudraker,

    Sorry about breaking a forum rule regarding formatting of code.

    Thank you for your coding expertise. I tried your code and it works well, I have been playing around with it trying to tinker it to my design but cannot work out how to do so.

    All I want to know regards formatting so:
    1. Each table is separated by a blank row.
    2. Each table is surrounded by the "All Borders" option.
    3. At the bottom row of each table, before the total columns, there are 3blank cells, I just want to fill this with a colour such as grey, I also need this for another blank cell on the same bottom row of each table for a column after the cost.
    4. Each heading in each table has a grey background with white bold font for any font but default.

    Also, there are times when some numbers in the No column contain an additional letter and need to be grouped together with this same number. E.g. 10, 10f, 10g, all should belong to the same separate table.

    Finally a personal question. How long have you been working with Excel to gain the expertise to write this code? I would love to write it myself but haven't the time.

    Thanks again for your knowledge.





    Leith Ross,

    I tried your code but nothing happened at all. Thank you for your time though.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Xelpme,

    If you post your workbook I'll install the macro for you. If you can't post the workbook, due to your location or confidentiality, post a larger sample of data that I can use. I will create a workbook from that with the macro it so you can see how it runs.

    Sincerely,
    Leith Ross

  6. #6
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Have a look at the attached book

    Run MainMacro

    Been writing macros early 1990's
    Attached Files Attached Files

+ 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