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

razzandy

Active Member
Joined
Jun 26, 2002
Messages
388
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.
 

Some videos you may like

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,898
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

razzandy

Active Member
Joined
Jun 26, 2002
Messages
388
Office Version
  1. 2007
Platform
  1. Windows
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!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,898
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,450
Messages
5,596,221
Members
414,046
Latest member
mbeutler1203

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
Top