+ Reply to Thread
Results 1 to 3 of 3

Applying VBA logic to other cells

Hybrid View

  1. #1
    Registered User
    Join Date
    03-19-2024
    Location
    USA
    MS-Off Ver
    2024
    Posts
    5

    Applying VBA logic to other cells

    Hi all,

    So I am stuck on how to go about finishing this sheet I created. The logic of this sheet works like this:

    Ok, I have wrote a section of code that is fairly long and includes many different cells. My question is this: Can I use the VBA code that I have written and push that code to another cell (just the logic of how it works)? I do not just want to simply copy and paste the logic into a cell, but I want the logic to be changed to look at the cells and reformat all the cells to match the new location.

    Here is part of my VBA code below. It basically consist of cell D21 with a dropdown selection of 1,2, or 3. It merges and changes cells based on the selection made in the dropdown. However, I want to move the "logic" part of it to D28. So now D21, if not touched, would not be merged with any other cells. Now I go to the next (D28) option and maybe I want that to be a 3 slot breaker. It would use the logic to merge all three together and so on. Doing this the "old fashion" way, I would have to go through the entire code and change each cell to it's appropriate new location. This is EXTREMELY time consuming and leads to a lot of error. There HAS to be an easier way to achieve this. I want the code to change based on the new location. So this is why a simple copy and paste will not work. Can this be done?




    When a "1" is selected in cell D22 it will show the following:

    1710168662782.png

    When a "2" is selected in cell D22 it will show the following:

    1710168803695.png

    When a "3" is selected in cell D22 it will show the following:

    1710168855472.png

  2. #2
    Registered User
    Join Date
    03-19-2024
    Location
    USA
    MS-Off Ver
    2024
    Posts
    5

    Re: Applying VBA logic to other cells

    This is only a part of my code however you can get an idea how I am doing it.

    Private Sub sub4(ByVal Target As Range)
        If (Target.Address = "$D$22") And IsNumeric(Target.Value) Then
            Application.DisplayAlerts = False
    
            ' Unmerge cells before handling each case
            Range("C22:C40").UnMerge
            Range("D22:D40").UnMerge
            Range("C27:C28").Interior.ColorIndex = xlNone
            Range("C34:C35").Interior.ColorIndex = xlNone
    
            Select Case Target.Value
                Case 1
                    HandleCase1
                Case 2
                    HandleCase2
                Case 3
                    HandleCase3
                    
            End Select
    
            Application.DisplayAlerts = True
        End If
    End Sub
       
    
    
    Private Sub HandleCase1()
        
        ' Merges and unmerges cells based on Case1 being selected
        
        Range("C22:C26").Merge
        Range("C22").Value = "-"
        Range("C22:C26").Borders(xlEdgeBottom).LineStyle = xlContinuous
        Range("C22:C26").Borders(xlEdgeBottom).Weight = xlMedium
        Range("C22:C26").Borders(xlEdgeLeft).LineStyle = xlContinuous
        Range("C22:C26").Borders(xlEdgeLeft).Weight = xlMedium
        Range("C22:C26").Borders(xlEdgeRight).LineStyle = xlContinuous
        Range("C22:C26").Borders(xlEdgeRight).Weight = xlMedium
        Range("C27:C28").Interior.ColorIndex = xlNone
    
        
        Range("C29:C33").Merge
        Range("C29").Value = "-"
        Range("C29:C33").Borders(xlEdgeTop).LineStyle = xlContinuous
        Range("C29:C33").Borders(xlEdgeTop).Weight = xlMedium
        Range("C29:C33").Borders(xlEdgeBottom).LineStyle = xlContinuous
        Range("C29:C33").Borders(xlEdgeBottom).Weight = xlMedium
        Range("C29:C33").Borders(xlEdgeLeft).LineStyle = xlContinuous
        Range("C29:C33").Borders(xlEdgeLeft).Weight = xlMedium
        Range("C29:C33").Borders(xlEdgeRight).LineStyle = xlContinuous
        Range("C29:C33").Borders(xlEdgeRight).Weight = xlMedium
        
        Range("C36:C40").Merge
        Range("C36").Value = "-"
        Range("C36:C40").Borders(xlEdgeTop).LineStyle = xlContinuous
        Range("C36:C40").Borders(xlEdgeTop).Weight = xlMedium
        Range("C36:C40").Borders(xlEdgeLeft).LineStyle = xlContinuous
        Range("C36:C40").Borders(xlEdgeLeft).Weight = xlMedium
        Range("C36:C40").Borders(xlEdgeRight).LineStyle = xlContinuous
        Range("C36:C40").Borders(xlEdgeRight).Weight = xlMedium
        Range("C34:C35").Interior.ColorIndex = xlNone
    
        
        Range("D22:D33").UnMerge
        Range("D22:D26").Merge
        Range("D27:D28").Interior.ColorIndex = xlNone
        Range("D34:D35").Borders(xlEdgeLeft).LineStyle = xlNone
        Range("D34:D35").Borders(xlEdgeRight).LineStyle = xlNone
        
        Range("C27:K28").Borders(xlEdgeLeft).LineStyle = xlNone
        Range("C27:K28").Borders(xlEdgeRight).LineStyle = xlNone
        Range("K27:K28").Borders(xlEdgeLeft).LineStyle = xlNone
        Range("D27:D28").Borders(xlEdgeLeft).LineStyle = xlNone
        Range("D27:D28").Borders(xlEdgeRight).LineStyle = xlNone
        
        Range("C34:K35").Borders(xlEdgeLeft).LineStyle = xlNone
        Range("C34:K35").Borders(xlEdgeRight).LineStyle = xlNone
        Range("K34:K35").Borders(xlEdgeLeft).LineStyle = xlNone
        
        Range("D22:D26").Borders(xlEdgeBottom).Weight = xlMedium
        Range("D22:D26").Borders(xlEdgeBottom).LineStyle = xlContinuous
        Range("D22:D26").Borders(xlEdgeBottom).Weight = xlMedium
        Range("D22:D26").Borders(xlEdgeLeft).LineStyle = xlContinuous
        Range("D22:D26").Borders(xlEdgeLeft).Weight = xlMedium
        Range("D22:D26").Borders(xlEdgeRight).LineStyle = xlContinuous
        Range("D22:D26").Borders(xlEdgeRight).Weight = xlMedium
        Range("D29:D33").Borders(xlEdgeTop).LineStyle = xlContinuous
        Range("D29:D33").Borders(xlEdgeTop).Weight = xlMedium
        Range("D29:D33").Borders(xlEdgeLeft).LineStyle = xlContinuous
        Range("D29:D33").Borders(xlEdgeLeft).Weight = xlMedium
        Range("D29:D33").Borders(xlEdgeRight).LineStyle = xlContinuous
        Range("D29:D33").Borders(xlEdgeRight).Weight = xlMedium
        Range("D29:D33").Merge
        If Range("D29").Value = "" Then Range("D29").Value = 1
        Range("D34:D35").Interior.ColorIndex = xlNone
        
        Range("D36:D40").Merge
        Range("D29:D33").Borders(xlEdgeBottom).LineStyle = xlContinuous
        Range("D29:D33").Borders(xlEdgeBottom).Weight = xlMedium
        Range("D29:D33").Borders(xlEdgeTop).LineStyle = xlContinuous
        Range("D29:D33").Borders(xlEdgeTop).Weight = xlMedium
        Range("D29:D33").Borders(xlEdgeLeft).LineStyle = xlContinuous
        Range("D29:D33").Borders(xlEdgeLeft).Weight = xlMedium
        Range("D29:D33").Borders(xlEdgeRight).LineStyle = xlContinuous
        Range("D29:D33").Borders(xlEdgeRight).Weight = xlMedium
        Range("D36:D40").Borders(xlEdgeTop).LineStyle = xlContinuous
        Range("D36:D40").Borders(xlEdgeTop).Weight = xlMedium
        Range("D36:D40").Borders(xlEdgeLeft).LineStyle = xlContinuous
        Range("D36:D40").Borders(xlEdgeLeft).Weight = xlMedium
        Range("D36:D40").Borders(xlEdgeRight).LineStyle = xlContinuous
        Range("D36:D40").Borders(xlEdgeRight).Weight = xlMedium
        If Range("D36").Value = "" Then Range("D36").Value = 1
        
        Range("E22:K33").UnMerge
        Range("E22:K26").Merge
        Range("E22:K26").Borders(xlEdgeBottom).LineStyle = xlContinuous
        Range("E22:K26").Borders(xlEdgeBottom).Weight = xlMedium
        Range("E22:K26").Borders(xlEdgeLeft).LineStyle = xlContinuous
        Range("E22:K26").Borders(xlEdgeLeft).Weight = xlMedium
        Range("E22:K26").Borders(xlEdgeRight).LineStyle = xlContinuous
        Range("E22:K26").Borders(xlEdgeRight).Weight = xlMedium
        
        Range("E29:K33").Merge
        Range("E29:K33").Borders(xlEdgeTop).LineStyle = xlContinuous
        Range("E29:K33").Borders(xlEdgeTop).Weight = xlMedium
        Range("E29:K33").Borders(xlEdgeLeft).LineStyle = xlContinuous
        Range("E29:K33").Borders(xlEdgeLeft).Weight = xlMedium
        Range("E29:K33").Borders(xlEdgeRight).LineStyle = xlContinuous
        Range("E29:K33").Borders(xlEdgeRight).Weight = xlMedium
        Range("E29:K33").Borders(xlEdgeBottom).LineStyle = xlContinuous
        Range("E29:K33").Borders(xlEdgeBottom).Weight = xlMedium
        If Range("E29").Value = "" Then Range("E29").Value = ""
        
        Range("E36:K40").Merge
        Range("E36:K40").Borders(xlEdgeTop).LineStyle = xlContinuous
        Range("E36:K40").Borders(xlEdgeTop).Weight = xlMedium
        Range("E36:K40").Borders(xlEdgeLeft).LineStyle = xlContinuous
        Range("E36:K40").Borders(xlEdgeLeft).Weight = xlMedium
        Range("E36:K40").Borders(xlEdgeRight).LineStyle = xlContinuous
        Range("E36:K40").Borders(xlEdgeRight).Weight = xlMedium
        If Range("E36").Value = "" Then Range("E36").Value = ""
        
        Range("N22:N33").UnMerge
        Range("N22:N26").Merge
       
        Range("N22:N26").Merge
        Range("N22").Borders(xlEdgeTop).LineStyle = xlContinuous
        Range("N22").Borders(xlEdgeTop).Weight = xlThin
        Range("N26").Borders(xlEdgeBottom).LineStyle = xlContinuous
        Range("N26").Borders(xlEdgeBottom).Weight = xlThin
        Range("N27:N28").Interior.ColorIndex = xlNone
                
        Range("N29:N33").Merge
        Range("N29").Borders(xlEdgeTop).LineStyle = xlContinuous
        Range("N29").Borders(xlEdgeTop).Weight = xlThin
        Range("N33").Borders(xlEdgeBottom).LineStyle = xlContinuous
        Range("N33").Borders(xlEdgeBottom).Weight = xlThin
        Range("N34:N35").Interior.ColorIndex = xlNone
        
        Range("N36:N40").Merge
        Range("N36").Borders(xlEdgeTop).LineStyle = xlContinuous
        Range("N36").Borders(xlEdgeTop).Weight = xlThin
        Range("N40").Borders(xlEdgeBottom).LineStyle = xlContinuous
        Range("N40").Borders(xlEdgeBottom).Weight = xlThin
        Range("N36:N40").Borders(xlEdgeLeft).LineStyle = xlContinuous
        Range("N36:N40").Borders(xlEdgeLeft).Weight = xlThin
        Range("N36:N40").Borders(xlEdgeRight).LineStyle = xlContinuous
        Range("N36:N40").Borders(xlEdgeRight).Weight = xlThin
        Range("N41:N42").Interior.ColorIndex = xlNone
        
        Range("M22:M40").UnMerge
        Range("M22:M26").Merge
        Range("M29:M33").Merge
        Range("M36:M40").Merge
        
        
        Range("M22").Borders(xlEdgeTop).LineStyle = xlContinuous
        Range("M22").Borders(xlEdgeTop).Weight = xlThin
        Range("M26").Borders(xlEdgeBottom).LineStyle = xlContinuous
        Range("M26").Borders(xlEdgeBottom).Weight = xlThin
           
        Range("M29").Borders(xlEdgeTop).LineStyle = xlContinuous
        Range("M29").Borders(xlEdgeTop).Weight = xlThin
        Range("M33").Borders(xlEdgeBottom).LineStyle = xlContinuous
        Range("M33").Borders(xlEdgeBottom).Weight = xlThin
           
        Range("M36").Borders(xlEdgeTop).LineStyle = xlContinuous
        Range("M36").Borders(xlEdgeTop).Weight = xlThin
        Range("M40").Borders(xlEdgeBottom).LineStyle = xlContinuous
        Range("M40").Borders(xlEdgeBottom).Weight = xlThin
        
        ' Insert bullet symbol in the range W23:X24 in Excel
        Range("W23:X24").Value = ChrW(&H2022)
        ' Remove bullet symbol from the ranges AA30 in Excel
        Range("AA30").Value = ""
        ' Remove bullet symbol from the ranges AA30 in Excel
        Range("AE37").Value = ""
        
        Range("AA30:AB31").UnMerge
        Range("AB37:AC38").UnMerge
        Range("AE37:AF38").UnMerge
        
        ' Add extra bold inside lines to the range AE37:AF38 in Excel
        Dim targetRange As Range
        Set targetRange = Union(Range("AA30:AB31"), Range("AE37:AF38"))
        
        ' Add extra bold inside vertical line
        With targetRange.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThick ' Adjust the thickness as needed
        End With
        
        ' Add extra bold inside horizontal line
        With targetRange.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThick ' Adjust the thickness as needed
        End With
        
         ' Set targetRange for the second range (T31:U34)
        Set targetRange2 = Range(("T31:U34"), ("T24:U27"))
        
        ' Remove the vertical line from the second range
        With targetRange2.Borders(xlInsideVertical)
            .LineStyle = xlNone
        End With
           
        ' Check if cell N22 is blank
    If Range("N22").Value = "" Then
        ' If N22 is blank, set default value to "15 AMP"
        Range("N22").Value = "15 AMP"
    End If
        ' Check if cell N29 is blank
    If Range("N29").Value = "" Then
        ' If N29 is blank, set default value to "15 AMP"
        Range("N29").Value = "15 AMP"
    End If
        ' Check if cell N36 is blank
    If Range("N36").Value = "" Then
        ' If N36 is blank, set default value to "15 AMP"
        Range("N36").Value = "15 AMP"
    End If
    
        ' Restore data validation for cells D29 & D36
        With Range("D29,D36").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Operator:=xlBetween, Formula1:="1,2,3"
            .IgnoreBlank = True
            .InCellDropdown = True
            .ShowInput = True
            .ShowError = True
        End With
     
        
        ' Restore data validation for cell N29 & N36
        With Range("N29,N36").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Operator:=xlBetween, Formula1:="15 AMP,20 AMP,25 AMP,30 AMP,40 AMP,45 AMP,50 AMP,60 AMP,70 AMP,80 AMP,90 AMP,100 AMP,110 AMP,125 AMP,150 AMP,175 AMP,200 AMP,225 AMP,250 AMP,300 AMP,350 AMP,400 AMP,N/A"
            .IgnoreBlank = True
            .InCellDropdown = True
            .ShowInput = True
            .ShowError = True
        End With
        
        ' Ensure Target is properly declared
    Dim Target As Range
    Set Target = Range("$D$22")
    
    
    
    End Sub
    
    
    Private Sub HandleCase2()
    
        ' Merges and unmerges cells based on Case2 being selected
        
        Range("C22:C33").Merge
        Range("C36:C40").Merge
        Range("C36").Value = "-"
        Range("C22:C33").Borders(xlEdgeBottom).LineStyle = xlContinuous
        Range("C22:C33").Borders(xlEdgeBottom).Weight = xlMedium
        Range("C22:C33").Borders(xlEdgeLeft).LineStyle = xlContinuous
        Range("C22:C33").Borders(xlEdgeLeft).Weight = xlMedium
        Range("C22:C33").Borders(xlEdgeRight).LineStyle = xlContinuous
        Range("C22:C33").Borders(xlEdgeRight).Weight = xlMedium
        Range("C34:C35").Borders(xlEdgeLeft).LineStyle = xlNone
        Range("C36:C40").Borders(xlEdgeTop).LineStyle = xlContinuous
        Range("C36:C40").Borders(xlEdgeTop).Weight = xlMedium
        Range("C36:C40").Borders(xlEdgeLeft).LineStyle = xlContinuous
        Range("C36:C40").Borders(xlEdgeLeft).Weight = xlMedium
        Range("C36:C40").Borders(xlEdgeRight).LineStyle = xlContinuous
        Range("C36:C40").Borders(xlEdgeRight).Weight = xlMedium
        
        Range("D22:D33").Merge
        Range("D34").UnMerge
        Range("D36:D40").UnMerge
        
        Range("D22:D33").Merge
        Range("D22:D33").Borders(xlEdgeBottom).LineStyle = xlContinuous
        Range("D22:D33").Borders(xlEdgeBottom).Weight = xlMedium
        Range("D22:D33").Borders(xlEdgeLeft).LineStyle = xlContinuous
        Range("D22:D33").Borders(xlEdgeLeft).Weight = xlMedium
        Range("D22:D33").Borders(xlEdgeRight).LineStyle = xlContinuous
        Range("D22:D33").Borders(xlEdgeRight).Weight = xlMedium
        Range("D34:D35").Interior.ColorIndex = xlNone
        
        Range("D36:D40").Merge
        Range("D36:D40").Borders(xlEdgeTop).LineStyle = xlContinuous
        Range("D36:D40").Borders(xlEdgeTop).Weight = xlMedium
        Range("D36:D40").Borders(xlEdgeLeft).LineStyle = xlContinuous
        Range("D36:D40").Borders(xlEdgeLeft).Weight = xlMedium
        Range("D36:D40").Borders(xlEdgeRight).LineStyle = xlContinuous
        Range("D36:D40").Borders(xlEdgeRight).Weight = xlMedium
        If Range("D36").Value = "" Then Range("D36").Value = 1
        
        Range("E22:K33").Merge
        Range("E22:K40").UnMerge
     
    End Sub

  3. #3
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,651

    Re: Applying VBA logic to other cells

    With 57 views and no one helping, even though this request isn't difficult to solve using VBA.
    You should attach an example file, remembering to remove any sensitive data.
    Quang PT

+ 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] Applying Predetermined Formatting/Logic to a Downloaded Table
    By lemoncells in forum Excel General
    Replies: 10
    Last Post: 12-05-2023, 11:01 PM
  2. [SOLVED] Sum of cells by logic in a row
    By chintakk in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-16-2020, 10:59 AM
  3. Applying IF logic as VBA down whole column for 5000 rows
    By Ravenous in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-19-2017, 10:11 PM
  4. Search Surrounding Cells and Use Logic Table to Highlight cells
    By Hekagigantes in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-14-2014, 08:36 AM
  5. Replies: 9
    Last Post: 11-20-2013, 09:37 PM
  6. [SOLVED] Sum cells after applying a formula
    By JMarchante in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-08-2013, 07:56 AM
  7. Applying same calculation on various cells
    By AndyOne in forum Excel General
    Replies: 2
    Last Post: 03-09-2005, 09:56 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