Display formula when cell is clicked vba - vba

So I have something like this:
A B C
1 11 12 13
2 10 20 15
3 1 -8 -2
So A3, B3, and C3 is generated by subtracting A1 to A2 and so on.
If you look at the top of the sheet there is a long bar that shows the formula of a cell when you click on one. If you fill in the sheet manually, you can type in that bar something like = A1 - A2 and it will fill A3 for you.
Right now, because my formula is actually in the code, when I click on A3 for example, the bar only shows 1 instead of = A1 - A2.
How do I get the formula to be displayed in the bar?

Use .formula for your requirement
Worksheets("Sheet1").Range("A3").Formula = "=A1-A2"

My guess is that right now, you're filling your cell along the lines of:
Range("A3").Value = Range("A1").Value - Range("A2").Value
So solve your Problem, use this:
Range("A3").FormulaLocal = "=SUM(A1;A2)"

Related

For Excel VBA, If B2 value did not change then A2 will keep same, If B2 is changed then A2 will increase by one

I need a VBA for the whole column B and A says that
if Value of Cell B2 did not change then A2 value will be the same
if B2 value is changed then A2 value will increase by one
Use this formula.
Put your staring number in A1.
Put this in A2:
=IF(B2<>B1,A1+1,A1)
And copy down.

VBA Excel - Transform Column value To another Column along with inserting blank row

I want to tranform the values.
Suppose in A1:A20 there are 20 values and in B1:B20 also 20 values.
I want to copy values of column B in such manner that outcome should be:
Cell A1 value is A1, cell A2 value is B1, cell A3 value is A2, cell A4 value is B1.
Likewise in cell A40 value is B20.
(for more clarity i have attached image)
So here first step is inserting blanck cells alternatively in column A
and than copy value of column B in that blank cells.
For more clarity Please find the image
.
Always post what you have tried first !
Sub test()
Application.ScreenUpdating = False
Dim i As Long
For i = 2 To 20 Step 2
Sheets("Sheet1").Range("A" & i).EntireRow.Insert
Next i
Sheets("Sheet1").Range("B1:B40").copy
Sheets("Sheet1").Range("A2").PasteSpecial Paste:=xlPasteAll, SkipBlanks:=True
Application.ScreenUpdating = True
End Sub
Maybe this is not exactly what you are looking for but an alternative solution is to use a regular formula like below.
=IFERROR(INDEX($A$2:$B$9,ROUNDDOWN(ROW()/2,0),ISODD(ROW())+1),"")
But this won't give the color you are looking for. Just throw out an alternative method for your reference.

VBA - If Loop for two sets of Data

So I have two columns of data and I want to add a date next to data that has two sets of values. So if A3 = Trucks and B3 = 2008 appear in the two columns I want C3 to have a date value of 11/1/2016. If these values A3 = Trucks and B3 = 2008 appear anywhere else in the data I want the date value to increase by 1 to 11/2/2016 and have that run until the data is fully queried.
This is what i understand from your question
if A3 = Trucks, B3 = 2008, C3 = 11/1/2016. then
if A10 = Trucks, B10 = 2008, then C10 = C3 +1 i.e. 11/2/2016
if my understanding is right, the below formula should work. Here is a google spreadsheet.
IF(MAX(--($A$1:A8=A9)*--($B$1:B8=B9)*ROW($A$1:A8))>0,OFFSET($C$1,MAX(--($A$1:A8=A9)*--($B$1:B8=B9)*ROW($A$1:A8))-1,0)+1,"New Date")
Please note the formula is an array formula i.e. press Ctrl+Shift+Enter after typing in cell. Also I have added an if clause, in case the item is appearing for the first time in the list, it will show "New Date".

VBA Resize Array, 1 Column to 4 Columns

So I copied some data into Excel, but unfortunately for me when I pasted the data the chart format sorta died, so I ended up with a 1 column full of data(Column A). Basically A1 is suppose to be Movie name, A2 is suppose to be in B1(Cost of movie), A3 is suppose to be in C1(How long is the movie), and A4 is suppose to be in D1(Sequel:yes/no). And A5 is suppose to be in A2, A6 in B2, A7 in C2, A8 in D2, A9 in A3, A10 in B3..etc.. Its suppose to be a chart with 4 columns, but everything ended up in column A. Anyone can help me write a VBA code to rewrite the first column back to 4 columns? Thanks in advance.
No need for VBA. In excel, use INDIRECT in the four columns
=indirect("A"&(row()-1)*4+1) | =indirect("A"&(row()-1)*4+2) | ...
=indirect("A"&(row()-1)*4+1) | =indirect("A"&(row()-1)*4+2) | ...
...

convert two number to a range

I am going create a macro to convert two number to a range, e.g.
On excel cell A1 and A2, I input integer 1 and 5 respectively. The output will look like:
A3 1
A4 2
A5 3
A6 4
A7 5
How can I do that?
You can accomplish this without even using macros :)
in A3 type your first number.
On the Home tab go to the Editing section > Fill > Series...
Choose to have your series populated in Columns. Choose a stop value of 20 (or whatever)
Click OK
Now you will have a series of numbers 1-20 going down starting in Cell A3.
You can change your step value so that each number is incremented by 1, or 2 or 3 or whatever. You can also fill in rows instead of columns.
If you need it more automated than that just do all of that while recording a macro and see what it does then change the VBA to suit you more specific needs/come back here with some code to get advice on.
The Below COde will read the Content from the A1 and A2. According to the range it will display the result in the A series only.
enter code here
For i = Cells(1, 1) To Cells(1, 2)
Cells(1, i + 2) = i
Next i