+ Reply to Thread
Results 1 to 2 of 2

Command button to add rows, copy and paste formulas, and format.

  1. #1
    Registered User
    Join Date
    06-05-2013
    Location
    Colorado
    MS-Off Ver
    Excel 2010
    Posts
    1

    Command button to add rows, copy and paste formulas, and format.

    Hi all, I searched around a bit before posting and couldn't find my problem, sorry if this can be found elsewhere.


    Problem 1: A command button within sheet 1 will not make changes in sheet 2.

    Currently, my code looks like this, tied to a button in sheet 1:
    [vb code]
    Private Sub CommandButton1_Click()

    'This inserts a new row
    Sheets("sheet 1").Range("D9").Select
    ActiveCell.EntireRow.Insert shift:=xlDown

    'This formats the cells
    Sheets("sheet 1").Range("C9:AG9").Select
    Selection.Borders.Weight = xlThin
    With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorAccent3
    .TintAndShade = 0.599993896298105
    .PatternTintAndShade = 0
    End With

    'This is where it stops working, but is the same code just a different sheet
    Sheets("sheet 2").Range("D9").Select

    ActiveCell.EntireRow.Insert shift:=xlDown

    'This formats the cells

    Sheets("sheet 2").Range("C9:AG9").Select

    Selection.Borders.Weight = xlThin
    With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorAccent3
    .TintAndShade = 0.599993896298105
    .PatternTintAndShade = 0
    End With

    End Sub
    [\vb]

    Problem 2: I also tried to write a macro that copies and pastes my formulas into a new row, but it's not working.

    [vb code]
    Dim y As Long
    y = Application.InputBox("How many rows do you want to add?", "", Type:=1)
    If y = 0 Then Exit Sub
    With Sheets("sheet 3").[a65536].End(9).EntireRow.Copy.Resize(y).Insert
    End With
    Application.CutCopyMode = False

    End Sub
    [/vb]


    Any help would be appreciated.
    Last edited by seankuus; 06-05-2013 at 07:38 PM.

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

    Re: Command button to add rows, copy and paste formulas, and format.

    Problem 1: two issues
    1. To select a cell on a non-active sheet, first select the sheet. Then select the cell
    2. You don't have to .Select a sheet or cell to copy\paste\insert with it.

    Try this...
    Please Login or Register  to view this content.
    Problem 2:
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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