+ Reply to Thread
Results 1 to 7 of 7

How to make VBA offset and speed up coding

Hybrid View

  1. #1
    Registered User
    Join Date
    07-03-2014
    Location
    Toronto
    MS-Off Ver
    2010
    Posts
    43

    Exclamation How to make VBA offset and speed up coding

    Dear Excel Gods,



    I've pieced together this code from information I have found online and the code works perfectly right now. I originally wanted to make this a user form where it would simply add the values to the next free column but that may be out of my league.

    To help get this project over with I'd like to make this code work for multiple columns with out copying and pasting the code. I think this would be called doing "offset"?

    Right now it works for P1 down.

    If D1 = True, then do the code below for columns D:E
    If F1 = True, then do the code below for columns F:G
    If H1 = True, then do the code below for columns H:I
    If J1 = True, then do the code below for columns J:K
    ect

    I'm VERY new to VBA so please be detailed in your response.


    Thank you!

           
    If .Address = "$P$1" Then
    If LCase(.Value) = "true" Then
    
    ' --------------------Beginning of Formatting Section--------------------
    
    Range("P8:Q12,P14:Q16,P22:Q29").Merge Across:=True
    
       With Range("P8:Q12,P14:Q16,P22:Q29")
            .Borders.LineStyle = xlNone
            For Each v In Array(xlEdgeTop, xlInsideHorizontal, xlEdgeBottom, xlEdgeLeft, xlEdgeRight)
                With .Borders(v)
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                    .ColorIndex = xlAutomatic
                End With
            Next v
        End With
        
        
          With Range("P17:Q21")
            .Borders.LineStyle = xlNone
            For Each v In Array(xlEdgeLeft, xlEdgeRight, xlInsideVertical)
                With .Borders(v)
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                    .ColorIndex = xlAutomatic
                End With
            Next v
             For Each v In Array(xlInsideHorizontal)
                With .Borders(v)
                    .LineStyle = xlDot
                    .Weight = xlThin
                    .ColorIndex = xlAutomatic
                End With
            Next v
        End With
    
    
    With Range("P8:P9")
        .Font.Bold = True
        .Interior.Color = RGB(192, 192, 192)
    End With
    
    
    With Range("P10:P11")
        .Font.Bold = True
        .Interior.Color = RGB(204, 255, 204)
    End With
    
    
    With Range("P12")
        .Font.Bold = True
        .Interior.Color = RGB(146, 206, 220)
    End With
    
    
    With Range("P8:Q29")
    .HorizontalAlignment = xlCenter
    .Font.Name = "Arial"
    .Font.Size = 9
    End With
    
    
    ' --------------------End of Formatting Section--------------------
    
    
    ' --------------------Beginning of Values Section--------------------
    
    
                    
    
    With Range("P8").Validation
     .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, _
     Formula1:="=SourceType"
     .IgnoreBlank = True
     .InCellDropdown = True
     End With
     
     Range("P8").Formula = "Export"
     
     
     With Range("P9").Validation
     .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, _
     Formula1:="=UDB"
     .IgnoreBlank = True
     .InCellDropdown = True
     End With
    
                    Range("P9").Formula = "TCI"
    
                With Range("P14")
                    .Formula = "100"
                    .NumberFormat = "$#,##0.00;-$#,##0.00"
                    .Font.Bold = True
                    .Interior.Color = RGB(204, 255, 204)
                End With
                  
                With Range("P15")
                    .Formula = "=VLOOKUP(P9,Conversion,3,FALSE)"
                    .NumberFormat = "0.00"
                    .Interior.Color = RGB(204, 255, 204)
                End With
                  
                  
                With Range("P16")
                    .Formula = "=P14*P15"
                    .NumberFormat = "$#,##0.00;-$#,##0.00"
                    .Interior.Color = RGB(255, 255, 67)
                    .Font.Bold = True
                End With
                  
    
                    
                    Range("P17").Formula = "0.01%"
                    Range("P18").Formula = "3.36%"
                    Range("P19").Formula = "3.00%"
                    Range("P20").Formula = ".50%"
                    Range("P21").Formula = "6.00%"
                    
                    
                With Range("Q17:Q21")
                    .NumberFormat = "$#,##0.00;-$#,##0.00"
                End With
                    
    
                    Range("Q17").Formula = "=$P$15*P16"
                    Range("Q18").Formula = "=$P$15*P17"
                    Range("Q19").Formula = "=$P$15*P18"
                    Range("Q20").Formula = "=$P$15*P19"
                    Range("Q21").Formula = "=$P$15*P20"
                    
                    
                With Range("P22")
                    .Formula = "=SUM(Q17:Q21)+P15"
                    .NumberFormat = "$#,##0.00;-$#,##0.00"
                    .Interior.Color = RGB(255, 255, 67)
                    .Font.Bold = True
                End With
                
              With Range("P23")
                    .Formula = "=$N$5"
                    .NumberFormat = "$#,##0.00;-$#,##0.00"
                End With
                    
                With Range("P24")
                    .Formula = "=(P26-(P22+P23))/P26"
                    .NumberFormat = "0.00%"
                    .Interior.Color = RGB(255, 255, 67)
                    .Font.Bold = True
                End With
                
                
                
                 With Range("P25")
                    .Formula = "=P26-P22-P26"
                    .NumberFormat = "$#,##0.00;-$#,##0.00"
                End With
                
                
                With Range("P26")
                    .Formula = "100"
                    .NumberFormat = "$#,##0.00;-$#,##0.00"
                    .Font.Bold = True
                    .Interior.Color = RGB(204, 255, 204)
                End With
                
                With Range("P27")
                    .Formula = "=(P29-P26)/P29"
                    .NumberFormat = "0.00%"
                    .Interior.Color = RGB(255, 255, 67)
                    .Font.Bold = True
                End With
                
                
                With Range("P28")
                    .Formula = "=P29-P26"
                    .NumberFormat = "$#,##0.00;-$#,##0.00"
                End With
    
                With Range("P29")
                    .Formula = "=VLOOKUP(P9,Conversion,3,FALSE)*P26"
                    .NumberFormat = "$#,##0.00;-$#,##0.00"
                End With
      
                 
                Else
     
                    Range("P8:P9").Validation.Delete ' replace with the correct formula
                    Range("P8:P29").Formula = ""
                    Range("Q17:Q21").Formula = ""
                    Range("P46:P69").Formula = ""
                    Range("P8:Q29").ClearFormats ' clears all formating
    
            
    End If
    End If
            
            
    ' --------------------End of Values Section--------------------

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

    Re: How to make VBA offset and speed up coding

    Is it the exact same code, except the columns, for each of D1, E1, D1... etc.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    07-03-2014
    Location
    Toronto
    MS-Off Ver
    2010
    Posts
    43

    Re: How to make VBA offset and speed up coding

    It is the same code however some of the formulas need to offset as well

    P:Q Column would be
                With Range("P24")
                    .Formula = "=(P26-(P22+P23))/P26"
                    .NumberFormat = "0.00%"
                    .Interior.Color = RGB(255, 255, 67)
                    .Font.Bold = True
                End With
    R:S Column would be

                With Range("R24")
                    .Formula = "=(R26-(R22+R23))/R26"
                    .NumberFormat = "0.00%"
                    .Interior.Color = RGB(255, 255, 67)
                    .Font.Bold = True
                End With

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

    Re: How to make VBA offset and speed up coding

    So it's just the columns that change, including in the formulas?

  5. #5
    Registered User
    Join Date
    07-03-2014
    Location
    Toronto
    MS-Off Ver
    2010
    Posts
    43

    Re: How to make VBA offset and speed up coding

    I'm 99% that's correct,

    I may be over looking one formula but for the most part that is the idea

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

    Re: How to make VBA offset and speed up coding

    Where is the code located and how is it being executed?

  7. #7
    Registered User
    Join Date
    07-03-2014
    Location
    Toronto
    MS-Off Ver
    2010
    Posts
    43

    Re: How to make VBA offset and speed up coding

    I've attached the file, very new to VBA's so I just want to make sure I'm being clear

    Price Template2.xlsm

+ 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. [SOLVED] Need help on offset and copy coding using VBA
    By megan-jane in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-31-2013, 09:20 AM
  2. Sum(n(offset speed
    By gottscpa in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-17-2013, 10:53 AM
  3. [SOLVED] Offset value to absolute column value - minor coding change
    By ChristineJ in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-08-2012, 12:04 PM
  4. Coding for offset cell references with multiple row headings with merged cells
    By AndyE in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-04-2010, 02:22 PM
  5. [SOLVED] Coding to make a transpose values button
    By Sally in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-30-2006, 06:15 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