+ Reply to Thread
Results 1 to 21 of 21

Help Looping VBA

  1. #1
    Registered User
    Join Date
    02-21-2021
    Location
    Southampton, England
    MS-Off Ver
    2016
    Posts
    29

    Help Looping VBA

    Hi

    I'm trying to get my code to copy & paste some data onto a new workbook, remove and resize some columns, save and close the workbook and then select the next name from a dropdown menu and repeat the process until the end of the list is reached. Whilst I've achieved most of it I'm stuck at the looping the whole module.

    Below is my code, any help would be greatly appreciated;-

    Sub IndvReport()
    '
    ' IndvReport Macro
    '

    '
    Workbooks.Add
    Application.DisplayAlerts = False
    Application.WindowState = xlMaximized
    ChDir _
    "U:\Housing\CSC and CEX Admin Centre\CSC\CSC Reports\Agents Stats\Weekly\Individual Reports"
    ActiveWorkbook.SaveAs filename:= _
    "U:\Housing\CSC and CEX Admin Centre\CSC\CSC Reports\Agents Stats\Weekly\Individual Reports\Book1.xlsx" _
    , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    Windows("Test Weekly Agent Stats.xlsm").Activate
    Cells.Select
    Selection.Copy
    Windows("Book1.xlsx").Activate
    ActiveSheet.Paste
    Application.WindowState = xlMaximized
    Cells.Select
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Columns("B:D").Select
    ActiveSheet.Shapes.Range(Array("Straight Arrow Connector 1")).Select
    Selection.Delete
    Range("C1:F2").Select
    Range("F1").Activate
    Application.CutCopyMode = False
    Selection.ClearContents
    Range("A1:A2").Select
    With Selection.Validation
    .Delete
    .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
    :=xlBetween
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
    End With
    Columns("B:D").Select
    Selection.Delete Shift:=xlToLeft
    Columns("C:F").Select
    Selection.Delete Shift:=xlToLeft
    Columns("J:K").Select
    Selection.Delete Shift:=xlToLeft
    Columns("L:L").Select
    Selection.Delete Shift:=xlToLeft
    Range("N:Q,T:W,AB:AH").Select
    Range("AB1").Activate
    Selection.Delete Shift:=xlToLeft
    Columns("Z:AC").Select
    Selection.Delete Shift:=xlToLeft
    Columns("S:S").EntireColumn.AutoFit
    Columns("T:T").EntireColumn.AutoFit
    Columns("X:Y").Select
    Selection.Delete Shift:=xlToLeft
    Range("B3").Select
    Selection.Copy
    Range("B3:D3").Select
    ActiveSheet.Paste
    Range("G28").Select
    Application.CutCopyMode = False
    Range("A1:A2").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeRight)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
    End With
    Selection.Borders(xlInsideVertical).LineStyle = xlNone
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
    Dim Path As String
    Dim filename As String
    Path = "U:\Housing\CSC and CEX Admin Centre\CSC\CSC Reports\Agents Stats\Weekly\Individual Reports"
    filename = Range("A1")
    ActiveWorkbook.SaveAs filename:=Path & filename & ".xls", FileFormat:=xlNormal
    ActiveWindow.Close

    Dim dv As Validation
    Dim vaSplit As Variant
    Dim i As Long

    Set dv = ActiveCell.Validation

    vaSplit = Range(dv.Formula1).Value

    For i = LBound(vaSplit, 1) To UBound(vaSplit, 1)
    If vaSplit(i, 1) = ActiveCell.Value Then
    If i < UBound(vaSplit, 1) Then
    ActiveCell.Value = vaSplit(i + 1, 1)
    Exit For
    End If
    End If
    Next i
    Application.DisplayAlerts = True


    End Sub

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Help Looping VBA

    It would be easier to help and test possible solutions if you could attach a copy of your file. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary). See the yellow banner at the top of this page for instructions to attach a file.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    02-21-2021
    Location
    Southampton, England
    MS-Off Ver
    2016
    Posts
    29

    Re: Help Looping VBA

    Hi

    Thanks for your reply, I have uploaded a copy of the file, I'm still working exactly I want to do on the new saved workbook with columns etc, the main issue I'm having the code repeat until the last name in the dropdown list has been reached

    Thank you

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Help Looping VBA

    I don't see the link to your file.

  5. #5
    Registered User
    Join Date
    02-21-2021
    Location
    Southampton, England
    MS-Off Ver
    2016
    Posts
    29

    Re: Help Looping VBA

    attaching sample file
    Last edited by royalew/cheese; 06-28-2023 at 04:45 AM.

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Help Looping VBA

    What is the password to the file?

  7. #7
    Registered User
    Join Date
    02-21-2021
    Location
    Southampton, England
    MS-Off Ver
    2016
    Posts
    29

    Re: Help Looping VBA

    I have reattached it and removed the password

  8. #8
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Help Looping VBA

    select the next name from a dropdown menu and repeat the process until the end of the list is reached
    Do you want to create a new workbook for each of the 42 agents?

  9. #9
    Registered User
    Join Date
    02-21-2021
    Location
    Southampton, England
    MS-Off Ver
    2016
    Posts
    29

    Re: Help Looping VBA

    Yes if possible I'd like it to create an individual one for each agent in the list

  10. #10
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Help Looping VBA

    Try the attached file. Run the macro in Module1. You will notice that the "Lookup" sheet is also copied into the new file. This sheet is necessary to populate the drop down list in A1:A2. I have tidied up your original macro. I can tidy it up a bit more if you let me know which columns you want deleted in the newly created files.
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    02-21-2021
    Location
    Southampton, England
    MS-Off Ver
    2016
    Posts
    29

    Re: Help Looping VBA

    That's fantastic, thank you. The only issue seems to be when it reaches the final agent in the list it errors, shows error code "Run Time Error '9': Subscript out of range and it highlights this section of the code - Range("A1:A2") = vaSplit(i + 1, 1) so the code doesn't finish running.
    Also would there be a way to hide the lookup sheet on the newly created workbooks as part of the process?

  12. #12
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Help Looping VBA

    Try this version:
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    02-21-2021
    Location
    Southampton, England
    MS-Off Ver
    2016
    Posts
    29

    Re: Help Looping VBA

    Thank you again. That hides the Lookup sheet on the new workbooks which is great however the same Runtime error pops up when reaching the last agent

  14. #14
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Help Looping VBA

    Try:
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    02-21-2021
    Location
    Southampton, England
    MS-Off Ver
    2016
    Posts
    29

    Re: Help Looping VBA

    that works perfectly now, thank you

  16. #16
    Registered User
    Join Date
    02-21-2021
    Location
    Southampton, England
    MS-Off Ver
    2016
    Posts
    29

    Re: Help Looping VBA

    After checking the workbooks the VBA is creating and saving it seems that all of them are identical apart from the Agent Name, its duplicating the stats for Agent 1 and saving them for all of the other agents so doesn't seem to be functioning correctly.

    I have added a bit to remove and format columns so they current VBA is now;-

    Sub IndvReport()
    Application.ScreenUpdating = False
    Dim dv As Validation, vaSplit As Variant, i As Long, Path As String, filename As String
    Sheets(Array("Individual Stats", "Lookup")).Copy
    ActiveSheet.UsedRange.Cells.Value = ActiveSheet.UsedRange.Cells.Value
    Range("B:D,F:K,P:R,U:U,X:Y,AD:AG,AJ:BD").Delete

    Range("I3:P3,A1:A2,I5:P22,I4:P4,I23:P23").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeRight)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
    Selection.Borders(xlInsideVertical).LineStyle = xlNone
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone


    End With
    Path = "U:\Housing\CSC and CEX Admin Centre\CSC\CSC Reports\Agents Stats\Weekly\Individual Reports"
    Set dv = Range("A1:A2").Validation
    vaSplit = Range(dv.Formula1).Value
    For i = LBound(vaSplit, 1) To UBound(vaSplit, 1)
    filename = vaSplit(i, 1)
    Range("A1") = vaSplit(i, 1)
    With ActiveWorkbook
    Application.DisplayAlerts = False
    .SaveAs filename:=Path & filename & ".xlsx", FileFormat:=51
    Application.DisplayAlerts = True
    End With
    If i + 1 <= UBound(vaSplit, 1) Then
    Range("A1:A2") = vaSplit(i + 1, 1)
    End If
    Sheets("Lookup").Visible = xlVeryHidden
    Next i
    Application.ScreenUpdating = True
    End Sub

  17. #17
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Help Looping VBA

    In your original post, you said:
    I'm trying to get my code to copy & paste some data onto a new workbook, remove and resize some columns, save and close the workbook and then select the next name from a dropdown menu and repeat the process until the end of the list is reached.
    The macro does as you requested.

    its duplicating the stats for Agent 1
    What stats should be different for each agent?

  18. #18
    Registered User
    Join Date
    02-21-2021
    Location
    Southampton, England
    MS-Off Ver
    2016
    Posts
    29

    Re: Help Looping VBA

    Apologies, I confused the issue by not including enough data in my uploaded file. I have uploaded another sample file. The summary Tab looks up the agent performance for the previous weeks which changes as you select a different agent in the dropdown box in A1. The aim is to export and save the data for agent 1, change the name to the next name is the list, export and save and continue until the last name in the list
    Attached Files Attached Files

  19. #19
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Help Looping VBA

    Try:
    Please Login or Register  to view this content.

  20. #20
    Registered User
    Join Date
    02-21-2021
    Location
    Southampton, England
    MS-Off Ver
    2016
    Posts
    29

    Re: Help Looping VBA

    That works perfectly, thank you

  21. #21
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Help Looping VBA

    My pleasure.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Looping a For Each... Next
    By ARowbot in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-07-2015, 03:23 AM
  2. help with if and looping
    By uniks in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-25-2015, 01:59 AM
  3. Do Until Looping (not looping through all other columns)
    By orle8050 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-11-2013, 10:37 AM
  4. Help with looping
    By enhydra in forum Excel Programming / VBA / Macros
    Replies: 24
    Last Post: 06-04-2011, 11:13 AM
  5. VBA help for looping
    By EXCELcior08 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-17-2010, 03:22 PM
  6. [SOLVED] Looping help please
    By Christy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-14-2006, 01:55 PM
  7. [SOLVED] looping
    By Neal in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-19-2006, 05:35 PM

Tags for this Thread

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