+ Reply to Thread
Results 1 to 7 of 7

clean code from recorded macro

Hybrid View

tim5 clean code from recorded macro 02-07-2012, 11:59 AM
Mordred Re: clean code from recorded... 02-07-2012, 12:06 PM
tim5 Re: clean code from recorded... 02-07-2012, 12:21 PM
Mordred Re: clean code from recorded... 02-07-2012, 12:29 PM
Mordred Re: clean code from recorded... 02-07-2012, 12:37 PM
tim5 Re: clean code from recorded... 02-07-2012, 12:44 PM
Mordred Re: clean code from recorded... 02-07-2012, 12:45 PM
  1. #1
    Registered User
    Join Date
    06-22-2011
    Location
    location
    MS-Off Ver
    Excel 2007
    Posts
    47

    clean code from recorded macro

    Hi guys,

    I have a wkb and if I make some changes in it and then try to work at office, the hours show "h:mm". I have recorded a macro in order to set "hh:mm" for some cells.
    because I have more than 50 sheets is possible ot have it ...shorter than I got it?
    the sheets are named as:
    "stu-2437"
    "stu-567A"
    "stu-700ABC"
    "stu-item1" to "stu-item12"
    I appreciate your help!

    regards, Tim

    Private Sub CommandButton7_Click()
    
    Application.ScreenUpdating = True
       Sheets("stu-71").Select
        Range("J16:L20").NumberFormat = "hh:mm"
        Range("P16:R20").NumberFormat = "hh:mm"
        
       Sheets("stu-812A").Select
        Range("J16:L20").NumberFormat = "hh:mm"
        Range("P16:R20").NumberFormat = "hh:mm"
        
       Sheets("stu-913").Select
        Range("J16:L20").NumberFormat = "hh:mm"
        Range("P16:R20").NumberFormat = "hh:mm"
    '
    '
    Last edited by tim5; 02-07-2012 at 12:22 PM.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: clean code from recorded macro

    Hi tim5, try
    Private Sub CommandButton7_Click()
        Dim Ws As Worksheet
        
        For Each Ws In Worksheets
            Ws.Range("J16:L20").NumberFormat = "hh:mm"
            Ws.Range("P16:R20").NumberFormat = "hh:mm"
        Next Ws
    '
    '
    End Sub
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Registered User
    Join Date
    06-22-2011
    Location
    location
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: clean code from recorded macro

    hi Mordred,

    Thank you very much for yr help! nice, fast and helpful!
    hope it won't affect other sheets!

    greatly appreciated!

    regards,Tim

  4. #4
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: clean code from recorded macro

    It's going to affect every sheet in the workbook. If you only want specific sheets affected then add the do something like:
    For Each Ws In Worksheets
            If Ws.Name = "stu-2437" Then
                Ws.Range("J16:L20").NumberFormat = "hh:mm"
                Ws.Range("P16:R20").NumberFormat = "hh:mm"
                End If
            Next Ws
    Except now you'll have to code for each specific sheet in your workbook. You could probably use Select Case (see here).

  5. #5
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: clean code from recorded macro

    This is not tested because I don't have your workbook but perhaps:
    Dim Ws As Worksheet
        Dim i As Integer
    
        For Each Ws In Worksheets
            Select Case Ws.Name
            Case "stu-2437"
                Ws.Range("J16:L20").NumberFormat = "hh:mm"
                Ws.Range("P16:R20").NumberFormat = "hh:mm"
            Case "stu-567A"
                Ws.Range("J16:L20").NumberFormat = "hh:mm"
                Ws.Range("P16:R20").NumberFormat = "hh:mm"
            Case "stu-700ABC"
                Ws.Range("J16:L20").NumberFormat = "hh:mm"
                Ws.Range("P16:R20").NumberFormat = "hh:mm"
                For i = 1 To 12
                Select Case Ws.Name
                Case "stu-item" & i
                    Ws.Range("J16:L20").NumberFormat = "hh:mm"
                    Ws.Range("P16:R20").NumberFormat = "hh:mm"
                End Select
                Next i
            End Select
        Next Ws

  6. #6
    Registered User
    Join Date
    06-22-2011
    Location
    location
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: clean code from recorded macro

    hi,
    I know it affects all sheets, but the range of the others sheets is outside and some of them have text!
    thank you for sending me to "Select case", I think it's .... a little bit complicated for me, I'll try to "decrypt" it!

    regards, Tim

    edit: I see what select means...well, I have too many sheets for using it! I use the first code you provided!
    I call you if I encounter problems!
    Last edited by tim5; 02-07-2012 at 12:48 PM.

  7. #7
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: clean code from recorded macro

    If it doesn't work or you need help understanding it, just let me know.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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