Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88081

Is there an easy way to bucket saved files with a case statement?

$
0
0

I'm trying to create code to examine a column of names. I want to bucket files into three categories (names that fall between A-G, H-P, Q-Z).

I figure a case statement before I save off the file is the right approach, but not sure if I need to utilize an instr function or something of the sort.

Here's a crude example of how I have it set up currently (commented out the case statements).

        Dim BASEPATH_1 As String, BASEPATH_2 As String, BASEPATH_3 As String


        BASEPATH_1 = "C:\Users\A-G\"
        BASEPATH_2 = "C:\Users\H-P\"
        BASEPATH_3 = "C:\Users\Q-Z\"


        Select Case wb.Cells(i, 8)
        'Case i.value is betwen "A-G"
            wb.SaveCopyAs BASEPATH_1 & _
            ValidFileName(Login & "_"& Last & "_PrePlanning File.xlsx")
        'Case i.value is betwen "H-P"
            wb.SaveCopyAs BASEPATH_2 & _
            ValidFileName(Login & "_"& Last & "_PrePlanning File.xlsx")
        'Case i.value is betwen "Q-Z"
            wb.SaveCopyAs BASEPATH_3 & _
            ValidFileName(Login & "_"& Last & "_PrePlanning File.xlsx")
        Case Else
        End Select

Full Code:

Sub Main()
    Dim wb As Workbook
    Dim Data, Last, Login, lvl2mgr
    Dim i As Long, j As Long, k As Long, a As Long
    Dim Dest As Range
    Dim BASEPATH1 As String, BASEPATH2 As String, BASEPATH3 As String, strNewPath As String


    BASEPATH1 = "C:\A-G"
    BASEPATH2 = "C:\H-P"
    BASEPATH3 = "C:\Q-Z"

    Set wb = Workbooks("Preplanning_Template.xlsx")

    Set Dest = wb.Sheets("Manager File").Range("A3")

    With ThisWorkbook.Sheets("Planning File")
        Data = .Range("BP2", .Range("A"& Rows.Count).End(xlUp))
    End With

    wb.Activate
    Call Ludicrous(True)

    For i = 1 To UBound(Data)

        If Data(i, 7) <> Login Then
            If i > 1 Then
                Dest.Select
                wb.Sheets(1).Cells.WrapText = False
                    Call FillDown
                    Call FillColors
                wb.Cells.Columns("A:BP").EntireColumn.AutoFit
                wb.Cells.HorizontalAlignment = xlLeft
                wb.Columns("E:F").EntireColumn.Hidden = True
                ActiveSheet.Outline.ShowLevels ColumnLevels:=1
            End If

           Select Case Asc(Cells(i, 8).Value)

                Case 65 To 71 'A-G
                    wb.SaveCopyAs BASEPATH1 & _
                    ValidFileName(Login & "_"& Last & "_PrePlanning File.xlsx")

                Case 72 To 80 'H-P
                    wb.SaveCopyAs BASEPATH2 & _
                    ValidFileName(Login & "_"& Last & "_PrePlanning File.xlsx")

                Case 81 To 90 'Q-Z
                    wb.SaveCopyAs BASEPATH3 & _
                    ValidFileName(Login & "_"& Last & "_PrePlanning File.xlsx")

            Case Else
            End Select

            With wb.Sheets("Manager File")
                .Rows(3 & ":"& .Rows.Count).ClearContents
                .Rows(3 & ":"& .Rows.Count).Interior.Color = xlNone
            End With

            Login = Data(i, 7)
            Last = Data(i, 8)


            j = 0
        End If

        a = 0
        For k = 1 To UBound(Data, 2)
            Dest.Offset(j, a) = Data(i, k)
            a = a + 1
        Next
        j = j + 1
    Next

    SaveCopy wb, Login, Last

    Call Ludicrous(False)

End Sub

Viewing all articles
Browse latest Browse all 88081

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>