VBA Excel - Find value and copy corresponding cell - vba

I have a workbook with several sheets. The Sheet1 acts as a sort of summary of action items based on the info we enter into the other sheets.
In Sheet1, Column A is titled Tester and under that we are manually entering the defect numbers that group needs to review.
In Sheet2, we have a list of all the defects. Each defect has its own row.
Column A has all the unique defect numbers (e.g., Defect001, Defect002, Defect003).
In Column B we have a dropdown list with all the groups a defect could be assigned to (e.g., Tester, Developer, Client).
Other columns have the various details, severity, SLA times, etc. for the defect.
Right now, we enter all the info into Sheet2 and then go to Sheet1 and enter the defect numbers under the team that needs to work them.
I want to remove the manual component of all this. Instead, I want the defects entered in Sheet2 to automatically appear under their corresponding group on Sheet1.
So, on Sheet2, I have a button tied to a macro which updates various fields in the Workbook.
I want to add to that macro some functionality that will do the following:
Look at Sheet 2, Column B
For each row that has a value of Tester in Column B, go to the corresponding cell in Column A to get the defect number.
Back in Sheet 1 under the Tester column (Column A), I want each defect to be listed.
Therefore, if Sheet2 has Defect001, Defect003, and Defect005 that were all assigned to the Tester group, those values would appear in one cell under the Tester column in Sheet1.
Since some folks think I am asking them to do all the work for me, let me clarify that I am not. I just need a starting point...something to get me going in the right direction. I appreciate any code or links that would get me started down the right path.
Let me know if I need to provide more detail or information.
Thanks for all your help.
==============
As requested, here are the code snipits I have tried so far:
For i = 1 To 5000
If ActiveSheet.Cells(i, 12).Value = "Tester" Then
MsgBox "This Works"
End If
Next i
And I looked into trying to figure out how to call the info via a pivot:
Sub ListAllItemObjects()
For Each pvt In ActiveSheet.PivotTables
For Each fld In pvt.PivotFields
For Each itm In fld.PivotItems
MsgBox itm
Next itm
Next fld
Next pvt
End Sub

Came up with a solution. Probably not the best. As FindWindow was so helpful to point out, I'm not an expert in VBA.
This will probably need some further adjustment to do everything I want, but it works well enough for now.
So to find the values I wanted that correspond to any row with Tester in Column B:
Sub UpdateStatusSummary()
For i = 1 To 5000
For Each cell In ActiveSheet.Cells(1, 2)
If ActiveSheet.Cells(i, 2).Value = "Tester" Then
ThisWorkbook.Sheets("Data").Cells(i, 10).Value = ActiveSheet.Cells(i, 1).Value
End If
Next
Next i
End Sub
This Sub (which will be changed to a Private Sub) is called with a button/graphic that has the Sub assigned to it.
To get the values from the Data sheet into one cell back on Sheet1, I went with this:
Function csvRange(myRange As Range)
Dim csvRangeOutput
For Each entry In myRange
If Not IsEmpty(entry.Value) Then
csvRangeOutput = csvRangeOutput & entry.Value & ","
End If
Next
csvRange = Left(csvRangeOutput, Len(csvRangeOutput) - 1)
End Function
Use =csvRange(Data!J2:J5000) in the cell you want the comma separated values from the Data sheet to go in.
Could and probably will be better after I play with it for a while. If I make any significant breakthroughs or changes, I'll be sure to post them.
If anyone has any feedback or better methods, I would greatly appreciate the constructive criticism.

Related

Copy Range Excluding Blank Cells - Paste to Another Sheet

