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
Related
SITUATION
I have a sequence of date serial numbers, for example:
42519, 42526, 42533, 42540, 42547, 42554
in cells
B2, C2, D2, E2, F2, G2
respectively.
Eventually there will be 52 date serial numbers, each one representing a Weekly Invoice Date.
You will notice that each one has been incremented by 7 in a previous macro, which presented no problems.
OBJECTIVE
I need to convert these date serial numbers into a format "dd-mmm-yy", using VBA mentioned in Method 2 below (as opposed to copying formulas manually).
So let's say the first date number is 42519 in cell B2.
Method 1.
This method which converts Date Serial Number 42519 to "dd-mmm-yy" format presents no problem to me but is long winded and involves formula copying manually:
Using expression:
Range("B3")="=TEXT(B2,""dd-mmm-yy"")" ' returns 29-May-16 in cell B3
I can even use the expression:
Range("C3")="=TEXT(B2+7,""dd-mmm-yy"")" 'returns 05-Jun-16 in cell C3
Method 2.
This has me stumped and I'm coming up against a brick wall. It was my idea to do something like the following and if I can get this to work I can go ahead and use a loop to generate 52 dates in a row of 52 cells (C3, D3, etc in the format "dd-mmm-yy", each one incremented by 7 days over the period of a year):
Sub sbNumToText_01()
Dim intAdd7 As Integer
intAdd7 = 0
Dim lngSerialDate As Long
lngSerialDate = Range("C2").Value
MsgBox lngSerialDate + intAdd7 'returns 42526, as expected
Range("C3") = "=TEXT(lngSerialDate + intAdd7,""dd-mmm-yy"")"
End Sub
Instead of cell C3 displaying "05-Jun-16", cell C3 displays #Name? and the mini drop down error menu on the left of cell C3 says "The formula contains unrecognised text".
Can any of you out there please explain how to use the TEXT function incorporating variables? Or any other solution along these lines.
Your problem is with the line
Range("C3") = "=TEXT(lngSerialDate + intAdd7,""dd-mmm-yy"")"
If you look at the formula in cell C3 after running the macro, you'll see it contains
=TEXT(lngSerialDate + intAdd7,"dd-mmm-yy")
But lngSerialDate and intAdd7 are VBA variables, not Excel names, so they are meaningless in a worksheet formula and that's why you see the error. What you want to do is convert the sum of those variables to a number before placing it in the worksheet function:
Range("C3") = "=TEXT(" & lngSerialDate + intAdd7 & ",""dd-mmm-yy"")"
I have a cell (A1) which conatins a number, ie. A1 = 4. This means, that I have to clone 4 times a column from a sheet.
It would be very easy, but the trick is, that the cloned columns cells may contain formulas too, which should be shifted by the number of the clone. For the better understanding here is an example:
If the columns contains =A1, the first clone should contain =B1, the second one =C1 and so on.
The copy script, without shifting:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
For i = 0 To Target
ActiveWorkbook.Sheets("Sheet1").Columns(3).Copy Destination:=Sheets("Sheet1").Columns(i) 'Columns(3) is the column that I am cloning
Next i
End If
End Sub
How should I solve this problem?
If you have a formula (=A1 for example) then the column is automatically shifted, if you want a different behaviour, use $ in front of the column or row:
If you write =$A1, then the column will always stay the same but the row can change
If you write =A$1, the column will change but the row will stay the same
If you write = $A$1 both the column and row will stay the same
Here's my example: In A1 i use the formula =A2. I entered 5 into A2 so A1 shows 5 as well. Now i ran this code:
Dim i As Integer
For i = 1 To 4 Step 1
Cells(1, 1).EntireColumn.Copy Destination:=Cells(1, i).EntireColumn
Next i
Now B1 has the formula =B2, C1 has =C2 and D1 has =D2
If you wouldn't want the formula to change, enter =$A$2 into A1
Hope this helps somehow
I have two sheets in a workbook. I want to find the input given in one sheet and increment the count in another sheet. For eg, I am having nearly 25 questions and the answers to the questions would be Yes / No / n/a. I have kept a drop down input selector for these three options. After the 25 questions have been answered with either of these three options, at the click of submit button, I want the count of number of yes, no and N/As. For eg. 9 N/As, 10 Yes and 6 No.
The point is, I am planning to use the Sheet 1 as a form and Sheet 2 as the accumulation of results. So it needs to check the previous counts of Yes/No/N/As and increment the count.
The code I have tried at this point is as follows,
Sub Button3_Click()
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Set sht1 = ThisWorkbook.Sheets("Table 1")
Set sht2 = ThisWorkbook.Sheets("Sheet1")
sht2.Range("A2") = sht1.Range("D8")
sht2.Range("B2") = sht1.Range("D9")
sht2.Range("C2") = sht1.Range("D10")
End Sub
This macro is assigned to the submit button and the macro copies the data(Yes,No,N/As) to another sheet. I want to find the count on A2,B2,C2 and increment it with number of Yes,No and N/As.
Can anybody help me in doing this?
Assuming you have two sheets in your workbook labeled "Table 1" and "Sheet1".
Assuming "Table 1" cell D8 contains the number of Yes's (probably computed by a COUNTIF or COUNTIFS), and similarly D9 contains Number of Nos and and D10 contains the number of N/As.
Your code works, but you seem to mention that you want to increment.
If so, you probably want
sht2.Range("A2") = sht2.Range("A2") + sht1.Range("D8")
sht2.Range("B2") = sht2.Range("B2") + sht1.Range("D9")
sht2.Range("C2") = sht2.Range("C2") + sht1.Range("D10")
If all you want is the total numbers of Yes, No, N/A in the 25 questions, then there is no need for a macro. Assuming that the answers to the 25 questions are in cells D8 to D32, on the other sheet you would write:
Cell A1: Yes
Cell A2: No
Cell A3: N/A
Cell B1: =COUNTIFS('Table 1'!$D$8:$D$32,A1)
You could now copy the formula from B1 to B2 and B3, and they would all work.
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)"
I want to have a threshold value in one cell(A1) and take it as a reference for adding cells.
Suppose I have
A1 - 10
A2 - 4
A3 - 2
A4 - 3
A5 - 4
A6 - 6
I want to add cells based on A1(Threshold).
As A1 is 10, cells from A6:A5 should be added - Result:10
If A1 is 6 then cell A6 should be returned- Result:6
If A1 is 16 then cells from A6:A3 should be added - Result:19
Is this possible without VBA? Can i get count of number of cells in return along with sum?
I've added some progression SUM operation with ROW and OFFSET for the following. Note that I have modified and added to your sample data for more thorough results.
The SUM formula in C2 is =SUM(OFFSET($A$2,0,0,MAX(INDEX((SUBTOTAL(9,OFFSET($A$2, 0,0,ROW(1:99),1))<$A$1)*ROW(1:99),,))+1,1)) anf the COUNT is derived in D2 with =MAX(INDEX((SUBTOTAL(9,OFFSET($A$2, 0,0,ROW(1:99),1))<$A$1)*ROW(1:99),,))+1. TBH, I didn't experiment much with zeroes in the data as I was unsure whether you would want to count them in the progression or not.
You can use the INDIRECT() function.
=SUM(INDIRECT("A6:A"&ROUNDDOWN(A1/2,0)))
for the count use
=COUNT(INDIRECT("A6:A"&ROUNDDOWN(A1/2,0)))
It's hard to hit a moving target but for your revised parameters try the following.
=SUM(OFFSET($A$6,0-MAX(INDEX((SUBTOTAL(9,OFFSET($A$6,1-ROW(1:5),0,ROW(1:5),1))<$A$1)*ROW(1:5),,)),0,MAX(INDEX((SUBTOTAL(9,OFFSET($A$6,1-ROW(1:5),0,ROW(1:5),1))<$A$1)*ROW(1:5),,))+1,1))
=MAX(INDEX((SUBTOTAL(9,OFFSET($A$6,1-ROW(1:5),0,ROW(1:5),1))<$A$1)*ROW(1:5),,))+1
While Excel expects to calculate in a 'down-and-to-the-right' progression, the OFFSET() function will accept parameters to both relocate the starting point and reshape the height and width of the range of cells being summed. Generally, working 'down-and-to-the-right' will allow to leave some breathing room for expansion but you will have to be very careful that you do not attempt to move upwards past row 1 (#REF! error).
OFFSET function
The formula could be simplified if there was any guarantee that nothing of numerical value was ever going to be below A6 but that point has not been addressed so my formulas halt the sum operation at A6.