Excel Automated Data Retrieval From Another Workbook - excel

So I'm pretty new to excel formulas and got almost no experience with VBA. But I've come across with a problem that I need to solve.
So the scenario goes like this.
I've got two workbooks and I need to retrieve data to one workbook from another if the condition for a cell value is met. Let me explain with an example.
(C for columns, R for rows, x for random numbers)
I've got Workbook A as shown below:
And Workbook B with the same structure
So what I'm trying to achieve here is:
When I change/insert values in Workbook A, C3Rx there will be a conditional mechanism that will check for the value.
Let's say if C3R1's value is "1" on Workbook A, it should fill C1R1, C2R1 and C3R1 on Workbook B accordingly.
If the value is not "1", it just should keep scanning the Workbook A, C3 and when it meets the conditional requirement (C3Rx having the value of "1"), it should write it in and go to the next row (C1R(x+1)). Follow the procedure again and again. Scanning all values in Workbook A.
I've tried to make it work using VLOOKUP and some other functions together but it doesn't suit really well with my case. It works with spaces when the value does not meet the condition and also, I need to fill all the cells on C1 with the formula till the end. (considering I don't know how long it may go, that's not really a solution for me)
I think it's achievable with Macros but like I've said, I don't have much experience with VBA.
Thanks for your help in advance.
Have a good one.

I'm not exactly get what you mean.
Anyway below I am guessing on what you mean.
Below is what contains in Workbook-A sheet1 column A to C
Below is what contains in Workbook-B sheet1 column A to C
With the first condition that Workbook-A and Workbook-B are arlready open.... below is Workbook-A sheet1 where cell C4 and C7 fill with 1 value,
and after the button is clicked :
1. Cell A4 to C4 value in Workbook-A Sheet1 become the value of cell A4 to C4 value in Workbook-B Sheet1.
2. Cell A7 to C7 value in Workbook-A Sheet1 become the value of cell A7 to C7 value in Workbook-B Sheet1
Button1 is assign to a macro like this :
Sub test()
Set wbA = Workbooks("Test-A.xlsm").Sheets("Sheet1")
Set wbB = Workbooks("Test-B.xlsm").Sheets("Sheet1")
Set Rng = wbA.Range("C2", Range("C2").End(xlDown))
For Each cell In Rng
If cell.Value = 1 Then
r = cell.Row
wbA.Range("A" & r, "C" & r).Value = wbB.Range("A" & r, "C" & r).Value
'wbA.Range("A" & r, "C" & r).Interior.Color = vbRed
End If
Next
End Sub
The code will look to each value in column C (starts from row 2) in Workbook-A sheet1.
If the code find the value is 1 in row X of column C, then it copy row X of column A to C in Workbook-B sheet1.
That's if I'm not mistaken on what you mean.

Assumed that both workbooks already open.
Below is the beginning look of wb-A right after it's open :
Below is the beginning look of wb-B right after it's open :
Back to wb-A, after you examined the data, you decided that id-003 branch location is not London, but Madrid. So you type number 1 in the third column id-003 row. WB-A now look like this :
And what you expect is, if the code find any row in the third column of wb-A with value "1", then the code have to copy all the three columns of id-003 row then paste it to the last blank row in the emp_id column of wb-B. So, wb-B look like this :
Here is the code which has to be in wb-A module :
Sub test()
Set wbA = Workbooks("Test-A.xlsm").Sheets("Sheet1")
Set wbB = Workbooks("Test-B.xlsm").Sheets("Sheet1")
Set Rng = wbA.Range("C2", Range("C2").End(xlDown))
For Each cell In Rng
If cell.Value = 1 Then
Range(cell, cell.Offset(0, -2)).Copy Destination:= _
wbB.Range("A1000000").End(xlUp).Offset(1, 0)
End If
Next
End Sub
Again, above is just my guessing because I'm still not clear what you want and how is the situation.

Related

How to copy and paste a filtered table using VBA from one column to another visible column?

I have trouble in solving this particular issue .
I am currently trying to automate a function where the macro would automatically update a value from Column C to Column D
For example if in filtered Cell C2 and C4 have a value i would like it to paste to Cell D2 and D4 and would skip D3
I hope my explanation makes sense , as this is a sample i can think of in explaining on larger scale
In order to copy the filtered cells of column "C:C" (starting from the second row), to the corresponding rows of column "D:D":
Sub copyVisCells()
Dim sh As Worksheet, lastR As Long, rngVS As Range, Ar As Range
Set sh = ActiveSheet
lastR = sh.Range("C" & sh.rows.count).End(xlUp).row
Set rngVS = sh.Range("C2:C" & lastR).SpecialCells(xlCellTypeVisible) 'here it needs error checking in case of no visible cells...
For Each Ar In rngVS.Areas
Ar.Copy Ar.Offset(0, 1)
Next Ar
End Sub
If you can define a rule based on what to copy values from B:B, let us say a row before the visible cells in D:D, the code can be adapted to do it, but you must explain based on what rule to do it...

