VBA question

drmidnight

New Member
Joined
Jun 27, 2018
Messages
5
Hoping for help with this, which is probably obvious to someone who knows more vba than me. I am v much a beginner learning as i go. My excel sheet 1 is connected to an api, so using Private Sub Worksheet_Change(ByVal Target As Range), and With Target.Parent etc.
From sheet 1 I want to for example query the value of cell C2 in sheet 4 and if true place a different value in cell D1 in sheet 1. Have tried vba along the lines of but no joy with:
If Worksheets("Sheet4").Range("C2").Value = 10 Then
.Range("D1").Value = 15
End If

Project explorer is set to show sheet 4 as Sheet4 (Data)
Is this relevant. ? Would really appreciate help with the correct code on this.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
If Sheet4 (Data) is what you see, Sheet4 is the codename property value for that sheet. It doesn't change if you change the sheet name/caption (what you see on the sheet tab). Use "Data" in this case.
AFAIK, you cannot start a line with .Range unless you've started a With block before that, otherwise vba does not know what .Range refers to.
You would have to write something like
VBA Code:
With Worksheets.("Data")
  If .Range("C2"). = 10 Then .Range("D1") = 15
End With
or
If Worksheets("Data").Range("C2") = 10 Then Worksheets("Data").Range("C2") = 15
There are also other ways, such as creating a variable that you set to a sheet object. I usually don't do that if I'm only going to use the variable 1 or 2 times.
 
Upvote 0
Can you show the complete code for your Sub Worksheet_Change?

(submitted 1 microsecond :biggrin:after Micron's suggestion)
 
Upvote 0
Micron and ant, Many thnx for making time to reply. Yes My code is within a With block
With Target.Parent
' main code here
End With

So if c2 in my data sheet =10 then D1 in my active sheet will become = 15. Thie code will be
If Worksheets("Data").Range("C2") = 10 Then
.Range("D1").Value = 15
End If
It seems to work
Thnx again for your help.
 
Upvote 0

Forum statistics

Threads
1,214,807
Messages
6,121,679
Members
449,047
Latest member
notmrdurden

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