I.E A2=D2 on another page i need my macro to go from A2 to J column

svis89

Board Regular
Joined
Oct 19, 2021
Messages
67
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
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

So if A2=RngB then go to J2
if A2:A23= RngB it needs to go to the J column for that row
i was thinking once it identifies the RngB to offset to the J but i could not think of how it could go back to the "Inventory management" copy D4 and then return to the correct cell
 
Last edited by a moderator:
Inventory List.xlsm
BCDEFGHIJMNOTUVW
1Part classificationIn stockboughtSoldDefectBoughtSoldDefect
2Sections0265024002502650265024002400250250
3Buckets0000000
4Wires0000000
50000000
60000000
70000000
80000000
90000000
100000000
110000000
120000000
130000000
140000000
150000000
160000000
170000000
180000000
190000000
200000000
210000000
220000000
230000000
Count sheet
Cell Formulas
RangeFormula
C2:C23C2=[@bought]-[@Sold]-[@Defect]
D2:D23D2=H2
E2:E23E2=M2
F2:F23F2=T2
H2:H23H2=SUM(I2:L2)
M2:M23M2=SUM(N2:R2)
T2T2=SUM(U2:AG2)
T3:T23T3=SUM(X3:X3)
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Which of Bought, Sold, Defect is the column for CngB? also is it the first, last or other instance of the word and also...
Can you also put up a screenshot of your spreadsheet using the boards XL2BB addin rather than an image so we can copy it into Excel please
i added the Mini sheet the problem is that each time i hit submit for bought the column for sold and defect change, i have the same word in the table as headers i can change those to added, removed and scrapped
 
Upvote 0
I need to know what we are looking for in the find (What word and what instance [I am assuming at the moment the last instance]), all the other columns can be referenced by an offset from that column but I need to know what to look for to get the starting column, without that then I can't provide any help.
Am I lookin for "Bought"? and if so what instance?
 
Upvote 0
I need to know what we are looking for in the find (What word and what instance [I am assuming at the moment the last instance]), all the other columns can be referenced by an offset from that column but I need to know what to look for to get the starting column, without that then I can't provide any help.
Am I lookin for "Bought"? and if so what instance?
Sold in the above minisheet it is M1

I Changed E1 to removed from inventory so M1 is the only sold in my sheet now
 
Upvote 0
As a demo how you could reference them...

VBA Code:
Sub Test1()
Dim mycol As Long, CngB As Range

mycol = Rows(1).Find("Sold", , xlValues, , xlByColumns, xlPrevious).Column

Set CngB = Columns(mycol)

MsgBox "Column with Sold is column number " & CngB.Column & vbLf & "Column with Bought is column number " & CngB.Offset(, -5).Column & vbLf & "Column with Defect is column number " & CngB.Offset(, 7).Column
End Sub

or just

Code:
Sub Test1a()
Dim mycol As Long

mycol = Rows(1).Find("Sold", , xlValues, , xlByColumns, xlPrevious).Column

MsgBox "Column with Sold is column number " & mycol & vbLf & "Column with Bought is column number " & Columns(mycol - 5).Column & vbLf & "Column with Defect is column number " & Columns(mycol + 7).Column

End Sub

You could get just the column number with mycol + 7 but using Columns(mycol + 7) demonstrates how to reference the actual column rather than just the column number
 
Upvote 0
As a demo how you could reference them...

VBA Code:
Sub Test1()
Dim mycol As Long, CngB As Range

mycol = Rows(1).Find("Sold", , xlValues, , xlByColumns, xlPrevious).Column

Set CngB = Columns(mycol)

MsgBox "Column with Sold is column number " & CngB.Column & vbLf & "Column with Bought is column number " & CngB.Offset(, -5).Column & vbLf & "Column with Defect is column number " & CngB.Offset(, 7).Column
End Sub

or just

Code:
Sub Test1a()
Dim mycol As Long

mycol = Rows(1).Find("Sold", , xlValues, , xlByColumns, xlPrevious).Column

MsgBox "Column with Sold is column number " & mycol & vbLf & "Column with Bought is column number " & Columns(mycol - 5).Column & vbLf & "Column with Defect is column number " & Columns(mycol + 7).Column

End Sub

You could get just the column number with mycol + 7 but using Columns(mycol + 7) demonstrates how to reference the actual column rather than just the column number
okay i have been trying to write the Dim mycol into a range selection i need to reference RngB to tell the macro what row to paste in then use Dim mycol to tell the macro what column of the row its supposed to paste E2 im making sure we are on same page here.. i thought Dim mycol was for getting references not deciding a range but i havent done anything with macro's since 2008 in highschool so i am very rusty (hmm now where did i lay down that macro)
 
Upvote 0
VBA Code:
Cells(cell.Row, mycol)
gives you the intersect (using cell from my earlier code).
 
Upvote 0
VBA Code:
[/
Sub Sold2()
    Dim cell As Range, RngB
    Dim mycol As Long, CngB As Range
    
    mycol = Rows(1).Find("Sold", , xlValues, , xlByColumns, xlPrevious).Column
    Set CngB = Columns(mycol)
    
    RngB = Sheets("Inventory management").Range("B2").Value


    For Each cell In Sheets("Count sheet").Range("A2:A23")
    
        If cell.Value = RngB Then Cells=(cell.Row , mycol)


    Next
    
    Sheets("Count sheet").Columns(mycol).Insert Shift:=xlToRight, Copyorigin:=xlFormatFromLeftOrAbove
   
    Sheets("Inventory management").Range("E2").ClearContents

End Sub
CODE]
So im getting a syntax error and its telling me i need a ) after cell.Row
 
Upvote 0
Remove the = sign after Cells, I didn't have one there
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,937
Members
449,094
Latest member
teemeren

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