If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > New to Loops

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-07-11, 16:59
Range Range is offline
Registered User
 
Join Date: Mar 2011
Posts: 28
New to Loops

Code:
Private Sub CmdMon_Click()
        Application.Goto Reference:="ALL1"
        Selection.EntireColumn.Hidden = True
        Application.Goto Reference:="bob1"
        Selection.EntireColumn.Hidden = False
        
        Application.Goto Reference:="ALL1"
        Selection.EntireColumn.Hidden = True
        Application.Goto Reference:="bob2"
        Selection.EntireColumn.Hidden = False  

      Application.Goto Reference:="ALL1"
        Selection.EntireColumn.Hidden = True
        Application.Goto Reference:="bob3"
        Selection.EntireColumn.Hidden = False  

 
    Application.Goto Reference:="ALL1"
        Selection.EntireColumn.Hidden = True
        Application.Goto Reference:="bob4"
        Selection.EntireColumn.Hidden = False    

    Application.Goto Reference:="ALL1"
        Selection.EntireColumn.Hidden = True
        Application.Goto Reference:="bob5"
        Selection.EntireColumn.Hidden = False    

    Application.Goto Reference:="ALL1"
        Selection.EntireColumn.Hidden = True
        Application.Goto Reference:="bob6"
        Selection.EntireColumn.Hidden = False 

       Application.Goto Reference:="ALL1"
        Selection.EntireColumn.Hidden = True
        Application.Goto Reference:="bob7"
        Selection.EntireColumn.Hidden = False
End Sub
Whats the best way to put this code into a loop? I'm new to VB code. Thanks
Reply With Quote
  #2 (permalink)  
Old 03-07-11, 17:45
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
Like this:
Code:
Private Sub CmdMon_Click()
 
    Dim lNum As Long
 
    For lNum = 1 To 7
 
        Application.Goto Reference:="ALL1"
        Selection.EntireColumn.Hidden = True
        Application.Goto Reference:="bob" & CStr(lNum)
        Selection.EntireColumn.Hidden = False
 
    Next lNum
 
End Sub
__________________
Colin

RAD Excel Blog

Other tutorials:
Array Formulas | Deleting Rows with VBA
Reply With Quote
  #3 (permalink)  
Old 03-07-11, 17:46
Range Range is offline
Registered User
 
Join Date: Mar 2011
Posts: 28
thanks colin
Reply With Quote
  #4 (permalink)  
Old 03-07-11, 17:54
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
And you don't need to hide ALL1's column seven times, so you could move it outside the loop. You can also simplify the code by removing the Goto method and the Selection object. For example, assuming the ranges are on the activesheet:
Code:
Private Sub CmdMon_Click()
    
    Dim lNum As Long
    
    ActiveSheet.Range("ALL1").EntireColumn.Hidden = True
    
    For lNum = 1 To 7
        ActiveSheet.Range("BOB" & CStr(lNum)).EntireColumn.Hidden = False
    Next lNum
End Sub
The other thing I'll mention is that in XL2007+, BOB and ALL are valid column letters. I'm not sure from your post, but if you are using BOB1 etc as named ranges then you would probably be better off naming something else to avoid conflicts. If you are using them just as cell references (ie BOB is the column reference) then that's fine.
__________________
Colin

RAD Excel Blog

Other tutorials:
Array Formulas | Deleting Rows with VBA
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On