VBA Help - Have code apply to specific cell range

surkdidat

Well-known Member
Joined
Oct 1, 2011
Messages
582
Office Version
  1. 365
Good afternoon

I have the following code which presently populates any value put in column B down a certain amount of lines, however, I need this only to apply to a certain range (from B75 to B1500)

Can I ask what I need to amend to apply to only a certain cell range please?

Also, I have a Macro (currently called Button9_Click ) that is currently affected by this, and comes up with a RunTime Error 13 - Type Mismatch) - this code worked perfectly fine before the below code was added to the worksheet - again can anyone advise the best way around this please?


(Code for the above:-

If Target.Count = 1 And Target.Column = 2 And Target <> "" Then
Application.EnableEvents = False
Range(Target.Offset(1, 0), Target.Offset(13, 0)) = Target
Application.EnableEvents = True

End If
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
You did not provide the entire script.

You should show us both scripts.

I suppose the code you showed us is a sheet change event of some type

Is there some reason why you do not want to show us the entire script?
 
Upvote 0
Apologies, the clear sheet script is as follows:-

Sub CALCULATOR_Button9_Click()
Range("B15:N65").Select
ActiveWorkbook.Worksheets("CALCULATOR").Sort.SortFields.CLEAR
ActiveWorkbook.Worksheets("CALCULATOR").Sort.SortFields.Add Key:=Range( _
"B16:B75"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("CALCULATOR").Sort
.SetRange Range("B15:d75")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Range("C9").Select
Selection.ClearContents
Range("c11").Select
Selection.ClearContents
Range("L8").Select
Selection.ClearContents
Range("L9").Select
Selection.ClearContents
Range("L10").Select
Selection.ClearContents




Range("E10").Select
Selection.ClearContents
Range("e12").Select
Selection.ClearContents

Range("O11").Select
Selection.ClearContents
Range("O12").Select
Selection.ClearContents
Range("N66").Select
Selection.ClearContents
Range("c16").Select


Range("c9").Select
End Sub

The code already provided is in its entirety, and sits on the worksheet itself.

You did not provide the entire script.

You should show us both scripts.

I suppose the code you showed us is a sheet change event of some type

Is there some reason why you do not want to show us the entire script?
 
Upvote 0
These two lines of code show the range:
Code:
"[COLOR=#ff0000]B16:B75[/COLOR]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("CALCULATOR").Sort
        .SetRange Range("[COLOR=#ff0000]B15:d75[/COLOR]")

You said:

I have the following code which presently populates any value put in column B down a certain amount of lines,

This script does not populate any thing.

It does a sort and then clears several ranges.
 
Upvote 0
Apologies, I think I am confusing issues with two problems

The below script (which is in full) when attached to the worksheet itself, will auto-populate whatever value is put into column B 13 times. So if you typed A into cell B1, then it would put A in allc ells in column B from B2 to B14.

What I need is the below code to be amended so that it will only work from B75 onwards. So if you entered anything in cells B1 to B74 then the code would not run, and if you entered it in any cell (in column B) from B75 and downwards, then it would autopopulate a further 13 times.

(I will forget the other issue with the longer "Clear" code for now to avoid confusing matters!!!

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count = 1 And Target.Column = 2 And Target <> "" Then
Application.EnableEvents = False
Range(Target.Offset(1, 0), Target.Offset(13, 0)) = Target
Application.EnableEvents = True

End If
End Sub

Apologies, the clear sheet script is as follows:-



The code already provided is in its entirety, and sits on the worksheet itself.
 
Last edited:
Upvote 0
When posting code please use code tags rather than quotes. It's the # icon in the reply window.
For your event code (assuming it's a worksheet_change event) try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("B75:B1500")) Is Nothing Or Target.CountLarge > 1 Then Exit Sub
    If Target <> "" Then
        Application.EnableEvents = False
        Range(Target.Offset(1, 0), Target.Offset(13, 0)) = Target
        Application.EnableEvents = True
    End If
End Sub
And for your button code add these 2 lines one at the very top & the other at the bottom.
Code:
        Application.EnableEvents = False
        Application.EnableEvents = True
 
Upvote 0
Try this:
See added code marked in red.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count = 1 And Target.Column = 2 And Target <> "" And Target.Row [COLOR=#ff0000]> 74 [/COLOR]Then
Application.EnableEvents = False
Range(Target.Offset(1, 0), Target.Offset(13, 0)) = Target
Application.EnableEvents = True
End If
End Sub
 
Upvote 0
You asked for B74 and onward

So I used >74 which does what you want

This line of code provided by others
Range("B75:B1500"))

Only allows it to work from row 75 to row 1500.

I saw no such request.
 
Upvote 0
It came from the 2nd line of the op
I need this only to apply to a certain range (from B75 to B1500)
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,578
Members
449,174
Latest member
chandan4057

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