I am trying to write a macro using if then goto offset

svis89

Board Regular
Joined
Oct 19, 2021
Messages
67
Office Version
  1. 365
Platform
  1. Windows
I recently took a job that requires me to brush up on my 12 year old macro skills, i need to check a list for part number then go to the part number and offset to the J column so that I can post the bought values on particular part number in that area so it can all calculate correctly i have a lot of changes to make to my existing macro, i am creating a input sheet for when parts are bought or sold they can input the value hit submit it will then copy value go to count page and find the part number they provided next to value and then offset from the part number to the J column
Bought macro.png
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
do you want it to select a cell if it finds one with the same value as B2 within the range B2:B23?
 
Upvote 0
Try This

Sub Bought_1()
Range("D2").Activate
If ActiveCell = vbNullString Then Exit Sub
Selection.Copy
RngB = Range("B2").Value

Sheets("Count sheet").Activate
For Each cell In Range("B2:B23")
If cell.Value = RngB Then
pp = pp + 1
End If
Next

If pp = 1 Then
Range("J2").Value = RngB
Sheets("Inventory management").Activate
Application.CutCopyMode = False
Range("A2").Copy
Sheets("Count Sheet").Activate
Range("J1").Paste
Columns("J:J").Activate
Application.CutCopyMode = False
Selection.Insert Shift:=xlToRight, Copyorigin:=xlFormatFromLeftOrAbove
Range("J2").Activate
Sheets("Inventory management").Activate
Range("D2").ClearContents
Else
MsgBox "Nothing"

End If

End Sub
 
Upvote 0
Solution
Try This

Sub Bought_1()
Range("D2").Activate
If ActiveCell = vbNullString Then Exit Sub
Selection.Copy
RngB = Range("B2").Value

Sheets("Count sheet").Activate
For Each cell In Range("B2:B23")
If cell.Value = RngB Then
pp = pp + 1
End If
Next

If pp = 1 Then
Range("J2").Value = RngB
Sheets("Inventory management").Activate
Application.CutCopyMode = False
Range("A2").Copy
Sheets("Count Sheet").Activate
Range("J1").Paste
Columns("J:J").Activate
Application.CutCopyMode = False
Selection.Insert Shift:=xlToRight, Copyorigin:=xlFormatFromLeftOrAbove
Range("J2").Activate
Sheets("Inventory management").Activate
Range("D2").ClearContents
Else
MsgBox "Nothing"

End If

End Sub
it is copying B2 instead of D2 so i think i need to add
Sheets("Inventory management").Activate
Range("D2).Activate
Selection.Copy
Sheets("Count sheet").Activate
Range(J2).PasteSpecial


Range("J2").Value = RngB i think this line is the problem

i added clips from sheets so you can see what I'm doing
 

Attachments

  • count sheet.png
    count sheet.png
    22.2 KB · Views: 4
  • Inventory managment sheet.png
    Inventory managment sheet.png
    14.1 KB · Views: 3
Upvote 0
Ok, so you want it to update the values in the other spreadsheet and the old values go to column H onwards?
 
Upvote 0
Sub Bought_1()
Range("D2").Activate
If ActiveCell = vbNullString Then Exit Sub
Selection.Copy
RngB = Range("B2").Value

Sheets("Count sheet").Activate
For Each cell In Range("A2:A23")
If cell.Value = RngB Then
pp = pp + 1
End If
Next

If pp = 1 Then
Sheets("Inventory management").Activate
Range("D2").Activate
Selection.Copy
Sheets("Count sheet").Activate
Range("J2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Inventory management").Activate
Application.CutCopyMode = False
Range("A2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Count sheet").Select
Range("J1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("J:J").Select
Application.CutCopyMode = False
Selection.Insert Shift:=xlToRight, Copyorigin:=xlFormatFromLeftOrAbove
Range("J2").Select
Sheets("Inventory management").Select
Range("D2").Select
Selection.ClearContents
Else
MsgBox "Nothing"

End If

End Sub

this is what i ended up with that solved it all thank you for your help
Ok, so you want it to update the values in the other spreadsheet and the old values go to column H onwards?
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,796
Members
449,095
Latest member
m_smith_solihull

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