How to have a macro search for a value in an offset cell on one sheet in another sheet - vba

this is my first question so please bear with me if i'm doing anything wrong.
Quick back storey... on sheet "Purchase" in column C i want the item name of what has been purchased, in column D i want the quantity and in column G i want the location of where the item is going. For example i want 10 boxes of tiles delivered to the office. C2=Tiles, D2=10, G2=Office
When the location of an item is entered into column G(cell change) i want the macro to offset to the same row in column C and then search for the value in column A on sheet "Office", i currently have this for searching if a cell changes;
Private Sub worksheet_change(ByVal target As Range)
Dim keycells As String
keycells = "g2:g9999"
If Application.Intersect(ActiveCell, Range(keycells)) = "Office" Then MsgBox "hello"
End Sub
This does work, but it also checks if all other cells change, which i dont want it to do this. In place of "msgbox "hello" " i want to put the script for it to offset to the same row in column C look at the value and then search for this value in column A on sheet "Office".
If it is found i need the macro to add the newly purchased quantity to the current quantity on sheet "Office" (this will be column B), if it is not found i need it add the information from columns C & D on sheet "Purchase" to columns A & B
Also i will need to add more locations to the script and will give these locations a sheet in the workbook over time
If this is possible i will be extremely gratefull for all help provided

The Worksheet_Change event fires on all cell changes.
To prevent your code running unless the cell is in column G, you could restrict it like this:
Private Sub worksheet_change(ByVal target As Range)
Dim keycells As String
Dim iSect
If target.Column = 7 Then
keycells = "g2:g9999"
Set iSect = Application.Intersect(target, Range(keycells))
If Not iSect Is Nothing Then
If lcase(target) = "office" Then
MsgBox "hello"
' add code here to copy the data
End If
End If
End If
End Sub
However, a better solution might be to use formulas (such as an array formula or using INDEX and MATCH) in the target worksheets to extract the data - this will avoid using a macro entirely.

Related

find text in column in one sheet and copy row data to another sheet

I have a spreadsheet with customer information that I want to search on by last name. I want to enter the last name on a separate sheet (Sheet 1) and have the macro search the Last Name column in the customer data spreadsheet (Sheet 2). When it finds a match, I want it to copy the entire row in Sheet 2 and paste it to a specific row in Sheet 1. I've searched a number of sites and tried numerous versions of code but cannot get it to work.
Here's a link that shows you how to get data from another sheet or workbook. Basically you use Sheet_name!Cell_address or Sheet_name!First_cell:Last_cell.
Hope this helps :)
I think this sounds simple enough, loop until you find the value you want. How do you want the trigger to fire? the below into sheet 2 into the will trigger after double clicking on a selected cell in column 1, will prompt you for input, then copy the first match.
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column <> 1 Then Exit Sub 'or which ever column you enter for
Dim str_Act, str_Test As String
Dim i As Integer
'find value to search
str_Act = InputBox("Enter User Last Name")
If str_Act = "" Then Exit Sub
'loop to find search
Do While str_Act <> str_Test
str_Test = Sheets(1).Range("A1").Offset(i, 0) ' or whichever column has your value
i = i + 1
Loop
'Copy and paste
Sheets(1).Range("A1:ZZ1").Offset(i - 1, 0).Copy
Target.PasteSpecial
End Sub

Excel VBA Compare cell value to list and overwrite value in separate sheet

In a workbook I have, users either manually enter an account code or select one from a list and the account codes are placed in column C (C7:C446) in a sheet called "JE". The account codes look like this ####### - ### - ## - ######. In column D (D7:D446) in sheet "JE", there is a formula that captures the last 6 digits of the account code. In a sheet called "required_refs", there is a list of 6 digit codes in column A. If the value in the D column in sheet "JE" equals any of the values in column A of "required_refs" sheet, I would like the value in the D column cell to overwrite the cell value in cell D1 in a separate sheet called "references" (I know that may have been confusing, sorry)
Example: if the value of D25 matches any of the values listed in column A of sheet "required_refs", upon double clicking a red colored F25 cell, put the value of D25 (of sheet "JE"), and put it in cell D1 on sheet "references".
I've taken a crack at it as best I know how. I've placed this code in sheet JE:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim project As Range: Set project = Range("D7:D446")
Dim param As Range: Set param = Worksheets("references").Range("D1").Value
For Each cell In project
If project.Value = Worksheets("required_refs").Range("A:A").Value Then
Call gotoRef_ 'macro that simply selects/navigates to the required_ref sheet
project.Value = param
End If
End Sub
Thanks so much in advance for any suggestions on how to complete this. I can elaborate on this further if needed.
This will do what you want:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("F7:F446")) Is Nothing Then Exit Sub
Dim varReference As Variant
varReference = Columns("D").Cells(Target.Row).Value2
If Not IsError(Application.Match(varReference, Worksheets("required_refs").Columns("A"), 0)) Then
Worksheets("references").Range("D1").Value = varReference
End If
End Sub
Important Points:
Whenever working with event handlers, always limit the scope of the target range in the first line. Otherwise, it might not work correctly or it could slow done your spreadsheet.
Make sure your JE sheet column D values and required_refs sheet column A values are all either text or numbers. Otherwise the values won't be compared correctly.
Note the usage of Application.Match() instead of WorksheetFunction.Match() to access the worksheet function. This, coupled with the use of a Variant type variable, allows us to trap the error that occurs if the match fails.
You can always do this on the sheet. Consider the MATCH function. See here for how to use MATCH.
Or another great tool if you're searching for something in a table associated with a value in another column (not your case I don't think)--VLOOKUP formula. Place this formula in the D cell of the sheet you want to place the numbers in. VLOOKUP is in the following format:
=vlookup(lookup value,table_array,column index number, [range lookup])
The lookup value is the 6 digit code you're looking for (on the JE sheet)
The table_array is simply selecting the values you want to search for (required_refs sheet)
The column index number would be one, since the table only has 1 column. It's basically the column number of the value you're looking for.
And range lookup is for if you think there might be more than one place where it matches.
For your case I think it would look like this:
=vlookup('JE'!D1,'required_refs'!A1:A,1,FALSE)
Then just lock the values you want to keep and click and drag down.
Explanation for VLOOKUP here

