Messy Code Needs Tidying Up with a Better more efficient way !?!?

razzandy

Active Member
Joined
Jun 26, 2002
Messages
390
Office Version
  1. 2007
Platform
  1. Windows
Hi Guys

My Brain is not working today! o_O It must be that Friday Feeling!

I have the below code and wondered if anybody can think of a better way of doing the i = 4 or i = 5 and so on:

VBA Code:
Dim ColWidth As String    
    For i = 1 To UBound(arr, 1)
        If ColWidth = "" Then
            ColWidth = Orders.Columns(i).ColumnWidth + 20
        ElseIf i = 4 Or i = 5 Or i = 7 Or i = 8 Or i = 9 Or i = 10 Or i = 11 _
        Or i = 12 Or i = 13 Or i = 14 Or i = 15 Or i = 16 Or i = 17 Or i = 18 _
        Or i = 19 Or i = 20 Or i = 21 Or i = 24 Or i = 25 Or i = 26 Or i = 27 _
        Or i = 28 Or i = 29 Or i = 30 Or i = 31 Or i = 32 Or i = 33 Or i = 34 _
        Or i = 35 Or i = 36 Or i = 37 Or i = 38 Or i = 39 Or i = 40 Or i = 41 _
        Or i = 42 Or i = 43 Or i = 44 Or i = 46 Or i = 47 Or i = 48 Or i = 49 _
        Or i = 50 Or i = 51 Or i = 52 Or i = 53 Or i = 54 Or i = 55 Or i = 56 _
        Or i = 59 Or i = 60 Then
        
            ColWidth = ColWidth & ", 0"
        Else
            ColWidth = ColWidth & ", " & Orders.Columns(i).ColumnWidth * 5
        End If
    Next
UserForm1.ListBox1.ColumnWidths = ColWidth

The Code builds a string of data to resize and hide columns in a Listbox.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
How about
VBA Code:
   For i = 1 To UBound(arr, 1)
      Select Case i
         Case 1
            ColWidth = orders.Columns(i).ColumnWidth + 20
         Case 4 To 5, 7 To 21, 24 To 44, 46 To 56, 59 To 60
            ColWidth = ColWidth & ", 0"
         Case Else
            ColWidth = ColWidth & ", " & orders.Columns(i).ColumnWidth * 5
      End Select
   Next
 
Upvote 0
Solution
How about
VBA Code:
   For i = 1 To UBound(arr, 1)
      Select Case i
         Case 1
            ColWidth = orders.Columns(i).ColumnWidth + 20
         Case 4 To 5, 7 To 21, 24 To 44, 46 To 56, 59 To 60
            ColWidth = ColWidth & ", 0"
         Case Else
            ColWidth = ColWidth & ", " & orders.Columns(i).ColumnWidth * 5
      End Select
   Next
Fluff you have saved the day again. You are amazing and teaching me so much! ? Thanks and have a great weekend!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,478
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top