Merging two worksheet change codes,now confused

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,737
Office Version
  1. 2007
Platform
  1. Windows
Hi,

I have two codes which both need to be under the Private Sub Worksheet_Change(ByVal Target As Range)

Like normal ive got into a mess & now confused.
Could you advise where ive gone wrong please.

Thanks.
Merged code supplied below

Code:
Private Sub Worksheet_Change(ByVal Target As Range)If Target.Address = "$A$6" Then
Application.EnableEvents = False
Dim CustomerName As String
Worksheets("DATABASE").Select
CustomerName = Range("A6")
Worksheets("INFO").Select
Worksheets("INFO").Range("CF2").Select
If Worksheets("INFO").Range("CF2").Offset(1, 0) <> "" Then
Worksheets("INFO").Range("CF2").End(xlDown).Select
End If
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = CustomerName
ActiveCell.Interior.ColorIndex = 6
ActiveCell.HorizontalAlignment = xlCenter
ActiveCell.VerticalAlignment = xlBottom
ActiveCell.VerticalAlignment = xlCenter
Selection.Borders.LineStyle = xlContinuous
ActiveCell.RowHeight = 19.5
ActiveCell.Font.Bold = True
Worksheets("DATABASE").Select
Application.EnableEvents = True
Else
With Target
        If .Column = 13 Then Exit Sub
        If .Count = 1 And Not .HasFormula Then
            Application.EnableEvents = False
            .Value = UCase(.Value)
            Application.EnableEvents = True
            End If
End With
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
What is the name of the worksheet that hosts the code?
 
Upvote 0
DATABASE

I merged the two codes incorrectly.
When i leave cell A6 the text should be forced to upper case & the code run.
 
Upvote 0
See if this is what you want.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target.Address = "$A$6" Then
    Dim CustomerName As String
    CustomerName = Target.Value
    With Worksheets("INFO").Range("CF2").Cells(Rows.Count, "CF").End(xlUp)(2)
        .Value = CustomerName
        .Interior.ColorIndex = 6
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .VerticalAlignment = xlCenter
        .Borders.LineStyle = xlContinuous
        .RowHeight = 19.5
        .Font.Bold = True
    End With
Else
    With Target
            If .Column = 13 Then
                Application.EnableEvents = True
                Exit Sub
            End If
            If .Count = 1 And Not .HasFormula Then
                    .Value = UCase(.Value)
            End If
    End With
End If
Application.EnableEvents = True
End Sub
 
Upvote 0
That pops up a run time error 1004
For the 6th line of code.

If you can merge these two below problem solved.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$6" Then
Application.EnableEvents = False
Dim CustomerName As String
Worksheets("DATABASE").Select
CustomerName = Range("A6")
Worksheets("INFO").Select
Worksheets("INFO").Range("CF2").Select
If Worksheets("INFO").Range("CF2").Offset(1, 0) <> "" Then
Worksheets("INFO").Range("CF2").End(xlDown).Select
End If
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = CustomerName
ActiveCell.Interior.ColorIndex = 6
ActiveCell.HorizontalAlignment = xlCenter
ActiveCell.VerticalAlignment = xlBottom
ActiveCell.VerticalAlignment = xlCenter
Selection.Borders.LineStyle = xlContinuous
ActiveCell.RowHeight = 19.5
ActiveCell.Font.Bold = True
Worksheets("DATABASE").Select
Application.EnableEvents = True
End If
End Sub







Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    With Target
        If .Column = 13 Then Exit Sub
        If .Count = 1 And Not .HasFormula Then
            Application.EnableEvents = False
            .Value = UCase(.Value)
            Application.EnableEvents = True
        End If
    End With
End Sub

Thanks
 
Upvote 0
Try:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim r   As Range
    
    Application.EnableEvents = False
    
        If Target.Address = "$A$6" Then
            With sheets("INFO").Range("CF2")
                If Len(.Offset(1).Value) Then
                    Set r = .End(xlDown).Offset(1)
                    With .End(xlDown).Offset(1)
                        .Value = ActiveSheet.Cells(6, 1).Value
                        .Interior.ColorIndex = 6
                        .HorizontalAlignment = xlCenter
                        .VerticalAlignment = xlBottom
                        .VerticalAlignment = xlCenter
                        .Borders.LineStyle = xlContinuous
                        .RowHeight = 19.5
                        .Font.Bold = True
                    End With
                End If
             End With
        End If
        
    With Target
        If .Column <> 13 And .Count = 1 And Not .HasFormula Then
            Application.EnableEvents = False
            .Value = UCase$(.Value)
            Application.EnableEvents = True
        End If
    End With
        
    Application.EnableEvents = True
    
End Sub
 
Upvote 0
Great,works a treat.

Now i dont see all the flashing pages just a quick copy / paste.


One thing i did notice is when the value from cell A6 is pasted to the INFO sheet its small case & would like it to be upper case.

So currently cell A6 reads tom jones,when you leave that cell it changes to TOM JONES which is correct.
I then go to the INFO sheet expecting it to also be TOM JONES but its tom jones.

Could you advise please.
Many thansk
 
Upvote 0
That pops up a run time error 1004
For the 6th line of code.

That line should read
Code:
With Worksheets("INFO").Cells(Rows.Count, "CF").End(xlUp)(2)

Sorry about that.
 
Upvote 0
Try:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim r   As Range
    
    Application.EnableEvents = False
    
        If Target.Address = "$A$6" Then
            With sheets("INFO").Range("CF2")
                If Len(.Offset(1).Value) Then
                    Set r = .End(xlDown).Offset(1)
                    With .End(xlDown).Offset(1)
                        .Value = UCase$(ActiveSheet.Cells(6, 1).Value)
                        .Interior.ColorIndex = 6
                        .HorizontalAlignment = xlCenter
                        .VerticalAlignment = xlBottom
                        .VerticalAlignment = xlCenter
                        .Borders.LineStyle = xlContinuous
                        .RowHeight = 19.5
                        .Font.Bold = True
                    End With
                End If
             End With
        End If
        
    With Target
        If .Column <> 13 And .Count = 1 And Not .HasFormula Then
            Application.EnableEvents = False
            .Value = UCase$(.Value)
            Application.EnableEvents = True
        End If
    End With
        
    Application.EnableEvents = True
    
End Sub
The flashing pages are due to using .Select to activate sheets. It's just a property of the sheet object and usually not needed
i.e.
Code:
Sheets("Sheet1").Range("A1").Value = 1
And
Code:
Sheets("Sheet1").Select
Range("A1").Value = 1
Will both put 1 into cell A1 of Sheet1 but the second version makes Sheet1 visible for the users benefit, has no impact on the value of the cell or its contents.
 
Upvote 0
Thanks,
The code in the last post now transfers it in uppercase.

My last goal for tonight is once the code has been pasted to the INFO sheet it needs to be sorted A-Z
Cell range is CF2 then down the page

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

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