Results 1 to 10 of 11

Thread: Excel Macro to populate the values in the required cells

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    Try this from a module or workbook code module

    Code:
    Sub ExcelFox()
    
        Dim wks As Worksheet
        
        For Each wks In ThisWorkbook.Worksheets
            If Right(UCase(wks.Name), 1) = "P" Then
                With wks
                    .Range("G9:J" & .Cells(.Rows.Count, 1).End(xlUp).Row).Value = _
                        Array(1, "Minutes", -4431, #1/1/2013#)
                End With
            ElseIf Right(UCase(wks.Name), 1) = "S" Then
                With wks
                    .Range("G9:J" & .Cells(.Rows.Count, 1).End(xlUp).Row).Value = _
                        Array(0, "Minutes", -4431, #1/1/2013#)
                End With
            End If
        Next wks
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  2. #2
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    Quote Originally Posted by Excel Fox View Post
    Try this from a module or workbook code module

    Code:
    Sub ExcelFox()
        Dim wks As Worksheet
        For Each wks In ThisWorkbook.Worksheets
            If Right(UCase(wks.Name), 1) = "P" Then
                With wks
                    .Range("G9:J" & .Cells(.Rows.Count, 1).End(xlUp).Row).Value = _
                        Array(1, "Minutes", -4431, #1/1/2013#)
                End With
            ElseIf Right(UCase(wks.Name), 1) = "S" Then
                With wks
                    .Range("G9:J" & .Cells(.Rows.Count, 1).End(xlUp).Row).Value = _
                        Array(0, "Minutes", -4431, #1/1/2013#)
                End With
            End If
        Next wks
    End Sub
    You can shorten the above code to this...

    Code:
    Sub ExcelFox()
      Dim wks As Worksheet
      For Each wks In ThisWorkbook.Worksheets
        If wks.Name Like "*[PS]" Then wks.Range("G9:J" & wks.Cells(Rows.Count, "A").End( _
               xlUp).Row) = Array(Abs(wks.Name Like "*P"), "Minutes", -4431, #1/1/2013#)
      Next
    End Sub
    Note: I wrapped the long line of code using the line continuation character for neatness of display, but you can unwrap that line if you want.
    Last edited by Rick Rothstein; 11-02-2013 at 12:11 PM.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0
    Quote Originally Posted by Rick Rothstein View Post
    You can shorten the above code to this...

    Code:
    Sub ExcelFox()
      Dim wks As Worksheet
      For Each wks In ThisWorkbook.Worksheets
        If wks.Name Like "*[PS]" Then wks.Range("G9:J" & wks.Cells(Rows.Count, "A").End( _
               xlUp).Row) = Array(Abs(wks.Name Like "*P"), "Minutes", -4431, #1/1/2013#)
      Next
    End Sub
    Note: I wrapped the long line of code using the line continuation character for neatness of display, but you can unwrap that line if you want.

    Hi Rick Rothstein

    I am having problem here ["G9:J" & wks.Cells(Rows.Count, "A")], I was just trying to do it for my practice but it is not giving me results, can you elaborate please?

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0
    Many thanks for your quick reply.
    great guys , this is perfectly working. would it be possible to generate a sequence set of values with respect to "P" sheets which means if 1st (P) sheet has a value of "1" then next "P" sheet should contain a value of "2" i,e adding +1 to the next occurrence.

    For example :

    P Sheet " 1 Minutes -4431 01/01/2013"
    S Sheet " 0 Minutes -4431 01/01/2013"
    P Sheet " 2 Minutes -4431 01/01/2013"
    S Sheet " 0 Minutes -4431 01/01/2013"

    Please note there is no change required for "S" sheets.

  5. #5
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    Quote Originally Posted by pbabu View Post
    Many thanks for your quick reply.
    great guys , this is perfectly working. would it be possible to generate a sequence set of values with respect to "P" sheets which means if 1st (P) sheet has a value of "1" then next "P" sheet should contain a value of "2" i,e adding +1 to the next occurrence.

    For example :

    P Sheet " 1 Minutes -4431 01/01/2013"
    S Sheet " 0 Minutes -4431 01/01/2013"
    P Sheet " 2 Minutes -4431 01/01/2013"
    S Sheet " 0 Minutes -4431 01/01/2013"

    Please note there is no change required for "S" sheets.
    Does this code do what you want?

    Code:
    Sub ExcelFoxAndRick()
      Dim wks As Worksheet, Rng As Range
      For Each wks In ThisWorkbook.Worksheets
        Set Rng = wks.Range("G9:J" & wks.Cells(Rows.Count, "A").End(xlUp).Row)
        If wks.Name Like "*[PS]" Then Rng = Array(Abs(wks.Name Like "*P"), "Minutes", -4431, #1/1/2013#)
        If Right(wks.Name, 1) = "P" Then Rng.Columns(1) = Evaluate("ROW(" & Rng.Address & ")")
      Next
    End Sub

  6. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0
    Apologies for the confusion.No Rick, the logic that you have provided is basically generating a sequence set of values with in the same sheet but i need other way around which means complete (P) sheet should have same set of value like ("1 Minutes -4431 01/01/2013")
    and next occurence of (P) should have incremental value like (" 2 Minutes -4431 01/01/2013).

    For example:
    FreeP sheet : " 1 Minutes -4431 01/01/2013".
    FreeS Sheet : " 0 Minutes -4431 01/01/2013"
    CostP Sheet : "" 2 Minutes -4431 01/01/2013"
    CostS Sheet : " 0 Minutes -4431 01/01/2013"

    I hope this clears the confusion.

  7. #7
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    Quote Originally Posted by pbabu View Post
    Apologies for the confusion.No Rick, the logic that you have provided is basically generating a sequence set of values with in the same sheet but i need other way around which means complete (P) sheet should have same set of value like ("1 Minutes -4431 01/01/2013")
    and next occurence of (P) should have incremental value like (" 2 Minutes -4431 01/01/2013).

    For example:
    FreeP sheet : " 1 Minutes -4431 01/01/2013".
    FreeS Sheet : " 0 Minutes -4431 01/01/2013"
    CostP Sheet : "" 2 Minutes -4431 01/01/2013"
    CostS Sheet : " 0 Minutes -4431 01/01/2013"
    Okay then, how about this macro, does it do what you want?

    Code:
    Sub ExcelFoxAndRick()
      Dim Index As Long, Wks As Worksheet, Rng As Range
      For Each Wks In ThisWorkbook.Worksheets
        Set Rng = Wks.Range("G9:J" & Wks.Cells(Rows.Count, "A").End(xlUp).Row)
        If Wks.Name Like "*[PS]" Then Rng = Array(Abs(Wks.Name Like "*P"), "Minutes", -4431, #1/1/2013#)
        If Right(Wks.Name, 1) = "P" Then
          Index = Index + 1
          Rng.Columns(1) = Index
        End If
      Next
    End Sub

  8. #8
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0
    it works perfect. Thank you very much rick !

Similar Threads

  1. populate default values in cell of a csv file
    By dhivya.enjoy in forum Excel Help
    Replies: 2
    Last Post: 10-23-2013, 12:59 PM
  2. Replies: 12
    Last Post: 07-26-2013, 07:39 AM
  3. VBA Program to Compare 4 Columns in Excel (Required)
    By vijaysram in forum Excel Help
    Replies: 11
    Last Post: 06-26-2013, 10:53 AM
  4. Unmerge Cells and Fill with Duplicate Values
    By princ_wns in forum Excel Help
    Replies: 3
    Last Post: 10-09-2012, 07:36 AM

Posting Permissions

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