Inserting formulas w/ FOR NEXT Loop - excel

Couldn't find a thread that would answer my question, os here I am. Please provide a link if there is one that I have not found.
Using Excel 2010
The comments should be sufficient to show what I'm trying to do.
I'm getting the Application-defined or Object-defined error (Runtime error 1004) I just can figure it out. Any help would be appreciated. Side note: I can get the loop to work when a formula like =if(A1=B1,"",3) If actually places the formula in the cell and show 3 in each cell when the condition is met. it just adding the Column,Row, I've tried & Range($, C) &, all sorts of combinations so, show me how much of a boob I am and help me with the SIMPLE fix that eludes me.
Thanks in advance.
Private Sub CommandButton1_Click()
Dim R, C As Integer
Dim Frmla1, Frmla2, Frmla3 As String
R = 4 'Initial Row #
C = 2 'Initial Column #
Frmla1 = "=IF('Log Sheet'!" '1st half of the formula
Frmla2 = "="""","""",'Log Sheet'!" '2nd half of the formula
Frmla3 = ")" 'Closing Parenthesis
' The Cells should have incremental Column,Row Identifiers.
' The following is what I want in each cell.
' The problem is trying to get the B4 and B5 into the formula.
' Formula "=IF('Log Sheet'!B4="","",'Log Sheet'!B4)
' Formula "=IF('Log Sheet'!B5="","",'Log Sheet'!B5)
For R = 4 To 301
ActiveSheet.Cells(R, C).Value = Frmla1 & R & C & Frmla2 & R & C & Frmla3
R = R + 2
Next R
End Sub

It looks like you should be using the R and C to reference a Range.Cells property that you can return a Range.Address property from.
For R = 4 To 301 Step 2
ActiveSheet.Cells(R, C).Formula = _
Frmla1 & Cells(R, C).Address(0, 0) & Frmla2 & Cells(R, C).Address(0, 0) & Frmla3
Next R
I've also removed your R = R + 2 and changed the Step of the For ... Next to increment by 2. You shouldn't self-increment a For ... Next inside the loop.
I changed the Range.Value property assignment to Range.Formula property. While your method often works, it wasn't correct and if the cells were formatted as Text, the formulas would come into the cells as text-that-looks=like-a-formula.
Your variable declarations should be more like the following.
Dim R As Long, C As Long
Dim Frmla1 As String, Frmla2 As String, Frmla3 As String
Each declaration should carry a variable type. Without it, they are declared as object/variant types,

Related

Excel VBA Trying to write a "MAX" formula to Cells with different Ranges with For-Loop