VBA copy value of selected cell in specific column

I've been looking for a way to code in VBA: when I click on a certain cell in range X10:X5000, Excel should copy that cell value to a specific cell B10.
Currently I have the following, but it only works when the cell I select is an actual value. When the cell I select is the outcome of a formula, the target cell shows the value for a fraction of a second and then the cell becomes blank.
So how can I make sure I always copy the value of the selected cell and not the formula? In the example, I did not take into account the range, but only a selected column.
Thanks for the help!
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 19 Then ' check if cell modifed is in column "S"
Target.Copy Range("B10")
End If
End Sub
Thanks!
Try using
Range("B10").Value = Target.Value
instead of
Target.Copy Range("B10")
This should transfer the value across, instead of the formula

Dynamically, continously, set a cell's value according to ActiveCell's value (which is on another worksheet)

I have a workbook with a two sheets, Rep and Aux.
I want to dynamically set Aux!A2 to the value of the ActiveCell, which is on sheet Rep, but only if the ActiveCell is on column D of that sheet (in the range Rep!D2:D5000).
To top it all of I need this mechanism to run as long as the workbook is active, not just a one-shot.
For example: While being on sheet Rep I place the cursor, i.e. ActiveCell on cell D2. I expect Aux!A2 to be set to the value of Rep!D2. I move the cursor to, say, Rep!F5 and expect nothing to happen to Aux!A2, lastly, I activate cell Rep!D7 and again, expect Aux!A2 to get the ActiveCell's value. Continue till I close the workbook.
My VBA skills are non-existent and Googling, the only thing remotely close to what I described was:
Sub Macro1()
If Not Intersect(ActiveCell, Sheets("Rep").Range("D2:D5000")) Is Nothing Then Sheets("Aux").Range("A2").Value = ActiveCell.Value
End Sub
Which fails completely.
Put this in the code of the "Rep" worksheet. Triggers anytime a cell is selected on that sheet, if the cell is in column 4 (D) then it sets the value of the cell on Aux to match.
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Target.Column = 4 Then
ThisWorkbook.Worksheets("Aux").Cells(2, 1).value = Target.Value
End If
End Sub
EDIT: In response to comments.
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
End Sub
This subroutine is an event that exists on every worksheet. Any time a selection changes it will run any code you put in it. The "ByVal Target as Excel.Range" part is saying it's giving you a copy of the target range being selected, because you could select more then one cell.
If Target.Column = 4 Then
end if
This is an If Block. If the condition is true, any code between the "Then" and the "End If" will execute. The condition is if the target's column is 4 in this case.
ThisWorkbook.Worksheets("Aux").Cells(2, 1).value = Target.Value
This sets the cell at row 2 column 1 value to match the value of the target that was selected.
Now that I think about it I wonder what this code will do if you select a range of cells.....

Excel Drop Down Box with Formula

Hi I have an excel with a drop down box whose list has 3 cells. One of these cells contain a formula. The problem is this formula is dependent on data in another cell and when this data changes the calculated value changes. The value is automatically update in the list where it was chosen from but I will manually have to go back to the drop down box and change it. How can I have the value be updated automatically. Willing to look at a VBA solution if need be
Put the following into the worksheet module. It assumes that the cell with validation applied is G9, and the second option of the list is the formula.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "G9" Then
If Target.HasFormula Then Exit Sub 'or else infinite loop
Dim ListRange As Range
Dim FoundIdx As Variant
Set ListRange = Me.Evaluate(Me.Range("G9").Validation.Formula1)
FoundIdx = Application.Match(Target.Value, ListRange, 0)
If Not IsError(FoundIdx) Then
If FoundIdx = 2 Then
Target.Formula = ListRange(2).Formula
End If
End If
End If
End Sub
Note that this will not work if the formula might have the same value as any of the other options!
I couldn't reproduce your issue. Here is what I did:
Populate 2 cells with a random value and a third cell with a formula (columns B3, B4, B5)
Define a Name Range with these 3 cells called it Options
Create a drop down using Insert/Form Controls/Combo Box
Set the input range of the drop down to Options
Change the cells value to get the formula give different results and the new values are reflected on the Options list and the Drop Down.
Is this what you are doing?