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.
Related
I am just starting out with Excel VBA and to be honest I am not that skilled in using normal Excel either.
I've got one sheet that has unique codes in column C, I also have another sheet that has unique codes in column A (except first rows as they've got column labels).
In case this unique code from sheet 1 is also found in the column in sheet 2, I want column J in the sheet1 to have that code value, otherwise, if it cannot be found, I want it to have #N/A.
Normally in Excel I would do this by selecting J2 and entering following function:
=VLOOKUP(C2,Sheet2!A:A,1,False)
Then I'd just double click the corner of the cell to populate all rows.
How do I do it in VBA? I wrote this code hoping it would do something:
Worksheets("Sheet1").Activate
ActiveSheet.Range("J:J").Value = Application.VLookup(C2,Worksheets("Sheet2").Range("A:A"),1,False)
However, this does not work. I just get #N/A for all cells in the J column. Any suggestions what I can do?
The following code will give you what you want. Note that you would only want to put the formula into rows where values actually exist in column C on sheet1.
Option Explicit
Sub InsertVlookup()
Dim LastRow As Long
LastRow = Sheet1.Cells(Rows.Count, 3).End(xlUp).Row
With Sheet1.Range("J2:J" & LastRow)
.FormulaR1C1 = "=VLOOKUP(RC[-7],Sheet2!C[-9],1,FALSE)"
.Value = .Value
End With
End Sub
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.
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
I want to check Column A (A1:A365) in Sheet2, Sheet3, Sheet4 and Sheet5 and find Maximum value for each cell. Compare A1 in Sheet2, Sheet3, Sheet4 and Sheet5, find maximum of it and report it in A1 in result page. Also in cell B1 report corresponding sheet for this maximum. This goes on to Column A (A1:A365)
the following code i used:
Worksheets("sheet2").Range("A1").Value = a
Worksheets("sheet3").Range("A1").Value = b
Worksheets("sheet4").Range("A1").Value = c
Worksheets("sheet5").Range("A1").Value = d
MaxValue = Application.Max(a, b, c, d)
Range("A1").Value = MaxValue
yes i have just 4 sheets – Mohsen 11 mins ago
Non VBA Solution
In Sheet1, Cell A1, put this formula
=MAX(Sheet2!A1,Sheet3!A1,Sheet4!A1,Sheet5!A1)
In Sheet1, Cell B1, put this horrifying formula. I am sure there is a better way to find the sheet name though.
=IF(Sheet1!A1=Sheet2!A1,RIGHT(CELL("filename",Sheet2!A1),LEN(CELL("filename",Sheet2!A1))- FIND("]",CELL("filename",Sheet2!A1),1)),IF(Sheet1!A1=Sheet3!A1,RIGHT(CELL("filename",Sheet3!A1),LEN(CELL("filename",Sheet3!A1))- FIND("]",CELL("filename",Sheet3!A1),1)),IF(Sheet1!A1=Sheet4!A1,RIGHT(CELL("filename",Sheet4!A1),LEN(CELL("filename",Sheet4!A1))- FIND("]",CELL("filename",Sheet4!A1),1)),IF(Sheet1!A1=Sheet5!A1,RIGHT(CELL("filename",Sheet5!A1),LEN(CELL("filename",Sheet5!A1))- FIND("]",CELL("filename",Sheet5!A1),1)),""))))
A word of caution though. To use the RIGHT(CELL("filename",Sheet2!A1),LEN(CELL("filename",Sheet2!A1))- FIND("]",CELL("filename",Sheet2!A1),1)), you need to have the workbook saved.
My Sheet2, A1 has 1, Sheet3, A1 has 2, Sheet4, A1 has 2.5,Sheet5, A1 has 3
VBA Solution
Sub Sample()
Dim ws As Worksheet
Dim i As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
.Range("A1:A365").Formula = "=MAX(Sheet2!A1,Sheet3!A1,Sheet4!A1,Sheet5!A1)"
.Range("A1:A365").Value = .Range("A1:A365").Value
For i = 1 To 365
Select Case .Range("A" & i)
Case ThisWorkbook.Sheets("Sheet2").Range("A" & i).Value: .Range("B" & i).Value = "Sheet2"
Case ThisWorkbook.Sheets("Sheet3").Range("A" & i).Value: .Range("B" & i).Value = "Sheet3"
Case ThisWorkbook.Sheets("Sheet4").Range("A" & i).Value: .Range("B" & i).Value = "Sheet4"
Case ThisWorkbook.Sheets("Sheet5").Range("A" & i).Value: .Range("B" & i).Value = "Sheet5"
End Select
Next i
End With
End Sub
Quick solution
Based on Sidd's answer, here's my non-VBA solution without the horrifying formula:
Place this formula in Sheet1!A1: =MAX(Sheet2:Sheet5!A1)
As the sheet name does not have to be flexible (I'd assume you don't change it that often), you can use this formula in B1:
=IF(Sheet2!A1=A1,"Sheet2",
IF(Sheet3!A1=A1,"Sheet3",
IF(Sheet4!A1=A1,"Sheet5",
"Sheet5")))
More structural solution (better suited for many worksheets):
If you have many worksheets, you could consider this alternative.
Have a list of the relevant worksheets stored somewhere in your worksheet. (in the example, I place the list in E3:E7). Name this range Sheets. (Similar to Sidd's horrifying formula, I used the CELL formula to dynamically get each sheet name. However, this is not necessary in a static model)
(same as step 1 above): Place this formula in Sheet1!A1: =MAX(Sheet2:Sheet5!A1)
Place this formula in A2:
=INDEX(Sheets,MATCH(1,COUNTIF(INDIRECT("'"&Sheets&"'!A1"),A1),0))
Enter it as an array formula, i.e. press Ctrl-Shift-Enter instead of Enter.
I uploaded the second solution here.
Kudos to this instruction!
Since the formula is intended to be copied down, you don't want to hard-code a reference to cell A1 in the INDIRECT in Peter Albert's formula. You can use CELL("address",A1) to get a reference to A1 that can be copied down instead.
You can also avoid the need to array-enter the formula by using LOOKUP to return the result instead of INDEX & MATCH.
Note that LOOKUP will return the name of the last worksheet with the max value in case of a tie.
The following formulas use a named range Sheets with the names of each worksheet
=MAX(Sheet2:Sheet5!A1) returns max value (identical to Peter Albert's formula)
=LOOKUP(2,1/COUNTIF(INDIRECT("'" & Sheets & "'!" & CELL("address",A1)),A1),Sheets) returns name of sheet with max value
Make two new sheets with one titled "First" and the other "Last". Make new sheets for your project originate through a button that contains a macro to add duplicate sheets between "First and Last". Then just put one of these simple formulas for example in your reporting cell: =SUM(First:Last!K28) or =MAX(First:Last!K28)
The two columns look like on this image.
When I want to show only the cells which contain a letter 'b', I can no longer see the text "Title1" and "Title2" which is normally visible in the column B.
I guess although the cells in column B are merged, the text is still bound to A3, respectively to A7.
So how can I at the same time filter the visible content and preserve the merged text? In simple words, I want to filter content by letter 'b' and I still want to see the text "title 1/2" in the column B.
You tagged excel so here is a solution in excel:
You need to click on that column with the merged cells and unmerge all cells.
Then you need to put this formula at the top of your list and enter it with ctrl+shift+enter(this will enter it as an array formula):
=OFFSET(C3,MAX(IF(NOT(ISBLANK(C$3:C3)),ROW(C$3:C3),0))-ROW(C3),0)
Then you need to autofill that down.(this function seems a little verbose but I just got it online - there is probably a simpler way to do this - but it finds the last nonblank cell in a range).
I think openoffice has similar functions so you should be able do the same or something similar in openoffice.
Alternatively if you are using excel you could click on the column you want to unmerge and run this macro:
Sub UnMergeSelectedColumn()
Dim C As Range, CC As Range
Dim MA As Range, RepeatVal As Variant
For Each C In Range(ActiveCell, Cells(Rows.Count, ActiveCell.Column).End(xlUp))
If C.MergeCells = True Then
Set MA = C.MergeArea
If RepeatVal = "" Then RepeatVal = C.Value
MA.MergeCells = False
For Each CC In MA
CC.Value = RepeatVal
Next
End If
RepeatVal = ""
Next
End Sub
Good Luck.
EDIT:
I found a Non-VBA solution that will work in both excel and openoffice and doesn't require you to enter it as an array formula(with ctrl+shift+enter):
=INDEX(B:B,ROUND(SUMPRODUCT(MAX((B$1:B1<>"")*(ROW(B$1:B1)))),0),1)
In open office I think you want to enter it like this:
=INDEX(B:B;ROUND(SUMPRODUCT(MAX((B$1:B2<>"")*(ROW(B$1:B2)))),0),1)
or maybe like this:
=INDEX(B:B;ROUND(SUMPRODUCT(MAX((B$1:B2<>"")*(ROW(B$1:B2)))),0))
You just need to autofill that formula down:
Your main problem seems to be the one "blank row" that you have left after the filter fields.
Remove it, and it will work fine.