I am trying to make VBA write a formula into different cells that will find the maximum value for a Range decided by some variables. My variables I and J are (numbers/Integers).
Here is my code.
Sub AddMAX()
Dim I As Integer
Dim J As Integer
Dim L As Integer
I = InputBox("Number of columns to check max value")
J = InputBox("Number of Rows to add formula inn and find max value of that row")
For L = 5 To 4 + J
Worksheets(1).Cells(L, 4 + I).Formula = "=" & Max(Range(Cells(L, 4), Cells(L, 3 + I)))
Next L
End Sub
Have tried to re-write the second part (part behind the equal sign) several times. Usually I get the message Compile error: Sub or Function not defined and it marks the "Max". I thought Max (also tried with big letters) was an in-built function like SUM and so on.
I'm trying to make it write an Excel formula like this into the cells:
For I=2 and J=3:
Cell F5: =MAX(D5:E5)
Cell F6: =MAX(D6:E6)
Cell F7: =MAX(D7:E7)
i.e. I want a formula in the cells like I had wrote it in the cells manually to calculate max value, so that if the value in Cells D5, to D7 and E5 to E7 change, the new max value will be found without any scripts having to run.
Let me know if something is unclear.
You should not be putting Range and Cells in a formula string, they mean nothing to the Excel formula engine. You need the Address of the cells:
Dim I As Long
Dim J As Long
Dim L As Long
I = InputBox("Number of columns to check max value")
J = InputBox("Number of Rows to add formula inn and find max value of that row")
L = 5
With Worksheets(1)
.Range(.Cells(L, 4 + I), .Cells(4 + J, 4 + I)).Formula = "=MAX(" & .Cells(L, 4).Address(False, False) & ":" & .Cells(L, I + 3).Address(False, False) & ")"
End With
The formula is actually the same for all cells, which is why it is possible to assign it in one assignment for the entire range. It looks different in the A1 reference notation, but if you switch to R1C1 in the Excel settings, you will see they are the same. Which also means it is easier to create that formula using the R1C1 notation in the first place:
Dim I As Long
Dim J As Long
Dim L As Long
I = InputBox("Number of columns to check max value")
J = InputBox("Number of Rows to add formula inn and find max value of that row")
L = 5
With Worksheets(1)
.Range(.Cells(L, 4 + I), .Cells(4 + J, 4 + I)).FormulaR1C1 = "=MAX(RC[-" & I & "]:RC[-1])"
End With
But it would appear to me that you should instead use the Excel interface the intended way. Select the cells in which the MAX formula should be. Keeping the entire range selected, put the MAX formula into any of its cells as if you were creating it for just that cell, but instead of pressing Enter, press Ctrl+Enter.
You have to be careful to distinct between the part that is seen by VBA and the final formula.
If you write
Worksheets(1).Cells(L, 4 + I).Formula = "=" & Max(Range(Cells(L, 4), Cells(L, 3 + I)))
Max (and all the following stuff) is seen by the VBA-interpreter, not Excel. But there is no Max-function, and you get an (compiler)-error.
If you write
Worksheets(1).Cells(L, 4 + I).Formula = "=Max(Range(Cells(L, 4), Cells(L, 3 + I)))"
the VBA-interpreter sees the whole stuff as a string. It cannot take care about variables like L or I because is doesn't see them. So you end up with a formula that is exactly like you write it - and Excel (not VBA) will show you an error because it doesn't understand L or I.
What you need is a statement (in VBA) that creates a string that contains the actual values of your variables, and assign it to the cell.formula. I strongly advice that you first assign this to a string variable - it makes debugging much easier:
Dim formula As String
formula = "=Max(Range(Cells(" & L & ", 4), Cells(" & L & ", 3 + " & I & ")))"
Debug.Print formula
Worksheets(1).Cells(L, 4 + I).Formula = formula
Update: Sorry, I haven't looked to the content of the formula at all, of course the Range and Cells-objects are VBA objects. What you need in your formula is the address of the range, so change the line to
formula = "=MAX(" & Range(Cells(L, 4), Cells(L, 3 + i)).Address & ")"
Now VBA will create a Range and put the address into the formula string.

How to use dynamic Match formula inside a vlookup in VBA?

