Using Excel Formula in VBA code with variable ranges - excel

I'm currently having a problem finding a way to vba an excel formula for all my columns in a worksheet. The number of rows in this worksheet is set to change from use to use hence it has to have a variable range.
The table currently looks like this:
I've got the value for noOfRowsOfData using:
Worksheets("Posttrans").Range("A1").Select
noOfRowsInPosttrans = Selection.CurrentRegion.Rows.Count - 2
The formula I am trying to put at the bottom of the table is:
=IF(noOfRowsInPosttrans=COUNTIF(A3:A688,A2),TRUE,FALSE)
Where 688 is set to change to noOfRowsInPosttrans
This will keep it dynamic. I'm just wondering how I would go about inserting the formulas displayed.
Any help would be great!
Thanks,
PineappleBeans

A6 = "=IF(COUNTIF(A$3:A5,A$2)=(CELL("row",A6)-3),TRUE,FALSE)"
Copy A6 to B6
EDIT: getting kind of crazy, but to show the formula:
Function function1(zcell As Range)
function1 = zcell.Formula
End Function
Then A8 = "=function1(A6)" and B9 = "=function1(B6)" gives

Related

Creating a cell that updates to new value

I am wondering if there is any way to create a cell that automatically updates to the next in a series in Excel.
If Column 2 contains any of these:
2A
2B
2C
Then the cell would say use 2D next. And then when that column shows 2D, then use 2E, and so on. I have tried using an if function, but I can't seem to get it to work. I plan on having the list of available entries on a separate worksheet that the data will draw from.
Thank you in advanced!
You could try this:
Have the text "2A" in cell A1.
In cell A2, paste this formula:
= LEFT(A$1,LEN(A$1)-1)&CHAR(CODE(RIGHT(A$1,1))+ROWS(A$1:A2)-1)
And drag down as necessary.
EDIT
Based on your comment, you can modify cell A2 to this:
= IF(RIGHT(A1,1)="Z",(LEFT(A1,LEN(A1)-1)+1)&"A",
LEFT(A1,LEN(A1)-1)&CHAR(CODE(RIGHT(A1,1))+1)

Excel: left of a reference

The cell C4 points to cell E4 (but in my real workbook E4 is another sheet). I want cell C5 to point to the right of whatever cell C4 points to. So I only have to change C4 pointer to make C5 change as well - always to the cell to the right of whatever C4 points to. How do I do this?
Offset() and Indirect() work perfectly together like this, if you add an option to read the formula from a VBA custom function:
=OFFSET(INDIRECT(CellFormula(C4));0;1)
Option Explicit
Public Function CellFormula(Rng As Range) As String
CellFormula = Right(Rng.Formula, Len(Rng.Formula) - 1)
End Function
You do not need the custom function, if you are using Excel 2013. Then FormulaText does exactly the same. Just make sure to eliminate the equal (=) sign on the left from FormulaText, if you are using it.
if VBA is an Option
Range("c5").Value = Range("c4").Precedents.Offset(0, 1)

Excel, Create a named range from the contents of cells usign cell formulas

I am stuck...
I have a 100 Row sheet with 10 Columns. This list is broken into classes simply by inline headers. I have definitions of the blocks of data under each head, for example:
UNASSOCIATED A2 A19
HOSTS A21 A32
ROOF A34 A100
I compute those ranges as they may change from time to time. I need
first: Define Name of each group by formula
Second: From time to time select the defined group
I am unable to get a formula to work which will allow me to use the "Content" of the cell as opposed to the cell location to define these ranges.
I'm stuck.
Suggestions would be appreciated.
Regards,
RHD
This cannot be done in a cell formula. You'll need to create a macro (VBA) to do this. Essentially, what you need is to grab the value of a cell then use that value as if it were a cell address.
Take a look at the example macro below. If cell A1 contains the characters "B22", then x will become the value of A1 (which is "B22") and "test" will be written to cell B22. This cannot be done in with cell formulas.
Sub test()
Dim x As String
x = Range("A1").Value
Range(x).Value = "test"
End Sub
A good amount of adaptation will been needed to incorporate this technique, and you'll need to play with this a bit. Ping this community to help answer more specific questions as they come up.

Using IF Function Range in Excel

I have some problem in using If Excel function. I want to use range in excel. So when my data is in range, it will show something. I use this formula but it doesn't work "
=IF(E11=Rekap.C8:C21, VLOOKUP(Rekap.C8:C21, Master.A2:C148, 3), "")
Form that formula, if data in E11 is same like data in range C8 - C21 in Rekap sheet, it will show another data in Master sheet that in range A2-C148 column 3. How can I use range in if formula ?
A single cell like E11 cannot be compared to a range like C8:C21.
I assume that you really want to check if the value in E11 appears anywhere in the range C8:C21 and if so, perform the lookup. That can be done in several ways
=if(isnumber(match(e11,Rekap!$C$8:$C$21,0)),vlookup(E11,Master!$A$2:$C$148,false),"")
Note that I added the fourth parameter to the Vlookup. It will default to TRUE if omitted, which may return wrong results if the lookup table is not sorted.

How to extract column from cell (Excel, VBA)

I'm running into a (run-time error '1004': Application-defined or object-defined error) error while trying to write my first Excel VBA Macro. I've looked at similar questions on stack overflow and other sites, but my issue seems to be more basic than issues others are having.
Currently, I'm trying to take the data from two cells from one sheet and write them to another sheet. I understand that trying to find the column or row of a cell that I reference by cell or row is unnecessary, but eventually I will use for-loops and will substitute the specific cell references with variables.
Here are the two lines of code that I have:
Worksheets("Sheet2").Range("A1").Value = Worksheets("Sheet1").Range("A" & Worksheets("Sheet1").Range("B2").Row).Value
Worksheets("Sheet2").Range("B1").Value = Worksheets("Sheet1").Range(Worksheets("Sheet1").Range("B2").Column & "1").Value
The first line runs fine. It writes Sheet1's A2 to Sheet2's A1.
The second line does not run, does not write Sheet1's B1 to Sheet2's B1, references the error, and I'm not sure why.
Thank you for your time and help!
The .Columns property returns an integer, not a letter. Use the Range.Cells property if you want to define a range with a numerical row and numerical column.
Worksheets("Sheet2").Range("B1").Value = Worksheets("Sheet1").Cells(1, Range("B2").Column).Value
I suppose there is a larger purpose to this but as it sits, it is very verbose code.
your last lines from your question say that you want to fill Sheet2-A1 with Sheet1-A2 and Sheet2-B1 with Sheet1-B1
the most straightforward way is
Worksheets("Sheet2").Range("A1") = Worksheets("Sheet1").Range("A2")
Worksheets("Sheet2").Range("B1") = Worksheets("Sheet1").Range("B1")
I am a beginner at VBA just like you. You don't need to know everthing in VBA to make productive use of it. I made my first macro by RECORDING it, and it worked (but was hopelessly inefficient).
I had a problem similar to yours: looking up data on another worksheet. I created the following function:
Function GetMyNumber(C3ll)
MyCol = C3ll.Column
GetMyNumber = MyCol
End Function
To use it in a spreadsheet, just enter the formula into some cell, like D5,
=GetMyNumber(D5)
When you recalculate, the number 4 appears in cell D5. If you copy cell D5 into Cell F3, you will see in F3, =GetMyNumber(F3), and calculate will return a 6. Of course you can fill down or across, the argument is changed to the cell the formula is in. And if you want, you can offset with an argument referring to any cell.
Once you get this working, you can insert the code to do you matching and other tasks that make use of your column number MyCol to extract the number from the other worksheet. Remember, MyCol is an integer.
Hopes this helps.

Resources