+ Reply to Thread
Results 1 to 7 of 7

Combining snippets of VBA to create new sheet and add formatted cells to another page.

Hybrid View

Mr_Bill Combining snippets of VBA to... 09-12-2014, 09:38 PM
AlphaFrog Re: Combining snippets of VBA... 09-13-2014, 03:18 AM
Mr_Bill Re: Combining snippets of VBA... 09-13-2014, 11:17 AM
AlphaFrog Re: Combining snippets of VBA... 09-13-2014, 05:40 PM
Mr_Bill Re: Combining snippets of VBA... 09-13-2014, 05:50 PM
AlphaFrog Re: Combining snippets of VBA... 09-13-2014, 06:27 PM
Mr_Bill Re: Combining snippets of VBA... 09-13-2014, 06:54 PM
  1. #1
    Forum Contributor
    Join Date
    08-14-2014
    Location
    US
    MS-Off Ver
    2013
    Posts
    135

    Combining snippets of VBA to create new sheet and add formatted cells to another page.

    Not honestly certain I will be able to explain this very well but I try. I managed to create my very own code to Create a new sheet, and name the sheet from a userform. Now as far as that part goes I do not need any assistance as of yet. However, I am trying to have 4 items from the userform added to another sheet ("Welcome"). My problem is I cant seem to find the last row and skip a row to input the data and formatting. Not sure what to give here so I will give the three snippets. One is from a recorded macro for the formatting I tried to edit.

    This first one is what I think or thought I had to add in order to get the values from the textbox's to show in the welcome sheet

       With Worksheets("Welcome")
          .Cells(emptyRow, 1).Value = txtTerm.Value
          .Cells(emptyRow, 2).Value = "Course Names:"
          .Cells(emptyRow, 3).Value = txtCourse1.Value
          .Cells(emptyRow, 4).Value = txtCourse2.Value
          .Cells(emptyRow, 5).Value = txtCourse3.Value
       End With

    Here is the recorded macro that I may or not have messed up trying to edit it.

        Sheets("Welcome").Select
        Range("A" & Rows.Count).End(xlUp).Offset(2).Select
        Dim xlEdgeType As Variant
        
        '******* This is a single cell which Should also have Term Name *********
        For Each xlEdgeType In Array(xlEdgeLeft, xlEdgeTop, xlEdgeRight, _
                xlEdgeBottom, xlInsideVertical, xlInsideHorizontal)
            With ActiveSheet.Range("A26").Borders(xlEdgeType)
                .LineStyle = xlContinuous
                .ThemeColor = 2
                .TintAndShade = 0
                .Weight = xlThin
            End With
        '******* This is for the above single cell to color it green *********
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorAccent6
                .TintAndShade = 0.799981688894314
                .PatternTintAndShade = 0
            End With
        Next xlEdgeType
        '******* This is for 4 rows 8 Columns with borders around them *********
        For Each xlEdgeType In Array(xlEdgeLeft, xlEdgeTop, xlEdgeRight, _
                xlEdgeBottom, xlInsideVertical, xlInsideHorizontal)
            With ActiveSheet.Range("A:H").Borders(xlEdgeType)
                .LineStyle = xlContinuous
                .ThemeColor = 2
                .TintAndShade = 0
                .Weight = xlThin
            End With
        Next xlEdgeType
    I need both of snippets somehow combined with this part which is for the form that creates the new worksheet.

    Private Sub shtCreate_Click()
     
     Sheets("MasterSheet").Visible = True
     Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = txtTerm.Value
      
     If SelCourse1.Value = "Weighted" Then
     Sheets("MasterSheet").Columns("G:L").Copy Destination:=Sheets(txtTerm.Value).Range("A1")
     Range("A1").Value = txtCourse1.Text
     Else:
     Sheets("MasterSheet").Columns("A:F").Copy Destination:=Sheets(txtTerm.Value).Range("A1")
     Range("A1").Value = txtCourse1.Text
     End If
     
     If SelCourse2.Value = "Weighted" Then
     Sheets("MasterSheet").Columns("S:X").Copy Destination:=Sheets(txtTerm.Value).Range("G1")
     Range("G1").Value = txtCourse2.Text
     Else:
     Sheets("MasterSheet").Columns("M:R").Copy Destination:=Sheets(txtTerm.Value).Range("G1")
     Range("G1").Value = txtCourse2.Text
     End If
     
     If SelCourse3.Value = "Weighted" Then
     Sheets("MasterSheet").Columns("AE:AJ").Copy Destination:=Sheets(txtTerm.Value).Range("M1")
     Range("M1").Value = txtCourse3.Text
     Else:
     Sheets("MasterSheet").Columns("Y:AD").Copy Destination:=Sheets(txtTerm.Value).Range("M1")
     Range("M1").Value = txtCourse3.Text
     End If
    
     Sheets("MasterSheet").Visible = False
        Unload Me
    End Sub
    Hopefully this all made sense and someone can lend me hand here is the workbook
    https://www.dropbox.com/s/rgliaz7ef1...cept.xlsm?dl=0


    Modified the last piece of code to a more efficient version it copies the corresponding columns and pastes them instead of copying the page and hide the columns I don't need.
    Last edited by Mr_Bill; 09-13-2014 at 12:11 AM.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Combining snippets of VBA to create new sheet and add formatted cells to another page.

    Add something like this...

        Dim NextRow As Long
        
        Sheets("Welcome").Select
        
        NextRow = Range("A" & Rows.Count).End(xlUp).Offset(2).Row
        If NextRow < 8 Then NextRow = 8
        
        '******* This is a single cell which Should also have Term Name *********
        With Range("A" & NextRow)
            With .Borders
                .LineStyle = xlContinuous
                .ThemeColor = 2
                .TintAndShade = 0
                .Weight = xlThin
            End With
            '******* This is for the above single cell to color it green *********
            With .Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorAccent6
                .TintAndShade = 0.799981688894314
                .PatternTintAndShade = 0
            End With
             '******* This is for 4 rows 8 Columns wi (relative to Term Name cell) *********
            With .Range("A2:H5").Borders
                .LineStyle = xlContinuous
                .ThemeColor = 2
                .TintAndShade = 0
                .Weight = xlThin
            End With
            
            'Column Labels
            With .Range("C2:H2")
                .Value = Array("Points Received", "Points Possible", "GPA", "Weighted", "Percentage", "Letter Grade")
                .HorizontalAlignment = xlCenter
            End With
            
            'Data
            .Range("A1").Value = txtTerm.Value
            .Range("A2").Value = "Course Names:"
            .Range("A3").Value = txtCourse1.Value
            .Range("A4").Value = txtCourse2.Value
            .Range("A5").Value = txtCourse3.Value
            
        End With
    Last edited by AlphaFrog; 09-13-2014 at 03:20 AM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    08-14-2014
    Location
    US
    MS-Off Ver
    2013
    Posts
    135

    Re: Combining snippets of VBA to create new sheet and add formatted cells to another page.

    Thank you so much for looking at this and fixing it. I appreciate it.

    If I want to edit this a little futher so that is pastes formulas into the cells under the 'Column Labels I would use With .Range("C3:H3") for the row under the one you made? The three rows of empty cells will all have a formula like ='Fall 2014'!E2 and to write that I thinking will need something like "="txtTerm.Value"!E2" just guessing

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Combining snippets of VBA to create new sheet and add formatted cells to another page.

    Quote Originally Posted by Mr_Bill View Post
    The three rows of empty cells will all have a formula like ='Fall 2014'!E2 and to write that I thinking will need something like "="txtTerm.Value"!E2" just guessing
    You're welcome.

    Very close. Try Something like...
    "=" & txtTerm.Value & "!E2"

    Or this to put the Value instead of a formula
    .Range("C3").Value = Sheets(txtTerm.Value).Range("E2").Value
    Last edited by AlphaFrog; 09-13-2014 at 05:43 PM.

  5. #5
    Forum Contributor
    Join Date
    08-14-2014
    Location
    US
    MS-Off Ver
    2013
    Posts
    135

    Re: Combining snippets of VBA to create new sheet and add formatted cells to another page.

    Thank you again I ended up with this
            With .Range("C3:H3")
                .Value = Array("='" & txtTerm.Value & "'!D2", "='" & txtTerm.Value & "'!E2", "='" & txtTerm.Value & "'!E4", "='" & txtTerm.Value & "'!D3", "='" & txtTerm.Value & "'!E3", "='" & txtTerm.Value & "'!D4")
                .HorizontalAlignment = xlCenter
            End With
    through trial and error found I needed the ' ones also if the sheet name had a space in it.

    Would you happen to know how I could make this line .Range("A1").Value = txtTerm.Value a link to that sheet and go in the same place as it would have if was just the text?

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Combining snippets of VBA to create new sheet and add formatted cells to another page.

    Quote Originally Posted by Mr_Bill View Post
    Would you happen to know how I could make this line .Range("A1").Value = txtTerm.Value a link to that sheet and go in the same place as it would have if was just the text?
    I understand the hyperlink to the sheet part. It don't get the "same place as it would have if was just the text" part.

    This creates a hyperlink to sheet txtTerm.Value Range A1
        ActiveSheet.Hyperlinks.Add Anchor:=.Range("A1"), _
                                   Address:="", _
                                   SubAddress:=Sheets(txtTerm.Value).Range("A1").Address(0, 0, xlA1, True), _
                                   TextToDisplay:=Sheets(txtTerm.Value).Range("A1").Value

  7. #7
    Forum Contributor
    Join Date
    08-14-2014
    Location
    US
    MS-Off Ver
    2013
    Posts
    135

    Re: Combining snippets of VBA to create new sheet and add formatted cells to another page.

    Sorry didnt explain that part better but you got the link to go in the same place the text was. Was using the one line for the Title of that section and was only trying to say I wanted it to be a link is all but you figured it out. made one change as it was using the text from A1 of the new sheet at the display text. Thank you very much I appreciate all you help all your additions are working perfectly.

            ActiveSheet.Hyperlinks.Add Anchor:=.Range("A1"), _
                                   Address:="", _
                                   SubAddress:=Sheets(txtTerm.Value).Range("A1").Address(0, 0, xlA1, True), _
                                   TextToDisplay:=txtTerm.Value

+ 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. Combining cells loses digits in formatted cells
    By marielouise1 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-03-2013, 10:11 AM
  2. Replies: 3
    Last Post: 12-31-2012, 12:03 PM
  3. [SOLVED] Selectively referencing formatted text (bold cells) from one sheet to another
    By Stndsh in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-18-2011, 11:05 PM
  4. [SOLVED] [SOLVED] i formatted an excel sheet, to fit my page, how do i add a page?
    By newnewnew in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 08-08-2006, 10:30 PM
  5. Converting 'General' formatted cells to Text formatted cell using.
    By Zahid Khan in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-12-2005, 04:06 PM

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