I need a code in VBA which is equivalent to match inside a vlookup in Excel.
Have got 2 sheets one MasterSheet and another required_data_sheet.
There are 18 columns in MasterSheet and in required_data_sheet there are only 11 out of 18 columns of MasterSheet (not all of these 11 columns are in same order as first 11 columns in MasterSheet). The first column in both sheets is item_id which is the primary key. MasterSheet has 45000 records and Required_data_sheet has only few hundreds rows with only 1st column (which is item ID) populated and for these few hundred other 10 (11-1st column) needs to be populated.
I can use below formula in required_data_sheet to get data from mastersheet, which gives correct result. I only write this formula in cell A2 of required_data_sheet and then copy the same in all cells.
=VLOOKUP($A2,Master,MATCH(B$1,Master[#Headers],0),FALSE)
Master is the table name of data in MasterSheet.
Problem is I am unable to write the correct VBA code for this. Code is problematic in the Match formula part.
Below is my code and Match formula is preventing it from giving the desired result.
Sub Fetch_Specific_Columns()
Dim lastrow As Long
Dim lastcolumn As Integer
Dim c As Integer
Dim r As Long
lastrow = Range("A" & Rows.Count).End(xlUp).row
lastcolumn = Range("A1").End(xlToRight).Column
For c = 2 To lastcolumn
For r = 2 To lastrow
Worksheets("Required_Data_Sheet").Cells(r, c).Formula = "=VLOOKUP(A" & r & ",Master,MATCH(" & Worksheets("Required_Data_Sheet").Cells(1, c) & ",'Master Sheet'!$A$1:$R$1,0),FALSE)"
Next r
Next c
End Sub
Expected result is somehow match formula works in the desired fashion.
The problem with your code is this portion:
MATCH(" & Worksheets("Required_Data_Sheet").Cells(1, c) & ",
It is inserting the value from cell(1,c) directly into the formula without quotes. This is incorrect excel formula syntax. Instead you should either:
Insert a cell reference (e.g. B3) or
The value of the cell in quotation marks.
Here is the whole line with the correction for method 1:
Worksheets("Required_Data_Sheet").Cells(r, c).Formula = "=VLOOKUP(A" & r & ",Master,MATCH(" & Worksheets("Required_Data_Sheet").Cells(1, c).Address & ",'Master Sheet'!$A$1:$R$1,0),FALSE)"
Here is the whole line with the correction for method 2:
Worksheets("Required_Data_Sheet").Cells(r, c).Formula = "=VLOOKUP(A" & r & ",Master,MATCH(""" & Worksheets("Required_Data_Sheet").Cells(1, c) & """,'Master Sheet'!$A$1:$R$1,0),FALSE)"

Trouble running a macro which selects every second column in a range and inputs the result in a formula

I have a macro that selects every second column and inputs the addresses in to an excel defined function:
Dim calcrange As Range
Dim c As Long
Set calcrange = Range("InvestmentOutlay")
For c = 3 To Range("C57").End(xlToRight).Column Step 2
Set calcrange = Union(calcrange, Cells(57, c))
Next
Range("IRR").Formula = "=IRR((" & calcrange.Address & "))"
This code works, however, when I try to run this code:
Dim npvRange As Range
Dim n As Long
Set npvRange = Range("C57")
For n = 3 To Range("C57").End(xlToRight).Column Step 2
Set npvRange = Union(Range("C57"), Cells(57, n))
Next
Range("NPV").Formula = "=NPV((EconGrowth1," & npvRange.Address & "))"
I get an "Application defined or object defined error". The debug highlights the last line of code. I'm still pretty new with VBA, and am unsure what is causing this error, and how to fix it. Any assistance would be greatly appreciated, thanks,
The brackets you are placing around both the EconGrowth1 and npvRange values is making them into one parameter, but EconGrowth1 should be the first parameter, and the other cells should be the second parameter.
So you need to move your brackets:
Range("NPV").Formula = "=NPV(EconGrowth1,(" & npvRange.Address & "))"
And, as SJR mentioned in a comment, your Union should be changed to:
Set npvRange = Union(npvRange, Cells(57, n))
And, as you have already initialised npvRange to be C57, you can start the loop at column 5.
The final code could look like:
Dim npvRange As Range
Dim n As Long
Set npvRange = Range("C57")
For n = 5 To Range("C57").End(xlToRight).Column Step 2
Set npvRange = Union(npvRange, Cells(57, n))
Next
Range("NPV").Formula = "=NPV(EconGrowth1,(" & npvRange.Address & "))"
SJR has just pointed out in a comment that the NPV formula doesn't even need brackets because, unlike the IRR formula which only takes one parameter for values, the NPV formula has parameters of rate, value1, [value2], ....
So that means the Formula line can be just:
Range("NPV").Formula = "=NPV(EconGrowth1," & npvRange.Address & ")"

excel VBA index and match functions

I am trying to change excel functions into vba code. the formula below in Col AC, Row 2...
=IF(ROWS($1:1)< MATCH(0.01,H$2:H$10)+1,"",INDEX(X:X,ROWS($1:1)-MATCH(0.01,H$2:H$10)+1))
...scans the first 10 rows of Col H.
This formula looks for the first none-zero value in the rows of Col H. When it finds that row, then the values in col X will be printed out in Col AC so that the row in Col AC matches the row with the first non-zero value in Col H.
I hope that description makes sense. It works perfectly in excel worksheet. Now, i would like to change it into VBA code, here is what I have...
For i = 2 To lengthRows
With Application.WorksheetFunction
Range("AC" & i) = .IF(Rows(1) < .Match(0.01, Range("H2:H10")) + 1, "", .Index(Columns(24), Rows(1) - .Match(0.01, Range("H2:H10")) + 1))
End With
Next i
...Rows(1) is the first row and Columns(24) is Col X.
When I run the code, I am getting a run-time error mismatch '13: Type mismatch.
I am trying to understand how this previous question was answered: Excel VBA: how to solve Index and Match function type mismatch error
Taking the example from the previous answered question. You're pushing the match result into the index formula. If the match result doesn't find a match then it'll return Error 2042, which when pushed into the Index formula gives the mismatch error.
To adapt that solution for your example would be as follows:
Dim rw As Variant
With Application.WorksheetFunction
For i = 2 To lengthRows
rw = .Match(0.01, Range("H2:H10")) 'Is there a reason you're not specifying the third parameter 0 for exact match?
If Not IsError(rw) Then
Range("AC" & i) = .If(Rows(1) < .Match(0.01, Range("H2:H10")) + 1, "", .Index(Columns(24), Rows(1) - .Match(0.01, Range("H2:H10")) + 1))
Else
' Do something else if there was an error
End If
Next i
End With
I think once you want to use VBA, you need to use the VBA added capabilities, and not stick with the formula you constructed in Excel.
Since, you are looking for the first cell in Column H with a none-zero value, you can easily find it using the Application.Match, but you need to set the third parameter of match to -1 (means Greater than, looking for a match for values > 0.01).
So now, we have the row number, if you want to find the value in Column X for this row, you can use Range("AC2").Value = Range("X" & MatchRow + Rng.Item(0).Row).Value
Code
Option Explicit
Sub ConvertFormulaToVBA()
Dim MatchRow As Variant
Dim Rng As Range
Dim lengthRows As Long, i As Long
lengthRows = Cells(Rows.Count, "H").End(xlUp).Row '<-- get last row with data in Column H (in your example it's 10)
Set Rng = Range("H2:H" & lengthRows) ' <-- set the range to H2 until last row in Column H
MatchRow = Application.Match(0.01, Rng, -1) ' <-- setting the third parameter to -1, meaning greater than 0.01
If Not IsError(MatchRow) Then
Range("AC2").Value = Range("X" & MatchRow + Rng.Item(0).Row).Value
Else
' raise a message box if there is no Match
MsgBox "No none-zero value found at Range " & Rng.Address
End If
End Sub

Write several values in one string

I am new to both VBA and stackoverflow. So please be patient ;).
I searched for a solution but could not find it.
My problem is as follows:
I have a column (A) with names and then a column (B) where some cells contain an "X" and others do not. I want to know which names have an "X" besides them.
Example:
I want now a string as a result, in one cell.
In this example:
Noah;Jacob;Elijah;Jayden
I got not very far.
For r = 1 To 20
If Cells(r, 2) = "X" Then A = Cells(r, 1) Else
Next
Then "A" is "Noah" and I can write it in a cell, but I want it to find all values and then write them combined, preferable seperated by ; in a cell.
Does anyone have any idea?
Create a string variable, then append your results to that variable based on "X" being in column B. Here's an example of how you could do it:
Sub Foo()
Dim i As Integer
Dim result As String
For i = 1 To 20
If UCase(Cells(i, 2).Value) = "X" Then
result = result & Cells(i, 1).Value & ";"
End If
Next
'// output the result to C1
Range("C1").Value = Left$(result, Len(result) - 1)
End Sub
Excel's native worksheet formulas do not handle concatenating an unknown number of strings together and compensating for the maximum number possible can get messy. A User Defined Function¹ (aka UDF) takes advantage of VBA's ability to process loops through a large number of rows while making numerical or string comparisons 'on-the-fly'.
build_List UDF
Function build_List(rNAMs As Range, rEXs As Range, vEX As Variant, _
Optional delim As String = ";", _
Optional bCS As Boolean = False)
Dim str As String, rw As Long, cl As Long
With rNAMs.Parent
Set rNAMs = Intersect(.UsedRange, rNAMs)
Set rEXs = .Cells(rEXs.Rows(1).Row, rEXs.Columns(1).Column). _
Resize(rNAMs.Rows.Count, rNAMs.Columns.Count)
End With
With rNAMs
For rw = .Rows(1).Row To .Rows(.Rows.Count).Row
For cl = .Columns(1).Row To .Columns(.Columns.Count).Row
If (.Cells(rw, cl).Offset(0, rEXs.Column + (cl - 1) - cl) = vEX And bCS) Or _
(LCase(.Cells(rw, cl).Offset(0, rEXs.Column + (cl - 1) - cl)) = LCase(vEX)) Then _
str = str & .Cells(rw, cl).Value & delim
Next cl
Next rw
End With
build_List = Left(str, Len(str) - Len(delim))
End Function
In D7 (as per image below) as,
=build_List(A:A, B:B, "x")
                               Applying the build_Lists UDf to your sample data
¹ A User Defined Function (aka UDF) is placed into a standard module code sheet. Tap Alt+F11 and when the VBE opens, immediately use the pull-down menus to Insert ► Module (Alt+I,M). Paste the function code into the new module code sheet titled something like Book1 - Module1 (Code). Tap Alt+Q to return to your worksheet(s).
Mate Juhasz answered the question very nice and simple, but now the answer dissapeared.
Mate wrote:
For r = 1 To 20
If Cells(r, 2) = "X" Then A = A & "; " & Cells(r, 1) Else
Next
And for me that solved it perfectly. Now "A" is a string as I wanted. Thank you so much!

Resources