Best way to return data from multiple columns into one row? - vba
I have a sheet with just order numbers and another with order numbers and all of the data associated with those order numbers. I want to match the order numbers and transfer all of the available data into the other sheet. I've been trying to use loops and VLOOKUP but I'm having problems (plus I have 116 columns I want to transfer data from so my vlookup expression doesn't look very nice). Any advice would be appreciated!
this is what I have so far and I'm getting an object error.
I don't think it's the right way to go about it in general though.
Dim LookUpRange As Range
Dim row As Range
Set LookUpRange = Worksheets("batches").Range("B4:B1384")
Set row = Worksheets("batches").Range("C:DL")
For Each row In LookUpRange
row.Select
Selection.FormulaArray ="=VLOOKUP(RC[-1],OrderLvl!RC[-1]:R[1380]C[113],{2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,207,108,109,110,111,112,113,114,115},FALSE)"
Next row
End Sub
Please consider this VBA script to resolve your inquiry:
Sub LookupOuput()
Dim OrderNumberColumn As Range
Set OrderNumberColumn = Worksheets("batches").Range("B2:B1384")
Dim LookUpRange As Range
Set LookUpRange = Worksheets("OrderLvl").Range("C:DL")
Dim cell As Range
Dim FindResult As Range
For Each cell In OrderNumberColumn
If Not cell.Value2 = Empty Then
Set FindResult = LookUpRange.Find(what:=cell.Value2)
If Not FindResult Is Nothing Then
cell.Range("A1:DJ1").Value2 = LookUpRange.Rows(FindResult.row).Value2
End If
End If
Next cell
End Sub
Basically searches for each Order Number in the first sheet on the second sheet. This outputs (if search term exists) the cell that that string is found which we later refer to its row number to output the whole row to the first sheet. Cheers,
A regular VLOOKUP may be able to give you what you need, if you use a small trick...
Insert a row above the data table, and put sequential numbers in
each cell of that row. (ie, A1 = 1, B1 = 2, C1 = 3, etc...)
Do the same thing on your blank table.
Assuming that your first order number is in cell A2, put the following formula into B2: =VLOOKUP($A2,[other sheet name]!$A$1:$DZ$5000,B$1,0)
Drag this formula across all 116 columns, then down all however many rows you've got.
You'll need to adjust the ranges, obviously, but make sure that your lookup array starts in column A. (or alternatively, that your numbers start in the same column as the first column in your array.) Adding the numbers along the top allows you to change what column of the array you're referencing, just by dragging the cell formula.
Related
how to combine cell vertically in excel
It might be the most silly question in planet. how can I merge two cell values vertically in a repeated manner. as like this: Column A and B has 400+ cells therefore it is impossible to do what I want to achieve manually. Note: I want to merge B into A.
You can create a simple loop in VBA that runs through each cell in the data range then adds it to the output column Sub Merge() Dim data As Range Dim cell As Range Dim output As Range Dim i As Integer Set data = Range("A2:B4") Set output = Range("D2") i = 0 For Each cell In data output.Offset(i, 0) = cell i = i + 1 Next End Sub
You can use the INDEX function to refer to each cell. If data is in A2:B4, this formula works in any column but must start in row 2 and can then be filled down: =INDEX($A$2:$B$4,ROW()/2,MOD(ROW(),2)+1) The formula uses the current row as a counter. On every even row it gets a value from the first column of data and on every odd row it gets a value from the second column of data. After every 2 rows it gets values from the next row of data.
excel: correct column position
Actually, it is a simple question, although I don't know if it's possibl to do what I wanted to. I'm just copying a column from another using this sub: Sub copy_column() Sheets("FROM").Columns("A").Copy Destination:=Sheets("TO").Columns("A") End Sub In fact, in my sheet "FROM" my first row with data is the row 3 and then when I copy to "TO" sheet it's starting from row 3 too. My idea was to place it at the row 1 at "TO" sheet. Is there any way to write something like Columns("A"-2) to put it in the right place?
An alternative which avoids deleting rows (if you have data you want to keep in other columns for example) is to take an intersection of a range overlaid with itself, but offset... The following code assumes you have some data in row 1 in the sheet so UsedRange would start from the correct row, but you can change the offset to accommodate missing data in the rows above Dim CopyRange As Range With Sheets("From") Set CopyRange = Application.Intersect(.UsedRange, .Columns("A")) Set CopyRange = Application.Intersect(CopyRange, CopyRange.Offset(2, 0)) CopyRange.Copy Destination:=Sheets("TO").Cells(1, 1) End With
Loop Through 1st cell of each column in used range Excel VBA
I have some sample data in a sheet, I want to get headers of each column in used range, Here is the sample I never know the exact number of columns used, I need to get all headers (Customers, Managers, Managers-2....)from this sheet to show it to user. I can loop through each cell in a used range like For Each cell In ActiveWorkbook.Worksheets(SheetName).UsedRange Next but how can I loop though each column, and only read values from their 1st cell? as number of these lists can be one or more than one.
Replace your code by this one iterate over the first row of used range: For Each cell In ActiveWorkbook.Worksheets(SheetName).UsedRange.Rows(1).Cells ... Next
'Get a 2D array of header values Dim aHeaders() As Variant aHeaders = Sheet1.UsedRange.Rows(1).Value
It all depends on your data quality. Come across this numerous times. If your headers are consistent, then you just need to resize to the first row. Something like: Dim r as range, r1 as range Set r = Range("A1").CurrentRegion.Resize(1) Then do a for each for the r range. For Each r1 in r Next You could also use an integer variable.... Dim i as integer For I = 1 to r.Count Next
Manipulating Excel spreadsheet, removing rows based on values in a column and then removing more rows based on values in another column
I have a rather complicated problem. I have a log file that when put into excel the column "I" contains event IDs, and the column J contains a custom key that keeps a particular even grouped. All i want to do is remove any rows that do not contain the value of say 102 in the event id column. And THEN i need to check the custom key (column J) and remove rows that are duplicates since any duplicates will falsely show other statistics i want. I have gotten as far as being able to retrieve the values from the columns using com objects and .entirecolumn cell value etc, but I am completely stumped as to how i can piece together a solid way to remove rows. I could not figure out how to get the row for each value. To give a bit more clarity this is my thought process on what i need to do: If cell value in Column I does not = 102 Then delete the row that cell contains. Repeat for all rows in spreadsheet. And THEN- Read every cell in column J and remove all rows containing duplicates based on the values in column J. Save spreadsheet. Can any kind persons help me? Additional Info: Column I holds a string that is an event id number e.g = 1029 Column J holds a string that is a mix of numbers and letters = 1ASER0X3NEX0S
Ellz, I do agree with Macro Man in that your tags are misleading and, more importantly, I did indeed need to know the details of Column J. However, I got so sick of rude posts today and yours was polite and respectful so I've pasted some code below that will do the trick ... provided Column J can be a string (the details of which you haven't given us ... see what Macro Man's getting at?). There are many ways to test for duplicates. One is to try and add a unique key to a collection and see if it throws an error. Many wouldn't like that philosophy but it seemed to be okay for you because it also gives you a collection of all the unique (ie remaining) keys in Column J. Sub Delete102sAndDuplicates() Dim ws As Worksheet Dim uniques As Collection Dim rng As Range Dim rowPair As Range Dim iCell As Range Dim jCell As Range Dim delRows As Range Set ws = ThisWorkbook.Worksheets("Sheet1") Set rng = Intersect(ws.UsedRange, ws.Range("I:J")) Set uniques = New Collection For Each rowPair In rng.Rows Set iCell = rowPair.Cells(, 1) Set jCell = rowPair.Cells(, 2) On Error Resume Next uniques.Add jCell.Value2, jCell.Text If Err = 457 Or iCell.Value2 = 102 Then On Error GoTo 0 If delRows Is Nothing Then Set delRows = rowPair.EntireRow Else Set delRows = Union(delRows, rowPair.EntireRow) End If End If Next If Not delRows is Nothing then MsgBox delRows.Address(False, False) & " deleted." delRows.Delete End If End Sub
There are a number of ways in which this can be done, and which is best will depend on how frequently you perform this task and whether you want to have it fully automated. Since you've tagged your question with VBA I assume you'll be happy with a VBA-based answer: Sub removeValues() Range("I1").Select 'Start at the top of the I column 'We are going to go down the column until we hit an empty row Do Until IsEmpty(ActiveCell.Value) = True If ActiveCell.Value <> 102 Then ActiveCell.EntireRow.Delete 'Then delete the row Else ActiveCell.Offset(1).Select 'Select the cell below End If Loop 'Now we have removed all non-102 values from the column, let`s remove the duplicates from the J column Range("A:J").RemoveDuplicates Columns:=10, Header:=xlNo End Sub The key line there is Range("A:J").RemoveDuplicates. It will remove rows from the range you specify according to duplicates it finds in the column you specify. In that case, it will remove items from the A-J columns based on duplicates in column 10 (which is J). If your data extends beyond the J column, then you'll need to replace "A:J" with the appropriate range. Note that the Columns value is relative to the index of the first column, so while the J column is 10 when that range starts at A (1), it would be 2 for example if the range were only I:J. Does that make sense? (Note: Using ActiveCell is not really best practice, but it's the method that most obviously translates to what you were trying to do and as it seems you're new to VBA I thought it would be the easiest to understand).
Excel: Use values in a sheet as index to list in a different sheet and replace values in the first sheet
I have an XL file with some data to be manipulated. I think I will need to use a VB script to do this - but perhaps there is a simpler way with a formula. Just the same, could someone point out BOTH ways of achieving the following? I have a column of numeric values (ID) in Sheet 1. I want to use each ID as an index to lookup a list in Sheet 2. Sheet 2 has two columns First column is the index and Second column is the Text String e.g. 1 Apple 2 Orange 3 Pear What I want is to replace the column of IDs in sheet 1 with the looked up text string from Sheet 2! Thats all... Please help!
Not a tough situation there. Here are some solutions... With VBA: I know you said you're a little new with VB so I tried to explain each line as I went along. Also, the code is free-handed so forgive me if I left an error in there somewhere. Sub replaceData() dim i as integer, j as integer 'These are just some variables we'll use later. dim sheetOne as worksheet, sheetTwo as worksheet, myWb as workbook dim myData as string, myId as string set myWB = excel.activeworkbook 'These three lines set your workbook/sheet variables. set sheetOne = myWB.worksheets("Old Data") set sheetTwo = myWB.worksheets("New Data") for i = 1 to sheetTwo.usedrange.rows.count 'This loops through the rows on your second sheet. myId = sheetTwo.cells(i,1).value 'This assigns the value for your id and the data on your second sheet. myData = sheetTwo.cells(i,2).value for j = 1 to sheetOne.usedrange.rows.count 'This loops through the rows on your first sheet. if sheetOne.cells(j,1).value = myId then 'This checks each row for a matching id value. sheetOne.cells(j,1).value = myData 'This replaces that id with the data we got from the second sheet. end if next j next i end sub With an Excel formula: Place the following formula in cell C1 of the first worksheet (the sheet with the IDs you will be replacing). **Note that you will have to replace the "InsertSheetTwoNameHere" portion with the name of your second sheet (don't remove those single quotes though). Also note you will need to replace the "1000" with the number of the last used row in sheet two. =vlookup(A1,’InsertSheetTwoNameHere’!$A$1:$B$1000,2,FALSE) Next simply drag the handle on the cell that makes it copy itself (whatever the heck it's called) all the way down to the end of your range. Next, copy those cells and then paste them over the IDs using the Values Only setting. Hope this helps and good luck.