+ Reply to Thread
Results 1 to 12 of 12

new sheet creation with specific information

Hybrid View

  1. #1
    Registered User
    Join Date
    03-24-2017
    Location
    Riga
    MS-Off Ver
    2016
    Posts
    25

    new sheet creation with specific information

    Hello, everyone.

    I have got problem with few macros creations, hope you will understand and will be able to help

    help1.JPG

    1) I want to create macros, which automatically creates new sheets with names from "ID" column with all information, for example sheet with name "1" will have 2 rows with all information like in picture.

    2) in F column find specific names and in next sheet copy just value (for example if in column F it contains information like "Next business day" then in new sheet it just shows "N2", if Same business day, then "N1"

    3) macros for dates, if the period is year, then copy just "year", if the period is 3 years, then copy "3 years"

    Thank you in advance.

    Best regards,
    Arturs.
    Attached Files Attached Files

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: new sheet creation with specific information

    Hi Swapijs

    This code should get you started to create the sheets based on cell value
    Option Explicit
    Function getSheetWithDefault(name As String, Optional wb As Excel.Workbook) As Excel.Worksheet
            If wb Is Nothing Then
                Set wb = ThisWorkbook
            End If
    
            If Not sheetExists(name, wb) Then
                wb.Worksheets.Add(After:=wb.Worksheets(wb.Worksheets.Count)).name = name
            End If
    
            Set getSheetWithDefault = wb.Sheets(name)
    End Function
    
    Function sheetExists(name As String, Optional wb As Excel.Workbook) As Boolean
            Dim sheet As Excel.Worksheet
    
            If wb Is Nothing Then
                Set wb = ThisWorkbook
            End If
    
            sheetExists = False
            For Each sheet In wb.Worksheets
                If sheet.name = name Then
                    sheetExists = True
                    Exit Function
                End If
            Next sheet
    End Function
    
    
    Sub CreateSheets()
            Dim MyCell As Range
            Dim MyRange As Range
            Dim ws As Worksheet
    
            Set MyRange = Sheets("Sheet1").Range("A2")
            Set MyRange = Range(MyRange, MyRange.End(xlDown))
    
            For Each MyCell In MyRange
                If Sheets(Sheets.Count).name <> MyCell.Value Then
                    Set ws = getSheetWithDefault(MyCell.Value)
                End If
            Next MyCell
        End Sub
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: new sheet creation with specific information

    This part copies the info to the required sheets
    Option Explicit
    Sub CopyInfo()
    Dim wkSht As Worksheet
    Dim cell As Range
    Dim nextRow As Long
    Dim lRow As Long
    Dim i As Integer
    lRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
    Application.ScreenUpdating = False
    For Each wkSht In Sheets
        For i = 2 To lRow
            If Sheets("Sheet1").Range("A" & i).Value = wkSht.name Then
                wkSht.Activate
                nextRow = wkSht.Range("A" & Rows.Count).End(xlUp).Row + 1
                Sheets("Sheet1").Range("A" & i).EntireRow.Copy Destination:=wkSht.Range("A" & nextRow)
            End If
        Next i
    Next wkSht
    Application.ScreenUpdating = True
    End Sub

  4. #4
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: new sheet creation with specific information

    Don't understand this part:
    2) in F column find specific names and in next sheet copy just value (for example if in column F it contains information like "Next business day" then in new sheet it just shows "N2", if Same business day, then "N1"
    1. Copy value in what next sheet and what in new sheet

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: new sheet creation with specific information

    Here is a sample with the result from the above codes. Edit the file by showing a result of what you mean.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    03-24-2017
    Location
    Riga
    MS-Off Ver
    2016
    Posts
    25

    Re: new sheet creation with specific information

    Thank you for fast reply. Great.

    About this one: 2) in F column find specific names and in next sheet copy just value (for example if in column F it contains information like "Next business day" then in new sheet it just shows "N2", if Same business day, then "N1"
    Sorry for not being clear.
    So, if in column F the description contains words "next business day" or "same business day" then in next sheet, which you created with that macros, for example in column L will show value N2 or N1, N2 for Next BD, N1 for Same BD? Hope you understand now.

    And for date the same, that in next sheet in some column it shows value "year" if period from start date and end date is year, "3 years" if calculated period is 3 years.

  7. #7
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: new sheet creation with specific information

    Hi Swapijs

    This code includes the "N1" or "N2" column Populate
    Option Explicit
    Function getSheetWithDefault(name As String, Optional wb As Excel.Workbook) As Excel.Worksheet
            If wb Is Nothing Then
                Set wb = ThisWorkbook
            End If
    
            If Not sheetExists(name, wb) Then
                wb.Worksheets.Add(After:=wb.Worksheets(wb.Worksheets.Count)).name = name
            End If
    
            Set getSheetWithDefault = wb.Sheets(name)
    End Function
    Function sheetExists(name As String, Optional wb As Excel.Workbook) As Boolean
            Dim sheet As Excel.Worksheet
    
            If wb Is Nothing Then
                Set wb = ThisWorkbook
            End If
    
            sheetExists = False
            For Each sheet In wb.Worksheets
                If sheet.name = name Then
                    sheetExists = True
                    Exit Function
                End If
            Next sheet
    End Function

    Sub CreateSheets()
            Dim MyCell As Range
            Dim MyRange As Range
            Dim ws As Worksheet
    
            Set MyRange = Sheets("Sheet1").Range("A2")
            Set MyRange = Range(MyRange, MyRange.End(xlDown))
    
            For Each MyCell In MyRange
                If Sheets(Sheets.Count).name <> MyCell.Value Then
                    Set ws = getSheetWithDefault(MyCell.Value)
                End If
            'Calls Subroutine to populate
            Next MyCell
            Call CopyInfo
        End Sub
    Option Explicit
    Sub CopyInfo()
    Dim wkSht As Worksheet
    Dim cell As Range
    Dim nextRow As Long
    Dim lRow As Long
    Dim i As Integer
    Dim celltxt As String
    Dim celltxtval As String
    
    lRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
    Application.ScreenUpdating = False
    For Each wkSht In Sheets
    Sheets("Sheet1").Activate
        For i = 2 To lRow
            celltxt = Sheets("Sheet1").Range("F" & i)
            If Sheets("Sheet1").Range("A" & i).Value = wkSht.name Then
                Sheets("Sheet1").Activate
                With Sheets("Sheet1")
                    If LCase(InStr(1, celltxt, "Next business day")) Then
                        celltxtval = "N2"
                    Else
                    If LCase(InStr(1, celltxt, "Same business day")) Then
                        celltxtval = "N1"
                    End If
                    End If
                End With
                wkSht.Activate
                nextRow = wkSht.Range("A" & Rows.Count).End(xlUp).Row + 1
                Sheets("Sheet1").Range("A" & i).EntireRow.Copy Destination:=wkSht.Range("A" & nextRow)
                wkSht.Range("L" & nextRow) = celltxtval
           End If
        Next i
    Next wkSht
    Application.ScreenUpdating = True
    End Sub
    I have no time to add the other code...will do so soonest.

  8. #8
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: new sheet creation with specific information

    Herewith the final code for your requirement...The subroutine Call CopyInfo. I am almost certain that there is a simpler way, however, I too am still learning lol.
    Option Explicit
    Sub CopyInfo()
    Dim wkSht As Worksheet
    Dim cell As Range
    Dim nextRow As Long
    Dim lRow As Long
    Dim i As Integer
    Dim celltxt As String
    Dim celltxtval As String
    Dim cellyearval As String
    
    lRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
    Application.ScreenUpdating = False
    For Each wkSht In Sheets
    Sheets("Sheet1").Activate
        For i = 2 To lRow
            celltxt = Sheets("Sheet1").Range("F" & i)
            If Sheets("Sheet1").Range("A" & i).Value = wkSht.name Then
                Sheets("Sheet1").Activate
                With Sheets("Sheet1")
                    If LCase(InStr(1, celltxt, "Next business day")) Then
                        celltxtval = "N2"
                    Else
                    If LCase(InStr(1, celltxt, "Same business day")) Then
                        celltxtval = "N1"
                    End If
                    End If
                End With
                With Sheets("Sheet1")
                    If LCase(InStr(1, celltxt, "3 y")) Then
                        cellyearval = "3 years"
                        Else
                            If LCase(InStr(1, celltxt, "4 y")) Then
                                cellyearval = "4 years"
                                Else
                                If LCase(InStr(1, celltxt, "5 y")) Then
                                    cellyearval = "5 years"
                                End If
                            End If
                     End If
                End With
                    
                wkSht.Activate
                nextRow = wkSht.Range("A" & Rows.Count).End(xlUp).Row + 1
                Sheets("Sheet1").Range("A" & i).EntireRow.Copy Destination:=wkSht.Range("A" & nextRow)
                wkSht.Range("L" & nextRow) = celltxtval
                wkSht.Range("M" & nextRow) = cellyearval
           End If
        Next i
    Next wkSht
    Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    03-24-2017
    Location
    Riga
    MS-Off Ver
    2016
    Posts
    25

    Re: new sheet creation with specific information

    You are beast. Thank you!!!

  10. #10
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: new sheet creation with specific information

    Glad i could help. Thanks for the Rep points.

  11. #11
    Registered User
    Join Date
    03-24-2017
    Location
    Riga
    MS-Off Ver
    2016
    Posts
    25

    Re: new sheet creation with specific information

    Hmmm, but there is some problem with it, when I copy on my original report file.
    Do not understand, what is the problem.
    I am so lost here, can not find anymore, how to add attachment

    ok, so in attachment you can see, that original report is on first sheet, and then I create formated in sheet1 with my macros. And then I want to lunch your macros, but it is not working.
    Attached Files Attached Files
    Last edited by Swapijs; 03-27-2017 at 04:53 AM. Reason: attachment added

  12. #12
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: new sheet creation with specific information

    Hi Swapijs

    Your above attached has no code.I have attached a updated version with the code.....I used Column Q & R to fill N1/N2 and 3Years/5Years etc

    Open file
    Enable Macros
    Press [RUN MACRO]
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 1
    Last Post: 07-07-2016, 01:10 PM
  2. Moving information to another sheet according to specific date
    By angebis in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 04-16-2015, 10:56 AM
  3. Hiding specific column and updating information on another sheet.
    By ajitexcelfourm in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-31-2014, 03:57 AM
  4. Need a range of information to be copied to specific sheet if criteria is met
    By sleepgone in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-07-2014, 10:37 PM
  5. Replies: 3
    Last Post: 12-20-2012, 01:16 AM
  6. Grabbing specific information from another sheet
    By gregbennett81 in forum Excel General
    Replies: 7
    Last Post: 08-24-2011, 07:48 PM
  7. Pulling specific information from a sheet
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-12-2006, 11:30 AM

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