Page 1 of 2 12 LastLast
Results 1 to 10 of 11

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

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0

    Excel Macro to populate the values in the required cells

    Hi All,

    I have come up with a generic excel sheet which has n number of sheets where i need to populate a set of values where the listid column ends.

    if sheet name ends with "P" letter than need to populate f,g,h and i columns with the following values" 1 Minutes -4431 01/01/2013"
    if sheet name ends with "S" letter than need to populate f,g,h and i columns with the following values" 0 Minutes -4431 01/01/2013"


    sample sheet enclosed here for your ready reference.would appriciate if you can help me to automate this.thank you for your assitance.

    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=UgykemWTw-fGoPwu8E14AaABAg.9iECYNx-n4U9iK75iCEaGN
    https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=UgykemWTw-fGoPwu8E14AaABAg.9iECYNx-n4U9iK7XF33njy
    https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=Ugy_1xkcndYdzUapw-J4AaABAg.9iGOq_leF_E9iKCSgpAqA1
    https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=Ugy_1xkcndYdzUapw-J4AaABAg.9iGOq_leF_E9iKCy--3x8E
    https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=UgwNaJiNATXshvJ0Zz94AaABAg. 9iEktVkTAHk9iF9_pdshr6
    https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=UgykemWTw-fGoPwu8E14AaABAg.9iECYNx-n4U9iFAZq-JEZ-
    https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=UgxV2r7KQnuAyZVLHH54AaABAg. 9iDVgy6wzct9iFBxma9zXI
    https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=Ugx12mI-a39T41NaZ8F4AaABAg.9iDQqIP56NV9iFD0AkeeJG
    https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=UgwnYuSngiuYaUhEMWN4AaABAg. 9iDQN7TORHv9iFGQQ5z_3f
    https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=UgwJ3yzdk_EE98dndmt4AaABAg. 9iDLC2uEPRW9iFGvgk11nH
    https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=UgyDWAVqCa4yMot463x4AaABAg. 9iH3wvUZj3n9iHnpOxOeXa
    https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=UgwvLFdMEAba5rLHIz94AaABAg. 9iGReNGzP4v9iHoeaCpTG8
    https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=Ugy_1xkcndYdzUapw-J4AaABAg.9iGOq_leF_E9iHpsWCdJ5I
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Attached Files Attached Files
    Last edited by DocAElstein; 09-22-2023 at 04:50 PM.

  2. #2
    Member
    Join Date
    Jun 2013
    Posts
    93
    Rep Power
    12
    Code:
    Sub a()
    For Each sh In Sheets
      LR = sh.Cells(Rows.Count, "A").End(xlUp).Row
      sh.Range("H9:H" & LR) = "Minutes"
      sh.Range("I9:I" & LR) = -4431
      If Right(sh.Name, 1) = "S" Then sh.Range("G9:G" & LR) = 0
      If Right(sh.Name, 1) = "P" Then sh.Range("G9:G" & LR) = 1
    Next
    End Sub

  3. #3
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    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

  4. #4
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    14
    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.

  5. #5
    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.

  6. #6
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    14
    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

  7. #7
    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.

  8. #8
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    14
    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

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

  10. #10
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0
    It was very useful rick , would it be possible to avoid hard coding of some values which means rather than hard-coding of "-4431,1/1/2013" can we pick up B1 and B2 values to populate them
    for example -4431 can be copied from B1 cell and 1/1/2013 can be copied from B2 Cell.

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
  •