using offset when making one cell value equal to another cells value with if statements

drop05

Active Member
Joined
Mar 23, 2021
Messages
285
Office Version
  1. 365
Platform
  1. Windows
Hello, i am trying to figure out why my below code is not taking the offset into effect, it is just

If Source.Worksheets("Sch Q").Range("N17").Value = "- Select -" Then
Target.Worksheets("Sch Q").Range("D15").Offset(, myvalQ).Value = none
ElseIf Source.Worksheets("Sch Q").Range("N17").Value = "GEN" Then
Target.Worksheets("Sch Q").Range("D15").Offset(, myvalQ).Value = "General"
ElseIf Source.Worksheets("Sch Q").Range("N17").Value = "PAS" Then
Target.Worksheets("Sch Q").Range("D15").Offset(, myvalQ).Value = "Passive"
ElseIf Source.Worksheets("Sch Q").Range("N17").Value = "901j" Then
Target.Worksheets("Sch Q").Range("D15").Offset(, myvalQ).Value = "Section 901(j)"
End If

the offset value comes from this code

Dim num_ent As Variant
ent_num = ThisWorkbook.Worksheets("File Paths").Range("d11").Value
Dim myvalQ As Variant
myvalQ = ThisWorkbook.Worksheets("Sheet1").Range("A15").Offset(0, ent_num).Value

the ent num is a number a user enters which gives this offset above, but myvalQ is the number in which the offset in the first chunk of code will follow
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
If Source.Worksheets("Sch Q").Range("N17").Value = "- Select -" Then
Target.Worksheets("Sch Q").Range("D15").Offset(, myvalQ).Value = none
give a column # in the offset - Like if you want to move 1 column forward from Column D

VBA Code:
If Source.Worksheets("Sch Q").Range("N17").Value = "- Select -" Then
Target.Worksheets("Sch Q").Range("D15").Offset(,1).Value = none
 
Upvote 0
give a column # in the offset - Like if you want to move 1 column forward from Column D

VBA Code:
If Source.Worksheets("Sch Q").Range("N17").Value = "- Select -" Then
Target.Worksheets("Sch Q").Range("D15").Offset(,1).Value = none
Thank you for the response, However i am giving a column #, that is what myvalQ is.
The way myvalQ is with this code as posted above as well
Code:

Dim num_ent As Variant
ent_num = ThisWorkbook.Worksheets("File Paths").Range("d11").Value
Dim myvalQ As Variant
myvalQ = ThisWorkbook.Worksheets("Sheet1").Range("A15").Offset(0, ent_num).Value

here a user enters a number on d11 so becomes ent_num
and there is sheet 1 which has a list of offset numbers needed so in this case Q's is on A15 where it just has "Q" on A15, so when the user enters say 1, then the offset will go to B15 and that cell has 0, if 2 is entered then it goes to C15 and in that cell there is 8 and so on, it goes in increments of 8 so d15 will be 16, e15 is 24 etc.

however when i run that code it is not taking in that offset, is there something i did wrong with the code:

If Source.Worksheets("Sch Q").Range("N17").Value = "- Select -" Then
Target.Worksheets("Sch Q").Range("D15").Offset(, myvalQ).Value = none
ElseIf Source.Worksheets("Sch Q").Range("N17").Value = "GEN" Then
Target.Worksheets("Sch Q").Range("D15").Offset(, myvalQ).Value = "General"
ElseIf Source.Worksheets("Sch Q").Range("N17").Value = "PAS" Then
Target.Worksheets("Sch Q").Range("D15").Offset(, myvalQ).Value = "Passive"
ElseIf Source.Worksheets("Sch Q").Range("N17").Value = "901j" Then
Target.Worksheets("Sch Q").Range("D15").Offset(, myvalQ).Value = "Section 901(j)"
End If
 
Upvote 0
Confused.....

I guess there is some problem with this

Dim num_ent As Variant
ent_num = ThisWorkbook.Worksheets("File Paths").Range("d11").Value
Dim myvalQ As Variant
myvalQ = ThisWorkbook.Worksheets("Sheet1").Range("A15").Offset(0, ent_num).Value

try giving a column number instead - and see if this works

also try changing it to

VBA Code:
If Worksheets("Sch Q").Range("N17").Value = "- Select -" Then
Worksheets("Sch Q").Range("D15").Offset(, myvalQ).Value = none
ElseIf Worksheets("Sch Q").Range("N17").Value = "GEN" Then
Worksheets("Sch Q").Range("D15").Offset(, myvalQ).Value = "General"
ElseIf Worksheets("Sch Q").Range("N17").Value = "PAS" Then
Worksheets("Sch Q").Range("D15").Offset(, myvalQ).Value = "Passive"
ElseIf Worksheets("Sch Q").Range("N17").Value = "901j" Then
Worksheets("Sch Q").Range("D15").Offset(, myvalQ).Value = "Section 901(j)"

maybe some other member can help
 
Upvote 0
When you say it is ignoring the offset, what is actually doing ? Outputting to D15 ?
When I run it with Option Explicit the code is actually crashing on = none.
Assuming you want none to appear in the cell it needs to have quote marks around it ie "none"

Can you try putting this before the IF Statement block:-
VBA Code:
    Debug.Print "myvalQ => "; myvalQ, "Source value => "; Source.Worksheets("Sch Q").Range("N17").Value
and see what turns up in the immediate window.
 
Upvote 0

Forum statistics

Threads
1,216,163
Messages
6,129,223
Members
449,495
Latest member
janzablox

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