Match 2 colums in 2 excel sheets and paste a third

I have two excel sheets both with matching product numbers. What I need to do is match the product numbers and then copy a column from the first sheet to the second.
My example:
Column C in the first sheet contains product numbers
Column A in the second sheet contains product numbers
I want to match C & A and then copy column B from the first to the second sheet.
Sorry if this has been answered before, my knowledge is basic but I am trying to learn
Thanks for any help.
CD
Assuming your numbers to match start in C2 (headers in row 1) and numbers to copy in AC2 on Sheet1, this should do what you want. Paste it into AC2 on Sheet2 and then drag copy to the length of column A on Sheet2.
=IF(IFNA(MATCH(A2,Sheet1!C:C,0), FALSE), INDIRECT("Sheet1!AC"&MATCH(A2, Sheet1!C:C, 0)), "Not Found")
Note this will give "Not Found" for a value on Sheet2 which is not found on Sheet1, you can change that by just replacing the string "Not Found" in the formula with whatever you want (e.g. 0).
Formula:
You can use OFFSET with IF. In B1 of sheet 2 for example you could put, drag down for as many rows as required,
=IF(A1=Sheet1!C1,OFFSET(Sheet1!A1,0,COLUMNS(Sheet1!A:AC)-1,1,1))
If you set your data up as a table in sheet 2 (Ctrl + T with an populated cell selected) then add this formula to the appropriate column it will autofilter down the formula.
Or:
With code in a standard module:
Public Sub AddFormula()
Dim rng As Range
With Worksheets("Sheet2")
If Application.WorksheetFunction.Subtotal(103, .Columns(1).Cells) > 0 Then
For Each rng In Intersect(.Columns(1), .UsedRange)
If Not IsEmpty(rng) And Not IsError(rng) Then 'assume ignore empty cells
If rng = Worksheets("Sheet1").Cells(rng.Row, "C") Then Worksheets("Sheet1").Cells(rng.Row, "AC").Copy rng.Offset(, 1) 'determine where you want value to go
End If
Next rng
End If
End With
End Sub
Note:
This is based on your comment that you are comparing sheet2 col A with sheet1 col C and copying sheet1 col AC if match.
I assume that the product numbers in both sheets are unique. If that, I suggest use the offset and match formulas to achieve what you want.
sorry for my half-baked answer due to i just typed them with phone and it is not convenient to input complicated style, here is the additional information:
the match formula is used to query the location of the target product number in the original sheet( sheet 1 in your case above);
after we located the position of the target product number, extract the information you wanted through the offset formula.
here are the specific process:
Sheet 1 represent the original information and sheet 2 represent the target one. What we required to do is copy the info in col D of sheet 1 to col D of sheet through the product:
OFFSET($D$5,MATCH(I6,$D$5:$D$16,0)-1,1,1,1)
OFFSET($D$5,MATCH(I7,$D$5:$D$16,0)-1,1,1,1)
OFFSET($D$5,MATCH(I8,$D$5:$D$16,0)-1,1,1,1)
OFFSET($D$5,MATCH(I9,$D$5:$D$16,0)-1,1,1,1)
OFFSET($D$5,MATCH(I10,$D$5:$D$16,0)-1,1,1,1)
or you can refer to the output directly:
enter image description here
for more information about these two formulas, you can refer to the help of excel of google.

