Excel 2010 VBA - Search Active Column for "Delete" then "Add/Retain" - excel

I am very new to VBA and need some guidance here. I have found little snippets here and there but can't seem to figure out how to place them all together.
The overall script looks at a specific cell to see what store number is being worked on, then goes to a different sheet to find that number in row 2. On this other sheet, Column C contains all possible items for the stores in the region. Once the store number is located in row 2, that column is now important as it lists "Delete" or "Add/Retain" for the previously mentioned items listed in Column C.
At the point in my script where I am at, I have the header cell active, on sheet2, that is the store number. Below it lists whether the products going down column C should be "Delete" or "Add/Retain".
I am looking for help on how to search the active column for "Delete" and "Add/Retain" then momentarily transfer the product number from column C to a variable that can be used externally.
For example, let's say the store number corresponded to Group 3. This is located in Column F on Sheet2. Below it is a specific/unique set of "Delete" or "Add/Retain". The first item down says "Delete", so the script should go to column C in which it will find item number "51308". It sets a variable to this value then passes it to my external program where it searches the database for that number (51308) and removes it since it was a "Delete". It writes this action to a text file "Deleted item 51308 from Group 3". Now the script moves down another row and there is "Add/Retain" listed. Back to column C (product list) and it lists "74063". The temporary variable gets set to this new product number, gets passed to my program, the database is checked to see if the number is currently there, if it is, nothing happens, if it isn't, it gets added and the text file is written a new line of "Added 74063 to Group 3".
That's it. Easy right!?
This is a screenshot of the Sheet2 I was mentioning.
So far I have this:
Dim rng1 As Range
Dim strSearch As String
strSearch = "Delete"
Set rng1 = Range("D:D").Find(strSearch, , xlValues, xlWhole)
If Not rng1 Is Nothing Then
Set rng2 = rng1.Offset(0, -1)
MsgBox rng2
MsgBox "Find has matched " & strSearch & vbNewLine & "Corresponding cell is " & rng1.Offset(0, -1)
Else
MsgBox strSearch & " not found"
End If
The script finds the first "Delete", then goes back one column to column C and displays the item number. I could then pass this on to my program no problem.
This is ok except the store group could be anywhere on Row 2 (not just column D).

Experiment first with the use of the Match and index functions. The match function to locate the row the matching store number is found on, and the index function to return the values from each of the other columns. this would allow you to join the data from two sheets onto your first worksheet for processing.
By putting the match function on sheet1 to locate the matching row on sheet2, you can then write your vba to loop down the column with the match function in it to then read across the columns to check the values or assign them elsewhere. Using loops vs. the Find function will allow you to navigate from one cell to the next efficiently and overcomes the limitation of not being able to easily find the next value you are looking for.

Related

Emtpy a Cell If another Cell becomes empty

Good day dear community,
I currently have a problem with VBA/Excel that I can't find a solution to. What I want to achieve is not complicated, but I can't find a way.
Let's assume we have two columns. In any row of one column A the User enter a value and then I start a macro. This macro executes certain instructions. Among other things, this macro ensures that if a cell in column A has a value, then the value "Yes" is entered in the same row in column B. Now my problem: As soon as the user deletes the cell value in column A, the value "Yes" in column B should also be deleted. At first glance you might think that i can use this confdition:
=IF(A1="";"";yes)
The problem is that as soon as the user has entered a value in the cell, "yes" is immediately written in the cell, but this is not desired. Because this task should be taken over by the macro.
As a small side note: I have simplified my problem. Due to the structure of my project, only the macro is allowed to write "yes".
Thanks.
Evaluate Excel Formula in VBA
In your code, you will define the occupied range in column A, and apply the second line appropriately.
Option Explicit
Sub checkColumnRange()
' Some code
' Define the column range...
' e.g.:
Dim rg As Range: Set rg = Range("A1:A10")
rg.Offset(, 1).Value = Evaluate("IF(" & rg.Address(0, 0) _
& "<>"""",""Yes"","""")")
' Some code
End Sub

How to reassign individual rows among multiple worksheets based on multiple cell values

I have four table-formatted worksheets (A, B, C, and D) each with rows of different data beneath matching column headers. I'm trying to write a VBA macro to cut rows from one sheet and paste them into the bottom of another based on the value of a single cell in a given column. I know this has been done from one individual sheet to another and I found the basic code to do so. However, I wanted to modify that to apply across all the worksheets, account for multiple permutations of the same value, and avoid re-checking rows that are already/have been placed in their "proper" worksheet, i.e., all rows marked 'Archived' in all its various permutations in column 17 of their respective other worksheets get shuffled over to the 'D' worksheet once the word archived is typed into that cell and the enter key is pressed. I think I'm screwing up something basic here with the "<>" and "or" portions, but not sure how to fix them. I'm also not sure if it is better to link this command to the 'Enter' keystroke or just have it check the values automatically?
Sub MoveRows()
Dim k As String
a = Worksheets(k).Cells(Rows.Count,1).End(xlUp).Row
If Worksheet.Name <> "D" Then
For i = 2 To a Step -1
If Worksheets(k).Cell(i,17).Value = "Archived" or "archived" or "ARCHIVED" Then
Worksheets(k).Rows(i).Cut
Worksheets("D").Range("A1").End(xldown).Offset(1,0).Insert
End If
next i
Else 'do nothing'
End Sub

find a specific header and insert a row above it and insert some value

I am trying to find a specific header and trying to insert a row above it and insert some value
for example sheet called "System" in (A1:Q1) if its find header as "assignee" in G column, it should inert a row above it and paste value as " Open with assignee" in G column
in the same way it should check entire G column and if its finds next assignee it should insert a row above it and paste value as " Open with customer " in G column
can anyone help me with vba code
For the 1st part of the query (inserting a row above the header):
One thing you could do, if this is just a single sheet, is to name the range in which the headers exist (so, if Row 1 is where all the headers are normally, say A1:Q1, then name A1:Q1 as HeaderRow or similar). This has the advantage that, when you insert rows you don't have to put in any code to reassign the header row. Now, rather than looking through an entire sheet, you're focusing your search on a single defined range.
Once this is done, simply use For Each [cell] in [range].Cells to iterate through each cell, and determine if the cell's contents are what you're looking for. If so, you can insert the row above, and populate the cell that is [cell].Row - 1 from your target cell.
It'd look something like this:
Dim InternalCell as Range, myNamedRange as Range
Dim SearchTerm as String
SearchTerm = "assignee"
Set myNamedRange.Cells = ThisWorkbook.Sheets("System").Range("HeaderRows")
For Each InternalCell In myNamedRange.Cells
If InternalCell.Value = SearchTerm Then
myNamedRange.Insert Shift:=xlDown
ThisWorkbook.Sheets("System").cells(MyNamedRange.Row - 1, InternalCell.Column).Value = "Open with assignee"
End If
Next InternalCell

Copy a row of data once column A matches a USERFORM textbox

Been searching around and got a bit confused.
I have a sheet (herein called "ModelSpec") filled with data of TVs with a header that starts at A5:Z5 (Example Header Title is "Part Number", "Brand", "Size", "Resolutions"...etc). Some are with data, some are blank cells.
I'd created some USERFORM to allow user to select how they want to search for their Model Specification.
What I'm trying to do now is to pick up a data ("Part Number") located in the USERFORM textbox that the user selected and do a search in the "ModelSpec" sheet for the Part Number (located in Column A6:A?). The number of rows of data may change and I guess we need a FOR loop to stop at empty rows.
Once found, the entire row will be copied and paste value only on A1:Z1 of the same ModelSpec sheet.
So basically I'm currently working on Private Sub within the FORMS and not MODULES.
From there I have another code to extract the details to a form else where.
Please help this confused man.
This is a starting point for you, and has all the elements you'd need to do what you are asking.
What's Happening:
Click event on the userForm launches this code
Get the last row of the sheet containing data
Loop through all rows
Compare the value of txtPartNumber to the value in Column A of the current row in the loop.
If a match is found, display message confirming and copy the row being searched to row 1 of the same sheet.
If no match found, display message.
notes:
There is no handling of user input error. No upper case to lower case, or dropdown box limiting the user's choices. A good thing to do (in another question) would be to create a dynamic drop down or comboBox, that is populated by already existing part numbers, limiting the user input error.
If there is more than one match on the page, it will loop through all of the rows and copy the match to row 1. Then copy over row 1 with any match found after that, so the LAST one will be the only one you see. If you want it to only match the FIRST, then include an End statement right after copying the search row to row 1. I'm assuming your search column has unique IDs / part numbers.
Code:
Private Sub cmdSearch_Click()
Dim lastRow As Long, lCol As Long, lRow As Long
Dim sName As String
sName = "ModelSpec"
lastRow = Sheets(sName).Range("A" & Rows.count).End(xlUp).row
For lRow = 6 To lastRow
'Check to see if A(lRow) = TextBox. Exact match required
If Sheets(sName).Cells(lRow, "A").Text = txtPartNumber.Text Then
MsgBox("Match Found for Part #: " & txtPartNumber.Text)
For lCol = 1 To 26 'Loop through columns A-Z, Copy lRow to Row 1
Sheets(sName).Cells(1, lCol) = Sheets(sName).Cells(lRow, lCol)
Next lCol
Else
MsgBox("No match found for Part #: " & txtPartNumber.Text)
End If
Next lRow
End Sub

Search for a Cell in Excel using VBA

Problem:
I would like to find a value of a cell next to or below the cell content a text value for a workbook.
Example:
In Sheet2, I have two cells stand random (assume its index is unknown and total is not a defined name)
I want to search for the value "200" stands next to the total and put it in sheet 2 (an active cell). In case, there are multiple cell that contains the word "Total" list all of them and if possible, put the name of the sheet that contains the cell that I am looking for. Value 200 Sheet2
My Approach:
1. User input
Go to each cell and search for it. This will take time if search for the whole limitation of cell in excel. So the search only limit to 100 columns x 10000 rows.
After find its index, offset to 1 columns to get the value
Write the result ActiveCell.Value = Search_Value. Then continue to search for the rest of sheets. Offset 1 coloum and 1 row to write the second value...
Searching is a very difficult concept, and I truly have no idea how to do the search part. Please help
With Worksheets(1).Range("a1:a500")
counter=0
Set c = .Find("Total", lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
counter=counter+1
Worksheets(2).range("A1").offset(counter,0)=c.offset(0,1)
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
firstaddress holds the location of the first cell found (so we know when to stop); firstaddress.offset(0,1) will give you the value you are trying to save, so setting worksheet(2).range("a1").offset(counter,0) will list all the values it finds on the 2nd tab, from a1 down to however many it finds in the range

Resources