I'm trying to make an automated sum based on an increasing number of rows.
Here is my sample of code but I'm stuck as I don't know the syntax to include a variable in the formula of a cell.
Sheets("Orderboek").Select
Range("H" & (rOi + 1)).FormulaR1C1 = "=Sum(H3:H"&rOi&")"
My variables are declared as follows:
Dim i As Integer, a As Range 'i= index a een gebied
Dim prText As String 'product text
Dim rOi As Long 'rij nummer in orderboek
Dim rng, sumrng As Excel.Range
Dim r As Long 'rij number in offerte
Dim Tot As Long 'totaal som van offerte
Dim ws As Excel.Worksheet
Thanks very much if you can help me out.
Try this:
Range("H" & (rOi + 1)).Formula = "=Sum(H3:H" & rOi & ")"
Make sure you use .Formula and not .FormulaR1C1 in this case. Also make sure that there are spaces between ampersands.
Note that FormulaR1C1 is useful when you want to refer to a certain point and would like to enter row and cell coordinates. Since you know the column you want to work with, using Formula is better. i'll delete soon; just can't comment yet (<50rep)
*edit: so #timthebomb is spot-on. :)
Related
I have a vector of values (C8:AM8) in an excel worksheet (XYZ). Using VBA code, what I need to do is copy and paste those cells starting at C11, BUT if C11 has a value, then I need to offset by 1 row and paste the above values (C8:AM8) in C12, or if C12 has a value, then the next empty row.
Basically, as of now, I have something that looks like this....
Dim CellName As String
Dim CheckCell As Range
Dim RowA As Range
CellName = "C8:AM8"
For Each CheckCell In Sheets("XYZ").Range("C11:C50")
If Not IsEmpty(CheckCell.Value) Then
Set RowC = CheckCell.Offset(1, 0)
Else
Range(CellName).Copy Range("C11" + RowC)
End If
Next CheckCell
Minimally, I know that the issue is here:
Range(CellName).Copy Range("C11" + RowC)
I am new to VBA and so I'm not sure of the proper syntax to use. I've tried multiple permutations with the RowC inside the parentheses, outside the parentheses, in quotes, outside of quotes, etc. and I can't seem to nail it. I think this issue is very simple, but I am clearly missing something critical and can't seem to find what I need online.
Any help would be much appreciated.
Thank you!
I'd go with:
With Sheets("XYZ")
.Range("C8:AM8").Copy .Range("C" & WorksheetFunction.Max(11, .Cells(.Rows.count, "C").End(xlUp).Offset(1).Row))
End With
The following line copies your range after the last non-empty cell in row C:
Worksheets("XYZ").Range("C8:AM8").Copy Worksheets("XYZ").Range("C1000000").End(xlUp).Offset(1)
You need to put this line of code:
Range(CellName).Copy Range("C11" + RowC)
Like this:
Dim my_row as integer
my_row=11
Range(CellName).Copy Range("C" & (my_row + RowC))
Hello stackoverflow community,
I must confess I primarily code within MS Access and have very limited experience of MS Excel VBA.
My current objective is this, I have an expense report being sent to me with deductions in another countries currency, this report has many columns with different account names that may be populated or may be null.
I currently have a Macro that will open an input box and ask for the HostCurrency/USD Exchange rate, my next step will be to start at on the first record (Row 14; Column A-K contains personal info regarding the deduction) then skip to the first deduction account (deduction accounts start at column L and span to column DG) checking if each cell is null, if it is then keep moving right, if it contains a value then I want to multiply that value by my FX rate variable that was entered in the input box, and update the cell with the converion. Once the last column (DG) has been executed I want to move to the next row (row 15) and start the process again all the way until the "LastRow" in my "Used Range".
I greatly appreciate any feedback, explanations, or links that may point me towards my goal. Thank you in advance for taking the time to read though this!
First off, you really should attempt to write the code yourself and post what you have so someone can try to point you in the right direction. If your range is going to be static this is a very easy problem. You can try something along the lines of:
Sub calcRate(rate As Double, lastrow As Integer)
Dim rng As Range
Set rng = Range("L14" & ":DG" & lastrow)
Dim c As Variant
For Each c In rng
If c.Value <> "" Then
c.Value = c.Value * rate
End If
Next
End Sub
This code will step through each cell in the given range and apply the code without the need for multiple loops. Now you can call the calcRate sub from your form where you input the rate and lastrow .
This will do it without looping.
Sub fooooo()
Dim rng As Range
Dim mlt As Double
Dim lstRow As Long
mlt = InputBox("Rate")
With ActiveSheet
lstRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Set rng = .Range(.Cells(14, 12), Cells(lstRow, 111))
rng.Value = .Evaluate("IF(" & rng.Address & " <>""""," & rng.Address & "*" & mlt & ","""")")
End With
End Sub
If your sheet is static you can replace ActiveSheet with WorkSheets("YourSheetName"). Change "YourSheetName" to the name of the sheet.
I'm not sure exactly what to call what I'm trying to do, so searching it has been tough. Basically I have a table of equations, each row has a different equations/references a different column, but all of them reference the same range of rows. i.e. Eq. A = average(A200:A400), Eq. B = sum(C200:C400), etc...
From file to file the range of rows changes, so what I want to do is be able to do is enter the start and end rows into cells and have them auto populate the equations. If anyone could tell me how to do it just for one cell and not an entire table, I could figure it out from there.
Thanks!
Sounds like the INDIRECT function would accomplish this. It allows you to enter in text to be interpreted as a cell reference.
For instance, lets say you wanted the range to cover A200:A400 for a given sheet, and you wanted that desginated in cell A1 of that sheet. In cell A1 you would just type in "A200:A400" then, in the actual equations, you would have:
=AVERAGE(INDIRECT(A1))
You can obviously split this further down, but thats the concept of it.
You could create a form with a few text boxes. Enter the start and end row. Then your code could go through and enter the formula.
Something like this.
Dim lRow as long
Dim lEnd as long
lRow = val(txtBoxStartRow.text)
lEnd = val(txtBoxEndRow.text)
ws.Range("A" & lEnd + 1).Formula = "=average(A" & lRow & ":A" & lEnd & ")"
ws.Range("C" & lEnd + 1).Formula = "=average(C" & lRow & ":C" & lEnd & ")"
This should do:
=AVERAGE(INDIRECT(ADDRESS($B$1;ROW())):INDIRECT(ADDRESS($B$2;ROW())))
In that code I'm assuming cells B1 and B2 contain the limits (you can replace these references with hard number), to use your example: B1 = 200 and B2 = 400.
If you then place this code in any row, you'd get average("rowNumber"200:"rowNumber"400).
Address() gives you the right range reference
Indirect() makes a range out of it
Then you can wrap it in whatever function you like.
I'm trying to write a program in VBA for Excel 2011 that can search a column (which column that is is determined by another variable) for the number 1 so that it knows where to start an iteration.
Say that the number of the column is given by colnumvar. The only way I can think of is the Match function, which led me to write the following:
Dim rowvar As Integer
rowvar = WorksheetFunction.Match(1,Range(Cells(1,colnumvar),Cells(1000,colnumvar)),0)
This gave me an error, however. After playing around with it some more, I realized that it must not accept the Cells([row],[col]) way of doing it, but rather wants something like Range("A1:A100"). Unfortunately, I can't do it that way, since the program is figuring out what column to look in. Any help for figuring out how to get past this would be greatly appreciated!
What you mean to do is better served with Range.Find.
Dim rngtrg As Range, rngsrc As Range
Dim ws As Worksheet
Set ws = ActiveSheet
Set rngsrc = ws.Range(ws.Cells(1,colnumvar),ws.Cells(1000,colnumvar))
Set rngtrg = rngsrc.Find(1,...)
rowvar = rngtrg.Row
this easy function retreive the positoin of that you find
Function rowvar(ByRef c As Integer) As Integer
Dim keySrc As Integer
keySrc = 22 'wath you want to find
rowvar = WorksheetFunction.Match(keySrc, Range(Cells(1, c), Cells(1000, c)), 0)
End Function
use with rowvar(x)
Update: I figured out my error - the variable sheetRange needed the Sheets("Schedule"). added to it as well
Sorry for the inconvenience.
I have a relatively simple problem in that I am trying to use VBA to find the number of rows in a particular sheet. I am getting a pop up box that just says 400 and am not really sure where my syntax is off.
Sub PhxCheck()
Dim i As Integer, x As Integer, numofRows As Integer
Dim top As Range, bottom As Range, sheetRange As Range
Dim phxContract As String, contractID As String
Set top = Sheets("Schedule").Range("A3")
Set bottom = Sheets("Schedule").Range("A65536").End(xlUp)
Set sheetRange = Range(top, bottom)
numofRows = sheetRange.Rows.Count
Cells(30, 1).Value = numofRows
End Sub
The error happens when I add Sheets("Schedule"). to the top and bottom ranges.
Thanks for your help!
You can simplify your code a lot and avoid using redundant variables which will limit mistakes.
All you need is:
With Sheets("Schedule")
Cells(30, 1).Value = .Range("A3", .Cells(Rows.Count, "A").End(xlUp)).Rows.Count
End With
I understand you solved your problem yourself, but perhaps the above will help you or someone else searching for a similar problem.