How to combine Two VBA into one

dharv3y78

New Member
Joined
Oct 26, 2019
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hi MrExcel Form,

I have a issue where i have two excel VBA code that work fine independently one is a Private Sub and the other is a Public Sub, the issue is that when i try to add the Public Sub inside of Private Sub using a Call SubName it crashes excel or does not work correctly.

What i have is a spreadsheet in where if Cells in Column D equal True (by a Checkbox) then in column E it will clear the contents and i'm able to put in my own rate but if column D equals FALSE it will insert the rate from column F, now the issue that i have is that when i select the right dropdown in column B the rate shows in column F but won't put the data in column E until i have check the checkbox twice is which is why i have used a CopyPaste code that will run manually but i want it to all run fluently and automatically. Is there a way that this can happen without crashing the app.

The code i'm using are.
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    On Error GoTo CleanUp
    Application.EnableEvents = False
    If Not Intersect(Target, Range("D2").EntireColumn) Is Nothing Then
        Set rng = Intersect(Target, Range("D2").EntireColumn)
        rng.Offset(0, 1).ClearContents
        If rng.Value = False Then
            rng.Offset(0, 1).Value = rng.Offset(0, 2).Value
        End If
    End If
CleanUp:
    Application.EnableEvents = True
End Sub

VBA Code:
Public Sub CopyPaste()
    Dim Cell As Range
    Dim rngSource As Range
    Dim rngTarget As Range

    ' Define the source and target ranges
    Set rngSource = Range("F4:F11")
    Set rngTarget = Range("E4:E11")
    
    ' Loop through each cell in the source range
    For Each Cell In rngSource
        ' Check if the cell contains a number
        If IsNumeric(Cell.Value) Then
            ' Copy the value from column F to column E
            Cells(Cell.Row, "E").Value = Cells(Cell.Row, "F").Value
        End If
    Next
End Sub

Any help you can give or provide the correct code would be appreciated.
Thanks
DHarv3y78
 

Attachments

  • Screenshot 2025-03-04 192745.png
    Screenshot 2025-03-04 192745.png
    23 KB · Views: 1
Hello @dharv3y78. try the following untested one, maybe it will work
VBA Code:
    ' ''''''''''''''''''''''''''''''''''''''''''''
    ' Here above is the rest of your code
    ' Call CopyPaste after handling column D logic
    Call CopyPaste

CleanUp:
    
    ' Ensure events are re-enabled
    Application.EnableEvents = True
End Sub
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top