Consulting

Results 1 to 3 of 3

Thread: Loopthroughsheets following code

  1. #1

    Loopthroughsheets following code

    Hi
    I have the following code in my worksheet, which I would like to run through all the sheets automatically, even if there is new sheet added. Also, if you could optimize the following code and for a certain range of cells that would be awesome...Thanks XX

    Somebody told me that I can not use Target (as it is mentioned in my current code) on another sheet. That's why I can not use your original code on other worksheets. So if somebody could modify this code so that it can work as per my requirements that would be really helpful.

    988,65,87,35,785.00 As u notice here, I want the numbers in this format in excel (this is the indian rupee format) see where commas areThe code that I posted on my thread, allow us to achieve the desired results.
    My requirement:
    Also I want this to work for every sheet that I add, but it could be for only certain range of cells (A1, G100), so that it is faster.


    Thanks


     
    Private Sub Worksheet_Change(ByVal Target As Range)
     
        Dim c
        If Target.Cells.Count = 1 Then
            Select Case Target.Value
            Case Is >= 1000000000
                Target.Cells.NumberFormat = "##"",""00"",""00"",""00"",""000.00"
            Case Is >= 10000000
                Target.Cells.NumberFormat = "##"",""00"",""00"",""000.00"
            Case Is >= 100000
                Target.Cells.NumberFormat = "##"",""00"",""000.00"
            Case Else
                Target.Cells.NumberFormat = "##,###.00"
            End Select
        Else
            For Each c In Target
                Select Case c.Value
                Case Is >= 1000000000
                    c.NumberFormat = "##"",""00"",""00"",""00"",""000.00"
                Case Is >= 10000000
                    c.NumberFormat = "##"",""00"",""00"",""000.00"
                Case Is >= 100000
                    c.NumberFormat = "##"",""00"",""000.00"
                Case Else
                    c.NumberFormat = "##,###.00"
                End Select
            Next c
        End If
    End Sub
     
    Private Sub Workbook_Open()

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Put this in ThisWorkbook module
    [VBA]
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    If Intersect(Target, Sh.Range("A1:G100")) Is Nothing Then Exit Sub

    Dim c
    If Target.Cells.Count = 1 Then
    Select Case Target.Value
    Case Is >= 1000000000
    Target.Cells.NumberFormat = "##"",""00"",""00"",""00"",""000.00"
    Case Is >= 10000000
    Target.Cells.NumberFormat = "##"",""00"",""00"",""000.00"
    Case Is >= 100000
    Target.Cells.NumberFormat = "##"",""00"",""000.00"
    Case Else
    Target.Cells.NumberFormat = "##,###.00"
    End Select
    Else
    For Each c In Intersect(Target, Sh.Range("A1:G100"))
    Select Case c.Value
    Case Is >= 1000000000
    c.NumberFormat = "##"",""00"",""00"",""00"",""000.00"
    Case Is >= 10000000
    c.NumberFormat = "##"",""00"",""00"",""000.00"
    Case Is >= 100000
    c.NumberFormat = "##"",""00"",""000.00"
    Case Else
    c.NumberFormat = "##,###.00"
    End Select
    Next c
    End If
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Thanks, It is working perfectly as expected....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •