Search through Excel tabs - excel

I am not very familiar with VBA programming, though I have written a few basic modules. I am having some difficulty in coding the following problem. I would appreciate if someone could show a basic solution.
Problem: I have 3 tabs in an Excel file, "Rack1", "Rack2" and "Rack3". They each have a column called "tag". The tag column contains a code, made up of numerals 0 to 9999 and one letter, A or B.
I want to look up the first "A" code in Rack1, then find the matching "B" code. If the B code is not in tab "Rack1", I want to search tab "Rack2", then "Rack3" if it's not found in either.
After the "B" part is found, I want to have a Msgbox message - "Found in Rack..." OR "B part not found"
Then ... go on to the next A code. Thanks

Please let us know what have you tried so far to get things working at your end.
You may have to loop through with your requirement with all sheets with respective column and its range.
I am not very clear, what is your exact requirement but you can do something like this
declare variables of your sheets
Set sheet1 = Worksheets("Sheet1")
Set sheet2 = Worksheets("Sheet2")
Set sheet3 = Worksheets("Sheet3")
You have to set range as per your need. Now you have to loop your sheet1 and tag column. While searching you need check whether A is exist in respective cell by cell value see below example
If InStr(cell_value, "A") > 0 Then
Meanwhile you can check below post for your further reference.
Excel Looping through rows and copy cell values to another worksheet

Related

How to loop through rows and changing the reference worksheet in each cell

I'm new to VBA and was just trying to figure this out.
I have a the following range CR7, CR9:CR24, CR28:CR39, CR45:CR50, CR52
Currently, each of these cells reference a value from another worksheet.
CR7='Trend'!CO7
CR9='Trend'!CO9
CR10='Trend'!CO10
This will follow the entire range.
What I was wanting to do was change the reference to 3 columns to the right.
CR7='Trend'!CO7 will then be CR7='Trend'!CR7
What I was trying to do was remove the last 3 characters after the "!" and then replace it with "RC[-3]" but I don't know what I'm doing at all. I just looked up different codes on this site and tried to piece it together, knowing that I would probably run into some obstacles because I'm new at this.
Here's the code I was trying to implement but running into an error.
For Each C In Range("CR7,CR9:CR24,CR28:CR39,CR45:CR50,CR52")
C.Value = Left(C.Value, InStr(C.Value, "!") - 1)
C.Value = C.Value & "RC[-3]"
Next
Any suggestions on how to get this corrected or is there a better way in doing this? Thanks in advance for looking into this!!
Solution
As stated in the comments, since you are looking for a specific header, the solution might be faster as follows:
Assuming the headers are in Sheet 1 as follows
Jan-22 (IE:actual data is 01-01-2022)
Feb-22
Mar-22
10
30
60
20
40
70
Where A1 = Jan-22, B2= Feb-22, etc.
Just use a combination of index and match, seems like you need to bring the row at the same level, so it will be as follows:
Set a referece in your formula to where you will have the criteria to look: In this example will be A1
=INDEX(Sheet1!$A:$C,ROW(),MATCH($A$1,Sheet1!$1:$1,0))
I'll explain:
Index will bring the column where you want the data, in this case will be the data under the month (Col A), seems like the row should be at the same level, so we will say the row will be equal to where the formula row is being casted, that's why the row(), finally, it will be in where the column matches the criteria that you want where the header is found, if you need an offset even after finding the column name, just sum it up at that part
MATCH($A$1,Sheet1!$1:$1,0) + 3
Demo:

Lock a range of cells in row based on first cell in row for every row in a table

Thank you in advance for your help.
The short version:
Need to have it so specific cells in a table are locked based on the what the first cell in that row contains.
The cell that is used to determine if other cells in the row are locked needs to always remain unlocked
I am working on an Excel sheet that contains a table. This table is used to record key information about products and is being updated daily. Each product is defined in its own row with columns being used to show details such as "Drawing number" and "Customer" etc.
Depending on the stage the product is at in development and how well the product went once completed, it will be labeled with a single "state" from a list such as "Ongoing", "Bad-product", "Archived" etc.
enter image description here
In each cell in each row, the text the cell contains is determined through either data validation in the form of lists or manually typed in. for example, all cells in the customer column will use a dropdown menu to select a customer from a list, but the cells in the drawing number column are always one of a kind so require manual input.
As so many people access and edit this table there are issues with mis-clicking and typing which causes data to be overwritten without someone noticing they have done so.
Because of this issue I want to be able to lock specific columns in a row based on what the first cell in the row contains ("State" column).
In the instance that the state column contains the word "archived" I want to lock the entire row from having its contents being changed.
In the case of the word being "Ongoing" I want to lock column AA and AB in that row.
As it is sometimes is necessary to go back and edit information due to a known mistake I would need to have it so the State column always remains unlocked whatever it contains.
I have attached a redacted Excel sheet that contains my attempt at a coded solution.
https://drive.google.com/open?id=1rzTp0ur1tpXIY_Wa3cVlcwruJ7Q1rT
The code can also be seen below.
Note: I was not sure how to even go about fulfilling the requirement for the "Ongoing" condition so would really appreciate it if someone could point me in the right direction
Sub Locking()
Dim KeyCells As Range
Set KeyCells = Range("A3:A612")
For Each cell In Range("A3:A612")
If cell.Value = "Archived" Then
cell.EntireRow.Locked = True
ElseIf cell.Value = "Ongoing" Then
cell.EntireRow.Locked = False
ElseIf cell.Value = "Bad" Then
cell.EntireRow.Locked = False
End If
Next cell
End Sub
This is my first post on here as I could not find the solution from looking at several different stack overflow questions. I also watched numerous videos on VBA code, but am definitely missing something, and am struggling to figure out what it is.
If I have posted this incorrectly or extra detail would be helpful, please let me know.
Any and all help is extremely appreciated!
Thank you
Edit: Code works now
Instead of locking the entire row
cell.EntireRow.Locked = False
if you just want to lock AA and AB in that row do the following:
cell.EntireRow.Locked = False 'unlock entire row first
'then lock just the 2 rows
Range("AA" & cell.Row & ":AB" & cell.Row).Locked = True
The others would work the same way.

Find currency and amount, also ID store in array and paste in Output sheet

I would like to find the text "Currency" in columns A or B, store all the currencies listed under Currency. Same process for Amount (Can be in an column)
Store values in an array. Then paste in Output Sheet. The currencies will already be listed in Output sheet in 1st row of the sheet. However if it is a new currency then the code should find last used cell in row 1 and add it. The value of Amount should be added to Output sheet against the currency and ID number also copied from the Source sheet.
I have some code.
Public Sub loopRow()
Dim curArray As Variant
Dim listarray As Variant
Dim cnt As Long
'Find Currency
Dim rgFound As Range
Set rgFound = Range("A:B").Find("Currency")
'Find last used row
curArray = Cells(rgFound.Address).End(xlUp).Row
'Transpose list of currecny from the row down from the word Currency that it has found
listarray = Application.Transpose(Cells(Rows, curArray).End(xlUp)).Row
For cnt = LBound(curArray) To UBound(curArray)
curArray(cnt) = curArray(cnt)
Next cnt
For cnt = LBound(curArray) To UBound(curArray)
'Debug.Print curArray(cnt)
'Copy and paste into Sheet under the correct curreny, if new currency then add this in row A
Next cnt
End Sub
Whilst you need to understand your question is unanswerable as is, I'll do my best to help.
The problem we have is not seeing the source sheet the way you do, as we can't see it at all. You say you have the word Currency in columns A or B or both, and an ID column somewhere, and Amount values everywhere. That's tricky source data. If as is more likely, the ID is in a specific column and the amounts are in a set of columns, then we'd have a chance.
Your question outlines the basic steps you'd want to take pretty well, so you're off to a good start.
However you can do all of the work without VBA, certainly if I'm right about the Source data. Create yourself a working sheet, or multiple working sheets. Definitely one to sort out the full list of currencies. Grab a copy of columns A and B (by formulae) and then have the working sheet go through line by line and use logic to build the list. Spreadsheets are great at this.
Once you have the list, use it as row headers on your Output sheet and use sumifs to get the values. I am not sure how the IDs would fit in, but if they were to be your row headings, then do the same as the above to get the list of unique ids and link them into your Output page in column A. Your sumifs can handle that.
That will hardly tell you all you need to know, but if you work it out you'll have learned a lot about Excel and when you need to go into VBA.
If you'd rather do it with VBA, break down each step until it works, and then go onto the next one.
And if you want more help, paste your data in here. Anonymise it first if you need to.

Set a named range based on search result Excel VBA

I’m cleaning up some data that I have to clean up on a regular basis. Manual process driving me nuts.
I would like to set a range name, based on a search in a single column for two values. I'm using Excel 2007 if that assists.
I have read many threads but cannot find anything this specific. The closest thread was Excel VBA - Making a Named Range based on a found cell
For example Search column A and when you find the word department set that as the beginning of a range then continue to search column A and when you find the word Medium set that as the second point in the range.
So the search has located Department at A5 and Medium at A24 I now want to set that range (“A5:A24”) and give it a name. Any name will do Just need to see how it’s done?
Once I can do this I can work out other things I do with these found ranges such as delete them, Mark them, Copy them etcetera. The main point is defining them in the fist place.
I can do the search to find both and have been performing some loops to do the same but I have no clue in how to set the range based on that search result.
enter image description here
Here's an example of how to create a Range from two found cells in a column, using the row number of each and the column number in question.
This can be modified to handle cells in differing columns, depending on your use case. You can delete, mark, and copy combinedRng without having to name it.
Sub MakeMyRange()
Dim departmentRng As Range, mediumRng As Range
With ActiveSheet.Columns(1)
Set departmentRng = .Find("Department")
Set mediumRng = .Find("Medium")
End With
If Not departmentRng Is Nothing Then
If Not mediumRng Is Nothing Then
Dim combinedRng As Range
Set combinedRng = Range(Cells(departmentRng.Row, 1), Cells(mediumRng.Row, 1))
End If
End If
End Sub
If you really need to name the range, you could just do:
combinedRng.Name = "Test"

Populating cells with text using VBA

I want to select a value from one sheet and put it in a cell on a different sheet.
My script determines the proper value but I cannot get the value into the sheet.
This function returns the !Value error on the 6th line of the following excerpt:
Function PrintTest(Cell)
Dim iRow As Integer
Dim bs As Worksheet
Set bs = ThisWorkbook.Sheets("By System")
iRow = Cell.Row
bs.Cells(iRow, 6).Value = "Hello World"
End Function
I also tried using .Text.
Note: In the actual Script the text will be seeded from the other sheet and I have it stored in a variable. I am not looking for a way to get the same text into many different cells.
Update: Cell is passed from an Excel spreadsheet as an empty cell G4. Row is defined as 4. To call the functions I have been typing "=PrintTest(G4)" in my Excel worksheet named "By System" .
Update 2: Scott Holtzman answered the question in a comment. You cannot write to cells from a UDF called from within a cell. The fix was to call it from a button.
Set a cell value with Cells(Row, Col).Value="Some text"
If your sheet is active you do not need to fully qualify the address. If you activate the sheet you can just use Cells all of the time. If you have to retrieve data from a different sheet then you will have to qualify the address with the sheet name, i.e. Sheets("My Sheet").Cells(Row, Col).Value.
Also your code has Row=Cell.Row, but you are not saying what cell is active. In that case you are getting some arbitrary value. So that is where your error is, probably. Cell.Row=????? Also Row is an excel word. Use something like intRow for your variable. For example intRow=25. Cells(intRow, 6).Value="My Cell". If the cursor is on a cell you can say intRow=ActiveCell.Row.
When you have a problem such as this set a break point in your code (in your example, the Set statement). Thenm run your code. When it stops at the set statement you can hit F8 to step through one line at a time and examine your variables. Then you will see if Row is really a number or just garbage.
Hope that helps

Resources