VBA - Enter a formula into all active rows of the current column (when you don't know the column letter, or if the column changes)

Hi guys this is my first post, I'm wondering if you can possibly assist me.
I'd like to write a macro / script that will allow me to put a formula into the column to the right of the currently selected one (for all active rows of the current column) based on what column I've selected. The issue I'm having is that I don't always know the current column letter (as my selection changes from worksheet to worksheet).
To give you an example:
One of my columns currently contains dates, that dates are entered in different formats though, some are separated with ".", some with "-", some with spaces and so on. I have a formula that will deal with this so I need to put this formula in the column to the right of the selected column (which has the dates).
I have been able to do this when I specify the column letter, but not if it changes.
Please can you help?
Give this a go,
Sub SomethingNeat()
Dim rng As Range, x
x = Selection.Column
On Error Resume Next
Set rng = Columns(x).SpecialCells(xlCellTypeConstants, 23)
If Not rng Is Nothing Then rng.Offset(, 1) = "'=MyFormula"
End Sub
You can use ActiveCell.Offset(0,1).Value = Variable
That means that whetever your current cell is you can move and "select" to put a value to the right cell of the one you have activated. You can move the selection using a loop.
Do
Workbooks("Yur workbook name").Worksheets(1).Range(Adress you want to start adding).Offset(0, 1).formula = "=FORMULA"
i = i + 1
ActiveCell.Offset(1, 0).Activate
Loop While i <= max_row
Edit: 2nd
Put the formula in a cell lets say C1
'Select a range
Set take = Worksheets(1).Range("C1")
take.Copy 'copy the formula
Worksheets(1).Paste Destination:=Worksheets(1).Range("B1:B10")
That will copy your function whenever you want it to

If/Then/Replace formula

I know VBA is probably the way to go but I believe this can be done using a few basic formulas.
I need "E2" to be replaced (cut/copy) with the contents from "A3" but only if "D2" = "Status:Active"...and so on down the sheet
the yellow and blue color-coding are only for this example and do not represent the whole sheet
this is a 7,000 line spreadsheet that was a report generated off some old system and I'm trying by best to collate and format.
Try in Column F starting with cell F2
=if(AND(E2="",D2="Status: Active"), A2, E2)
This will test to see if D2 has "Status: Active" and if it does, it will pull the value form A2. If it isn't then it will use the address already in E2
As explained in your comments that you are looking for A to be blank when F accepts a value from it (Cut/Paste)... there is no way for a formula to affect another cell, but... You could add a new Column B inserted after A and put the following formula in there: =if(A2<>G2, "", A2). Then hide Column A. The new B column will then only show values of Column A when it's not already in Column G (formerly column F before the insertion of the new column).
You could also do all of this through VBA, but that seems like more effort than it's worth when some simple formulas will get you there.
Seeing as you want the column A to be blank you can try this macro out:
Option Explicit
Sub SwapCols()
Dim oWs As Worksheet
Dim lRowNum As Long
Dim i As Long
Set oWs = ActiveWorkbook.Worksheets("Sheet1")
lRowNum = oWs.Range("A2").End(xlDown).Row
For i = 2 To lRowNum
If oWs.Range("D" & CStr(i)).Value = "Status:ACTIVE" Then
oWs.Range("E" & CStr(i)).Value = oWs.Range("A" & CStr(i)).Value
oWs.Range("A" & CStr(i)).Value = Null
End If
Next i
End Sub
Make sure you replace "Sheet1" with the name of your sheet. the macro basically checks if a cell in column D has the value "Status:ACTIVE" and if it does it copies the corresponding cell in column A to column E.
Just make sure if you do run this and you do not like the results do not save.

Excel 2007 - Formula changes to #REF

So I've got this Workbook which contains a lot of data. And I've got this one sheet which basically copies the data based on certain conditions.
Each cell in each row looks like this (the last specified cell is the one where the formula is in):
=IF(Numbers1!E2<>0;Numbers1!A2;"")
=IF(Numbers1!E3<>0;Numbers1!A3;"")
=IF(Numbers1!E4<>0;Numbers1!A4;"")
=IF(Numbers1!E2<>0;Numbers1!B2;"")
=IF(Numbers1!E3<>0;Numbers1!B3;"")
=IF(Numbers1!E4<>0;Numbers1!B4;"")
So the formula in cell A2 is the first one, formula in A3 is the second line etc.
I want to copy the value from the same column and row from the sheet Numbers1, IF the value in the same row of column E is not 0. This seems to be working just fine.
But, when I update the data in Numbers1 sheet, the formulas are all of a sudden invalid and the formula now looks like this:
=IF(Numbers1!#REF!<>0;Numbers1!#REF!;"")
Each formula in each cells look identical to the formula above. And I can't have that, why can't Excel just keep the formula as it is without "helping" me?
Since you may be better off using a macro to rewrite your formulas, here are the basics:
Sub RewriteFormulas()
Dim row, col As Integer
row = 1 'row you want your target formulas to be on
For row = 1 To 60
For col = 1 To 13
ActiveSheet.Cells(row, col).Formula = "=IF(Numbers1!" & Cells(row,col).Address & "<>0,Numbers1!" & Cells(row+2,col).Adddress & ","""")"
Next row
Next col
End Sub
You can play around with using different sheets (or different workbooks) instead of just ActiveSheet so you can have 1 workbook that stores the macro and alters data in whatever workbooks provide your updated datasets.
Hope that helps...

Resources