+ Reply to Thread
Results 1 to 5 of 5

selecting spreadsheet instead of specific columns

Hybrid View

  1. #1
    Registered User
    Join Date
    10-19-2012
    Location
    Virginia
    MS-Off Ver
    Excel 2010
    Posts
    23

    selecting spreadsheet instead of specific columns

    Hello,

    I am new to macros and currently working on a file that is an export from another application. The macro is basically to add lines, reformat, etc. While it is not complicated (for those who are familiar with macros) it does have a lot of steps.

    The issue I am having is with trying to format the columns as percentage, currency, etc. When I run the macro as is, it selects columns A-J, instead of just the individual column. At the end of that section, however, the formatting for columns K - N are fine. The section in red is the section I am having issues with.
    Any help will be greatly appreciated!!

    Sub Salesreports()
    '
    Rows("2:3").EntireRow.Delete
    Columns("C:C").EntireColumn.Delete
    Columns("E:E").Insert Shift:=xlToRight
    Columns("H:H").Insert Shift:=xlToRight
    Cells.Select
    
    Rows("1:2").Select
        Selection.UnMerge
        Columns("A:B").Select
        Selection.Hyperlinks.Delete
    
    With Selection.Font
        .Name = "Verdana"
        .Size = 10
    End With
    
        
    Range("E3").Select
        ActiveCell.FormulaR1C1 = "Single Qty - Variance"
        Range("H3").Select
        ActiveCell.FormulaR1C1 = "Sales - Variance"
        Range("K3").Select
        ActiveCell.FormulaR1C1 = "Cost - Variance"
        Range("L3").Select
        ActiveCell.FormulaR1C1 = "Profit- Current"
        Range("M3").Select
        ActiveCell.FormulaR1C1 = "Profit - Prior"
        Range("N3").Select
        ActiveCell.FormulaR1C1 = "Profit - Variance"
        Range("N2").Select
    
    Rows("3:3").Select
    With Selection
        .WrapText = True
    
    Columns("K:K").Select
    Selection.ColumnWidth = 9
    
    Columns("A:N").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ThemeColor = 1
        .TintAndShade = -0.14996795556505
        .Weight = xlThin
    End With
    
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ThemeColor = 1
        .TintAndShade = -0.14996795556505
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ThemeColor = 1
        .TintAndShade = -0.14996795556505
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ThemeColor = 1
        .TintAndShade = -0.14996795556505
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ThemeColor = 1
        .TintAndShade = -0.14996795556505
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ThemeColor = 1
        .TintAndShade = -0.14996795556505
        .Weight = xlThin
    End With
    
    Range("H4").Select
        ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-1])/RC[-1]"
        Range("K4").Select
        ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-1])/RC[-1]"
        Range("L4").Select
        ActiveCell.FormulaR1C1 = "=RC[-6]-RC[-3]"
        Range("M4").Select
        ActiveCell.FormulaR1C1 = "=RC[-6]-RC[-3]"
        Range("N4").Select
        ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-1])/RC[-1]"
        Range("E4").Select
        ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-1])/RC[-1]"
        Range("C4").Select
        Selection.NumberFormat = "#,##0"
        Range("D4").Select
        Selection.NumberFormat = "#,##0"
    End With
    
    Columns("E:E").Select
        Selection.NumberFormat = "0.00%"
        Columns("F:G").Select
        Selection.NumberFormat = "$#,##0.00_);($#,##0.00)"
        Columns("H:H").Select
        Selection.NumberFormat = "0.00%"
        Columns("I:J").Select
        Selection.NumberFormat = "$#,##0.00_);($#,##0.00)"
        Columns("K:K").Select
        Selection.NumberFormat = "0.00%"
        Columns("L:M").Select
        Selection.NumberFormat = "$#,##0.00_);($#,##0.00)"
        Columns("N:N").Select
        Selection.NumberFormat = "0.00%"Range("A1:N1").Select
        With Selection
        .HorizontalAlignment = xlCenter
    End With
        Selection.Merge
        
    Rows("1:1").Select
        With Selection.Font
            .Size = 12
    End With
        Selection.RowHeight = 30
    End Sub

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,199

    Re: selecting spreadsheet instead of specific columns

    Hi, ds0919,

    instead of
    Columns("E:E").Select
        Selection.NumberFormat = "0.00%"
        Columns("F:G").Select
        Selection.NumberFormat = "$#,##0.00_);($#,##0.00)"
        Columns("H:H").Select
        Selection.NumberFormat = "0.00%"
        Columns("I:J").Select
        Selection.NumberFormat = "$#,##0.00_);($#,##0.00)"
        Columns("K:K").Select
        Selection.NumberFormat = "0.00%"
        Columns("L:M").Select
        Selection.NumberFormat = "$#,##0.00_);($#,##0.00)"
        Columns("N:N").Select
        Selection.NumberFormat = "0.00%"
    try
        Range("E:E,H:H,K:K,N:N").NumberFormat = "0.00%"
        Range("F:G,I:J,L:M").NumberFormat = "$#,##0.00_);($#,##0.00)"
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    10-19-2012
    Location
    Virginia
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: selecting spreadsheet instead of specific columns

    Holger,

    That worked perfectly!!!

    Can you tell me if you see other areas that I could condense? I am trying to learn and am doing so one step at a time.

    Thanks for your timely and accurate response!!

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,199

    Re: selecting spreadsheet instead of specific columns

    Hi, ds0919,

    as your macro is mostly code from the macro recorder you can avoid the typical going of the recorder: select an item and then work on it. And there is at least one line of code that I sort of dislike (Merge).

    untested, an option would be to use BorderAround for the outside borders of the range:
    Sub Salesreports()
    '
    Rows("2:3").EntireRow.Delete
    Columns("C:C").EntireColumn.Delete
    Columns("E:E").Insert Shift:=xlToRight
    Columns("H:H").Insert Shift:=xlToRight
    
    Rows("1:2").UnMerge
    Columns("A:B").Hyperlinks.Delete
    
    With Cells.Font
        .Name = "Verdana"
        .Size = 10
    End With
    
        
    Range("E3").Value = "Single Qty - Variance"
    Range("H3").Value = "Sales - Variance"
    Range("K3").Value = "Cost - Variance"
    Range("L3").Value = "Profit- Current"
    Range("M3").Value = "Profit - Prior"
    Range("N3").Value = "Profit - Variance"
    
    Rows("3:3").WrapText = True
    
    Columns("K:K").ColumnWidth = 9
    
    With Columns("A:N")
      .Borders(xlDiagonalDown).LineStyle = xlNone
      .Borders(xlDiagonalUp).LineStyle = xlNone
      With .Borders(xlEdgeLeft)
          .LineStyle = xlContinuous
          .ThemeColor = 1
          .TintAndShade = -0.14996795556505
          .Weight = xlThin
      End With
      
      With .Borders(xlEdgeTop)
          .LineStyle = xlContinuous
          .ThemeColor = 1
          .TintAndShade = -0.14996795556505
          .Weight = xlThin
      End With
      With .Borders(xlEdgeBottom)
          .LineStyle = xlContinuous
          .ThemeColor = 1
          .TintAndShade = -0.14996795556505
          .Weight = xlThin
      End With
      With .Borders(xlEdgeRight)
          .LineStyle = xlContinuous
          .ThemeColor = 1
          .TintAndShade = -0.14996795556505
          .Weight = xlThin
      End With
      With .Borders(xlInsideVertical)
          .LineStyle = xlContinuous
          .ThemeColor = 1
          .TintAndShade = -0.14996795556505
          .Weight = xlThin
      End With
      With .Borders(xlInsideHorizontal)
          .LineStyle = xlContinuous
          .ThemeColor = 1
          .TintAndShade = -0.14996795556505
          .Weight = xlThin
      End With
    End With
    
    Range("H4").FormulaR1C1 = "=(RC[-2]-RC[-1])/RC[-1]"
    Range("K4").FormulaR1C1 = "=(RC[-2]-RC[-1])/RC[-1]"
    Range("L4").FormulaR1C1 = "=RC[-6]-RC[-3]"
    Range("M4").FormulaR1C1 = "=RC[-6]-RC[-3]"
    Range("N4").FormulaR1C1 = "=(RC[-2]-RC[-1])/RC[-1]"
    Range("E4").FormulaR1C1 = "=(RC[-2]-RC[-1])/RC[-1]"
    Range("C4").NumberFormat = "#,##0"
    Range("D4").NumberFormat = "#,##0"
    
    Range("E:E,H:H,K:K,N:N").NumberFormat = "0.00%"
    Range("F:G,I:J,L:M").NumberFormat = "$#,##0.00_);($#,##0.00)"
    
    With Range("A1:N1")
        .HorizontalAlignment = xlCenter
        .Merge
    End With
        
    With Rows("1:1")
        .Font.Size = 12
        .RowHeight = 30
    End With
    
    End Sub
    Ciao,
    Holger

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: selecting spreadsheet instead of specific columns

    What exactly is the problem?

+ 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