Ok, back again!
I have tried to search throught this and other forums to find a similar solution, but everything Ive found is either just different enough that I cant figure out the application to my problem, or super complex, and I cant translate it! So Im hoping someone can help me here. Thanks in advance!!
Here is the scenario. I have a database that Im needing to add data to. Quote Number, PO Number,SubSystem Part Name, Vendor, Material, Price, Qty. Etc.
Long story short, and without getting into the context of why I did it this way (mostly because I think I would botch the explaination and be more confusing than helpful!) ... I have essentially 3 tables right next to each other.
Table 1 is columns H and I. These all have a formula similar to =if(isblank(J4),"",$I$1) Where I1 is the PO Number (which will remain the same for this set of entries.)
Table 2 is a pivot table in columns J through M. Using a slicer the user can select what sub systems they need for this PO. The pivot table will repopulate with the appropriate part numbers and unique information contained in another table.
Table 3 is a regular table in columns N through R. These columns have some formulas like above that pull from a single cell (for entering the date), some pull information from another table based on information in column J via a VLOOKUP, and some information is entered manually.
That might be too much information, but better to have it and not need it eh?
So heres the goal. With a VBA macro, I want to copy the data and paste it onto another sheet, at the bottom of a database. The trick is, because that whole setup above runs based on information coming from a pivot table, the list changes length constantly. It will never be longer than a certain length (still TBD) but will almost always be shorter. I can copy the whole thing, and have it paste to another sheet below the last entry... but it pastes below the last empty cell in the database sheet. What I mean is this:
The longest the table could be would be range H4:R38 for example. So I copy that, paste it to Sheet2 starting at cell A2. Upon further inspection, we see that there is only actual data in the range H4:R11. However, when we pasted it to Sheet2 it pasted the whole range H4:R38. When I run the macro again, instead of the new data set being pasted to row A10 (the row after where the data should have ended), it pastes to something like row 36... because its pasting below all the blank cells.
I have no idea what to do or where to start. Any help would be greatly appreciated! Thanks so much!
Code I've Tried:
Dim fabricationrange As Range
Dim destination As Range
Dim LastBBUFabDatabaseRow As Long
Worksheets("Sub Systems").Range("h4:r38").Copy
With ThisWorkbook.Sheets("BBU Fab. Database")
Worksheets("bbu fab. database").Range("z" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
Range("b" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats
lastbbufabdatabserow = .Cells(.Rows.Count, 2).End(xlUp).Row = 1
Set destination = .Cells(LastBBUFabDatabaseRow, 2)
destination.Value = PasteSpecial.Values
End With
Untested but here's a brute-force approach to locating the first empty row:
set rngDest = ThisWorkbook.Sheets("BBU Fab. Database").rows(2) '<< start here
do while application.counta(rngDest) > 0
set rngDest = rngDest.offset(1, 0) 'next row
loop
Worksheets("Sub Systems").Range("H4:R38").Copy
rngDest.cells(1).PasteSpecial xlPasteValuesAndNumberFormats '<< paste to col A

How to search multiple columns for unique text to input those unique rows into other sheet

I am not sure if my question is going to be regarding a macro in VBA or a VLOOKUP type of thing but I'll try my best to explain both.
I am trying to make a time sheet for people to enter their time spent working on various projects. It works well but I am trying to add a feature to separate the time spent at regular pay, overtime pay, or double time pay.
There are three sheets named Time Log, Project List, and Timesheet.
This is an image from Time Log:
Employees enter the project name and project number and task is retrieved from Project List using VLOOKUP and then the employee enters the type of payment they receive for that work. Throughout the week, an employee may enter multiples of the same entries.
At the end of the week, they'll go to Timesheet and press a button I have attached to a macro which will retrieve the unique values from all the entries in the Time Log.
This is an image from Timesheet:
As you can see, I have made it work to find the unique project names but now I have run into an issue with the pay type that is entered. I am not sure how to have it draw a new entry for each unique project name and unique type.
Ideally, it would come out like this:
So what I have so far is my macro to search the Time Log for unique project names and put them into Timesheet:
Sub Input_Project_Names()
'
' Input_Project_Names Macro
'
' Clear Project Names
Worksheets("Timesheet").Range("A4:A50").ClearContents
'Advanced Filter from Time Log to Timesheet
Sheets("Time Log").Range("A1").CurrentRegion.AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=Sheets("Project List").Range("H1:H2"), _
CopyToRange:=ActiveSheet.Range("A3:A52"), _
Unique:=True
End Sub
So this is where I am stuck... How do I modify the macro so that it looks for unique project names & types? And if I'm unable to do this, is there a way to use VLOOKUP or similar to do what I'm aiming for?
Big thanks for all help!
you could use SortedList object:
Option Explicit
Sub main()
Dim sortedList As Object
Set sortedList = CreateObject("System.Collections.SortedList")
Dim cell As Range
With Worksheets("Time Log") 'reference "Time Log" worksheet
For Each cell In .Range("A2", .cells(.Rows.Count, 1).End(xlUp)) 'loop thorugh referenced sheet column A cells from row 2 down to last not empty one
sortedList(cell.Value & "," & cell.Offset(, 3).Value) = cell.Resize(, 4).Value 'add current record to SortedList assigning "Project Name, Type" as ist key : it will do nothing if there's already a record with the same "Project Name" & "Type" key
Next
End With
Dim i As Long
With Worksheets("Time Sheet") ''reference "Time Sheet" worksheet
For i = 0 To sortedList.Count - 1 'loop through Sorted List object items
.cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(, 4).Value = sortedList.GetByIndex(i) 'write referenced sheet currently first empty row with current Sorted List item
Next
End With
End Sub

VBA - Finding, cutting, and pasting to another sheet

I am trying to make a check in/out system for tools. So far I have a worksheet that has a list of items that are checked out currently. Column A in that worksheet is the item number, Column B is the name, and Column C is the date and time. The macro I am trying to make is that when someone enters an the item number they want to check back IN, the macro will find that number they enter on the "Items Out" sheet, cut the entire row, and paste it to the "checkout History" sheet on the first available row. The cell that the user inputs the number they are checking in is the cell "E3" on the "Interface" worksheet. This is what I have so far.
Sub Checkin()
'
Dim targetSh As Worksheet
Dim i As Long
For i = 1 To Cells(Rows.Count, "F").End(xlUp).Row
If Cells(i, 6).value = Worksheets("Interface").Range("E3") Then
Range(Cells(i, 1), Cells(i, 6)).Copy Worksheets("Items Out").Range("A" & Worksheets("Checkout History").Cells(Rows.Count, "A").End(xlUp).Row + 1)
End If
Next i
End Sub
This doesn't work, I got this code from another post and I tried to change it to match my needs but it did not work and I could use some help here. You don't need to make the entire macro unless you really want to, I just need help finding what will make this macro work for me!

VBA script for grouping columns

I've come across Stackoverflow many times when I've been looking for excel VBA scripting answers, but I've come up to a wall with my searching for an answer to my problem.
I have data which I need to sort, but it needs to be row grouped first, otherwise the sorting takes away the conformity of the info.
I want to group using info on just 1 column, and have found VBA scripts which do what I want except that they group 1 row to many. (if that makes sense)
i have info a little like
a b c d
Revise blank cell info info
blank cell blank cell info info
blank cell blank cell blank cell blank cell
Revise blank cell info info
blank cell blank cell info blank cell
Revise blank cell info info
etc etc
I want to group the top 3 rows, then the next 2.
but the only VBA script I found looks down the column for the word 'revise' but then groups the cells above the word revise, not below
I hope all that makes sense
thanks for any help
BTW, I dont really have any knowledge of programming, other than what I've gained through running some macros on other projects. Hopefully I wont need telling like a 5 YO, but may need some explanations of any code specific terms
I got this VBA script which works as I described
Option Explicit
Sub Macro2()
Dim rData, rCel As Range
Set rData = Range("a1", Range("a" & Rows.Count).End(xlUp))
Application.ScreenUpdating = False
With rData
On Error Resume Next
.Rows.Ungroup
.Rows.EntireRow.Hidden = False
On Error Goto 0
End With
For Each rCel In rData
If rCel = "END" Then Exit For
If rCel <> "Revise" Then
Rows(rCel.Row).Group
rCel.EntireRow.Hidden = True
End If
Next
Application.ScreenUpdating = True
End Sub
Here's a quick and dirty non-VBA solution, as you say you are not overly familiar with VBA.
Add a column to the right of your data, which will hold a new index that tracks each time 'Revise' is listed in column A. Starting in A2 and copied down, this will look as follows [you may need to hardcode the first entry as 1]:
=if(A2="Revise",A1+1,A1)
This will create a column which increases by 1 each time there is a new "Revise". Then just select your entire data block, right-click, and Sort by your new index column.

VBA Assistance needed. trying to select a range of cells, cut, move and paste

I have a spread sheet that is updated weekly. What i need to do is cut come of the cells and paste to a new location. I have never used macros or VBA before but I am getting frustrated with the amount of time I spend doing this. I know that I can use a macro but don't know how to write it.
I am trying to move the name of the hotel and resort to the left of the passengers title
R81C00 CHALET LE VALENTIN SAUZE D'OULX
MR HAYHOE 8
MR GLOVER 2
This repeats throughout the spread sheet. The number of lines between the names is dependent on information further right in the sheet.
546L
__________1 RESORT INFORMATION
__________5 SKI/S.BOARD CARRIAGE
__________8 AD L/P BRN BF 31/12/99
what I would like to do here is move these lines onto the same line as the flight number (this is the same line as passenger details) and then delete the lines with no data. this way all the details would be on the same line and then i would just need to fill down for the hotel names.
thanks in advance for any help please let me know if i haven't explained it clearly.
Trying to keep this general enough to be of use to other people, the basic process to follow would be:
find the next hotel/resort combination
find each passenger for that hotel/resort
add in the details for the other attributes
move on to the next passenger
move on to the next hotel/resort
If we start with finding the hotel/resort combination and we assume that this is on Sheet1 in column A in a single cell and that nothing else is in column A then we would need this macro:
Option Explicit
Sub main()
Dim lngCurrRow As Long
Dim lngMaxRow As Long
With ThisWorkbook.Worksheets("Sheet1")
lngMaxRow = .UsedRange.Rows.Count
For lngCurrRow = 1 To lngMaxRow
If (.Cells(lngCurrRow, 1).Value <> "") Then
MsgBox .Cells(lngCurrRow, 1).Value
End If
Next lngCurrRow
End With
End Sub
This should pop up a message box with the name of each hotel/resort in turn.
All the code does is work out how many used rows there are on the worksheet (and stores that in lngMaxRow) and then works through every used row (using lngCurrRow to keep track of which row we are on) checking the value of the cell in column A on that row (the .Cells(lngCurrRow, 1).Value part). If there is something in that cell (the (<> "" part) then it displays the value of that cell.
The more difficult case is when there is other data in column A (e.g. if the passenger names were also in column A). In that scenario, we need a way to easily recognise what is a hotel/resort combination and what is a passenger name but I don't have enough info about your current structure to determine how to do that