Results 1 to 7 of 7

Thread: My First Macro Task - Please Help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    29

    Unhappy My First Macro Task - Please Help

    I have an excel spreadsheet which I need to write macro for. The followings is what I need to do
    For every new spreadsheet, it should have
    1) the header
    2) the legion
    3) data (different data according to customer codes, so if there are 12 customer codes i'll have 12 different spreadsheets,the only similarity will be the header and legion.)
    I need to copy paste all this in a newspreadsheet and save it according to different customer codes.Im not suppose to hard code the location (rows,columns) since the location of data keeps changing from time to time.
    Does anyone have any idea how I can go about with this? If there's any examples I can look into,please do forward it to me.Would really appreciate it.Please help as this is my first macro task. Thank you so much.
    Attached Files Attached Files
    Last edited by harjit; Jul 19th, 2005 at 09:20 AM.

  2. #2
    New Member
    Join Date
    Feb 2005
    Location
    Florida
    Posts
    12

    Re: My First Macro Task - Please Help

    Well you can start with your auto_open macro. That will automatically run when your that excel file is open. You could create make an input box pop up, input the number of sheets and what you need to add to the Header. Here is an example.
    VB Code:
    1. Public Sub auto_open()
    2.  
    3. Sheets("Sheet1").Activate
    4. Application.ScreenUpdating = False
    5. ctr = 1
    6. Number = InputBox$("How many sheets do you  need?")
    7. Entry = InputBox$("Put header title here")
    8. Number = Number - 1
    9. While ctr <= Number
    10.     With ActiveSheet.PageSetup
    11.         .CenterHeader = "&P of &N"
    12.         .LeftHeader = Entry
    13.         .RightHeader = "&D" & Chr(10) & "&T"
    14.     End With
    15.     ActiveSheet.DisplayAutomaticPageBreaks = False
    16.     Sheets.Add
    17.     ctr = ctr + 1
    18. Wend
    19.  
    20. End Sub
    It's not super fast put it adds the sheets and places header info.
    ~Queen B

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    29

    Unhappy Re: My First Macro Task - Please Help

    THanks!But thats not what I want ,im so sorry,you see there's only one spreadsheet with countless of customer codes,each with different data,i need to break each and every customer code together with their data into separate spreadsheets.All the spreadsheets with have the same header and legion.One customer code,one spreadsheet.The only thing that differs is the customer code and its data.I also need to save each of the spreadsheets according to their customer codes.So for ex : customer code is - ADI the filename should be ADI.xls.Can you help me plz?I hope u understand what im trying to say

  4. #4
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: My First Macro Task - Please Help

    Not sure what Legion is so I didn't include it in the code. You can fill in that part if this is useful


    This code requires you to edit a few things: The 3 constants at the top
    topRowOfData = the first row that your customer code data starts.
    custCodeCol = the column the customer code is in. If its in Column A, put 1. If its in column D, put 4.
    And saveDir = the directory you want to save your sheets. Include the "\" at the end.

    Further assumptions this code makes are:
    There is a row above your top row of data that contains column headings.
    Everything above that set of column headings is counted as your header.
    There are no empty rows between your rows of data.

    Assuming all of this is accurate, the code below will make one spreadsheet for every customer code, with the same headings as the original sheet.

    If ANY of these assumptions are wrong, let me know and I can update the code accordingly.

    VB Code:
    1. Sub SplitCustomerCodes()
    2.  
    3.     Const topRowOfData = 6
    4.     Const custCodeCol = 1
    5.     Const saveDir = "C:\tmp\"
    6.    
    7.     Dim custCodes() As String, custCodeCount As Integer, ccFound As Boolean, curCC As String
    8.     Dim legion As String
    9.     Dim i As Integer, j As Integer
    10.     Dim thisBook As String, wipBook As String
    11.    
    12.     thisBook = ActiveWorkbook.Name
    13.     ccFound = False
    14.     ReDim custCodes(0)
    15.    
    16.     For i = topRowOfData To Cells(topRowOfData, custCodeCol).End(xlDown).Row
    17.         curCC = Cells(i, custCodeCol).Value
    18.         For j = 0 To UBound(custCodes)
    19.             If custCodes(j) = curCC Then ccFound = True
    20.         Next j
    21.         If Not ccFound Then
    22.             custCodeCount = custCodeCount + 1
    23.             ReDim Preserve custCodes(custCodeCount - 1)
    24.             custCodes(custCodeCount - 1) = curCC
    25.         End If
    26.         ccFound = False
    27.     Next i
    28.    
    29.     Rows(topRowOfData - 1 & ":" & topRowOfData - 1).AutoFilter
    30.     For i = 0 To UBound(custCodes)
    31.         Rows(topRowOfData - 1 & ":" & topRowOfData - 1).AutoFilter Field:=custCodeCol, Criteria1:=custCodes(i)
    32.         Rows(topRowOfData & ":" & Cells(topRowOfData, custCodeCol).End(xlDown).Row).Copy
    33.        
    34.         Workbooks.Add
    35.         wipBook = ActiveWorkbook.Name
    36.        
    37.         Rows(topRowOfData & ":" & topRowOfData).PasteSpecial xlPasteAll
    38.         Windows(thisBook).Activate
    39.         Range(Cells(1, 1), Cells(topRowOfData - 1, 256)).Copy
    40.         Windows(wipBook).Activate
    41.         Range("A1").PasteSpecial xlPasteAll
    42.         Range("A1").Select
    43.        
    44.         'paste Legion
    45.        
    46.         ActiveWorkbook.SaveAs Filename:=saveDir & custCodes(i) & ".xls"
    47.         ActiveWorkbook.Close
    48.     Next i
    49.     Rows(topRowOfData - 1 & ":" & topRowOfData - 1).AutoFilter
    50.    
    51. End Sub
    Last edited by mikeyc1204; Jul 18th, 2005 at 11:42 AM.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    29

    Re: My First Macro Task - Please Help

    Hey mate!Thanks yea,that was very kind of you,the thing is im not suppose to hard code this since the column n rows of the spreadsheet will keep changing in the future and there are empty rows here and there in the spreadsheet.Ive managed to upload the attachment for your reference(Refer to my first thread)I preety much think u'll have a clearer idea about my task.Now you dunt have to give me you e mail add Thank you so much!
    Last edited by harjit; Jul 19th, 2005 at 09:23 AM.

  6. #6
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: My First Macro Task - Please Help

    This code is tailored specifically to the file you attached in your post:

    You need only change the constant at the top to reflect your base directory where you want your files and folders saved.

    VB Code:
    1. Sub splitCustomers()
    2.  
    3.     Const baseDir = "c:\tmp\"
    4.    
    5.     Dim lastRow As Integer, curRow As Integer
    6.     Dim topRow As Integer, bottomRow As Integer
    7.     Dim company As String
    8.     Dim newCompany As Boolean
    9.     Dim thisBook As String, wipBook As String
    10.    
    11.     lastRow = Range("A" & 256 ^ 2).End(xlUp).Row
    12.     thisBook = ActiveWorkbook.Name
    13.     curRow = 4
    14.     newCompany = True
    15.    
    16.     Do While curRow <= lastRow
    17.         If newCompany Then
    18.             newCompany = False
    19.             topRow = curRow
    20.         End If
    21.         If Left(Cells(curRow, 1).Value, 19) = "Total by Customer :" Then
    22.             company = Mid(Cells(curRow, 1).Value, 21)
    23.             bottomRow = curRow + 2
    24.            
    25.             Rows("1:3").Copy
    26.             Workbooks.Add
    27.             wipBook = ActiveWorkbook.Name
    28.             Range("A1").PasteSpecial xlPasteAll
    29.             Windows(thisBook).Activate
    30.             Rows(topRow & ":" & bottomRow).Copy
    31.             Windows(wipBook).Activate
    32.             Range("A4").PasteSpecial xlPasteAll
    33.             bottomRow = Range("A" & 256 ^ 2).End(xlUp).Row + 3
    34.             Windows(thisBook).Activate
    35.             Rows(lastRow + 3 & ":" & lastRow + 3).Copy
    36.             Windows(wipBook).Activate
    37.             Range("A" & bottomRow).PasteSpecial xlPasteColumnWidths
    38.             Range("A" & bottomRow).PasteSpecial xlPasteAll
    39.             Range("A4").Select
    40.             ActiveWorkbook.Names.Add Name:="company", RefersToR1C1:="=""" & Trim(company) & """"
    41.             Call MakeCompanyDir(baseDir, Trim(company))
    42.             ActiveWorkbook.SaveAs Filename:=baseDir & Trim(company) & "\" & Trim(company) & ".xls"
    43.             ActiveWorkbook.Close
    44.             curRow = curRow + 2
    45.             newCompany = True
    46.         End If
    47.         curRow = curRow + 1
    48.     Loop
    49.  
    50. End Sub
    51.  
    52. Sub MakeCompanyDir(base As String, co As String)
    53.     On Error Resume Next
    54.     MkDir base & co
    55. End Sub

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    29

    Re: My First Macro Task - Please Help

    Hey mate!Your program works perfectly alrite,i manage to do some changes n it works good.Thank you so much!Anyways this is how it looks.It would be great if you can explain the codes to me as I am a little unsure about some part of the codes here.Thank you so much Michael.
    VB Code:
    1. Sub Auto_open()
    2.  
    3. Const baseDir = "c:\tmp\"
    4.    
    5.     Dim lastRow As Integer, curRow As Integer
    6.     Dim topRow As Integer, bottomRow As Integer
    7.     Dim company As String
    8.     Dim newCompany As Boolean
    9.     Dim thisBook As String, wipBook As String
    10.    
    11.     lastRow = Range("A" & 256 ^ 2).End(xlUp).Row
    12.     thisBook = ActiveWorkbook.Name
    13.     curRow = 4
    14.     newCompany = True
    15.    
    16.     Do While curRow <= lastRow
    17.         If newCompany Then
    18.             newCompany = False
    19.             topRow = curRow
    20.         End If
    21.         If Left(Cells(curRow, 1).Value, 19) = "Total by Customer :" Then
    22.             company = Mid(Cells(curRow, 1).Value, 21)
    23.             bottomRow = curRow + 2
    24.             Application.DisplayAlerts = False
    25.             Rows("1:3").Copy
    26.             Workbooks.Add
    27.             wipBook = ActiveWorkbook.Name
    28.             Range("A1").PasteSpecial xlPasteAll
    29.             Windows(thisBook).Activate
    30.             Rows(topRow & ":" & bottomRow).Copy
    31.             Windows(wipBook).Activate
    32.             Range("A4").PasteSpecial xlPasteAll
    33.             bottomRow = Range("A" & 256 ^ 2).End(xlUp).Row + 3
    34.             Windows(thisBook).Activate
    35.             Rows(lastRow + 3 & ":" & lastRow + 3).Copy
    36.             Windows(wipBook).Activate
    37.             Range("A" & bottomRow).PasteSpecial xlPasteAll
    38.             Range("A4").Select
    39.             ActiveWorkbook.Names.Add Name:="company", RefersToR1C1:="=""" & Trim(company) & """"
    40.             Call MakeCompanyDir(baseDir, Trim(company))
    41.             ActiveWorkbook.SaveAs Filename:=baseDir & Trim(company) & "\" & "S" & Format(Now(), "yyyymmdd") & ".xls"
    42.             ActiveWorkbook.Close
    43.             curRow = curRow + 2
    44.             newCompany = True
    45.             ActiveCell.HorizontalAlignment = xlRight
    46.             ActiveCell.HorizontalAlignment = xlLeft
    47.             ActiveCell.HorizontalAlignment = xlCenter
    48.             Columns("C:C").EntireColumn.AutoFit
    49.             Selection.RowHeight = 25#
    50.         End If
    51.         curRow = curRow + 1
    52.     Loop
    53.     ActiveWorkbook.Close
    54.  
    55. End Sub
    56.  
    57. Sub MakeCompanyDir(base As String, co As String)
    58.     On Error Resume Next
    59.     MkDir base & co
    60. End Sub
    Note* If new customer codes and data are added in the spreadsheet,would it still work like it does now?
    Last edited by harjit; Jul 20th, 2005 at 02:11 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width