I have created a code that adds a new line to my spreadsheet with the information that users enter into a user-form. The problem I'm having is that I have formulas in two of the columns that I need to be carried into the new rows. I have tried several different ways to do this but keep receiving an error message. The formula I need carries is
=IFERROR(INDEX(Equipment!$E$3:$E$1000,MATCH($B5&"-"&$A5,INDEX(Equipment!$B$3:$B$1000&"-"&Equipment!$A$3:$A$1000,0),0)),"")
The values that change line to line are the B and A cells after the MATCH command (in this case $B5 and $A5). Can anyone help me figure out how to convert this to VBA syntax so that I can add it to my code? Thanks in advance!
I Converted this formula, and tested it in a loop.
The formula is copied to Column D from rows 11 to 20, where $B5 and $A5 change according to whatever row they are (I hope I understand what you meant).
Sub ConvertFormula()
' modify to where (worksheet) you want to copy this formula
Set eqp_sht = ActiveWorkbook.Worksheets("Equipment")
' modify this according to whatever rows and column you want this formula copied
For i = 11 To 20
eqp_sht.Cells(i, 4).Formula = "=IFERROR(INDEX(Equipment!$E$3:$E$1000,MATCH($B" & i & "&-$A" & i & ",INDEX(Equipment!$B$3:$B$1000&-Equipment!$A$3:$A$1000,0),0))," & Chr(34) & Chr(34) & ")"
Next
End Sub
Related
I am trying to combine two cells like this:
Lets say that cell 1 is 123 and cell 2 is 321
Then I want cell 3 to look like this:
123 (321)
So if this was in excel and I wanted to do it with two cells, I would just do it like this:
=E2&"("&F2&")"
But in VBA, I'm actually trying to do it with a loop. So that the i value is the row, and the column is constant.
I've tried to do it like this:
ActiveCell.FormulaR1C1 = _
"='Nye_Virksomheder'!R & i & C2 & ""(""&'Nye_Virksomheder'!R & i & C4&"")"""
So "Nye_Virksomheder" is the sheet that i am taking the data from.
But I keep getting an error. Not sure what is wrong. Anyone that is able to figure it out?
Kind regards,
Zebraboard
You have several issues with quotes.
"='Nye_Virksomheder'!R" & i & "C2 & ""("" & 'Nye_Virksomheder'!R" & i & "C4 &"")"""
Note that you don't actually need a loop to write a formula with an increasing row. You can write the formula to the entire range in one go (keeping the row as a relative reference).
I am trying to get an Excel If formula to apply to all rows with content in a sheet. If the cell value entered after the macro is applied is y, then something appears in the next column, if it is n something else appears. It looks like this:
For Row = 2 To LastRow
Cells(Row, 8).Formula = "=IF(""H"" & Row & ""="" ""Y"",""Included"",IF(""H"" & Row & ""="" ""N"",""SYS"", """"))"
Next
I believe the issue is with where I putting the quotes and how many, but I can't figure that out for the life of me. I was able to get the formula to work when I chose a specific cell, ie. H2, but the issue began when I tried to use a variable.
Any help or tips or references would be appreciated!
I find it easier to replace tokens than to use concatenation, as it's easier to manage the quotes that way.
Const FRM As String = "=IF(H<r>=""Y"",""Included"",IF(H<r>=""N"",""SYS"", """"))"
For Row = 2 To LastRow
Cells(Row, 8).Formula = Replace(FRM, "<r>", Row
Next
I'm not sure if this is even possible in Excel but this is what I need to do:
I have a column with a list of hotels and then another column which needs to pull data from each individual hotel's excel file. For example, cell A2 will have the name "Paris" for the hotel and then cell B2 will have the link:
='G:\Hotels\Paris\Paris - Monthly\[Paris_summary_2018.xlsm]Feb'!$CD$89
I have lots of hotels I need to do this for in different sheets. So I need the link to dynamically refer to whatever hotel is in column A and the title of the sheet, for example could I do something like this?
=''G:\Hotels\A2\A2 - Monthly\[A2_summary_2018.xlsm]Feb'!$CD$89
where A2 has the string "Paris". Also is there a way to dynamically refer to "Feb" depending on what sheet I am in the month will be the title
I am also open to using VBA for this.
As long as you don't mind using VBA, you can easily generate the links with something like this:
Sub generate_hotel_links()
Dim r As Range, c As Range
Dim s As String
' This is the range which all the hotel-locations are in
Set r = ThisWorkbook.Worksheets("Sheet1").Range("A1:A10")
On Error Resume Next
For Each c In r
' Generate the formula based on the current cell we are in
s = "=" & Chr(39) & "G:\Hotels\" & CStr(c) & "\" & CStr(c) & " - Monthly\[" & CStr(c) & "_summary_2018.xlsm]Feb" & Chr(39) & "!$CD$89"
' ...and put it in the neighbouring cell
c.Offset(0, 1).Formula = s
Next c
On Error Goto 0
End Sub
On Error Resume Next will make the macro continue no matter what error pops up - the ideal case would be some more robust error handling, or a check for if the workbook / sheet actually exists before attempting to write the formula, but I'll leave it to you to attempt to write this if you feel the need improve the macro.
If you want to just use generic Excel formulas, I'd advice having a look at the question and answers I posted in the comments to your question.
I am not sure if you can use HYPERLINK.
Like this:
=HYPERLINK("G:\Hotels\"&A2&"\"&A2&" - Monthly\["&A2&"_summary_2018.xlsm]Feb!"&$CD$89)
I need to use vba to place an IF function into a cell which is 8 columns to the right of the active cell. The IF function compares the value of the cell 2 columns to the left of the active cell.
So far i have tried various variations of the below but can't seem to get it to work, any ideas?
targetRange.Ofsett(0, 8).Formula = "=IF(" & targetRange.Ofsett(0, -2).Address & " =""FAIL"",""Enter Required Data"","""")"
targetRange.Ofsett(0, 8).FormulaR1C1 = "=IF(R[0]C[-10]=""FAIL"",""Enter Required Data"","""")"
Hello from an unexperienced vba user.. I'm having trouble with the code for autofill to the last row when trying to use named ranges for the column. Everything seems to work fine when I use a hard coding for the column, in this case Column CW, and what I need is to replace this column CW with a named range so that the macro will still work when adding or deleting columns in the worksheet.
I used the following named ranges:
First_Date: This is the header cell of one of the columns (in this case AP5)
Second_Row: This is the range of columns I want to copy the formulas from (AP7:CW7)
Second_Cell: The cell where I want to start to autofill (AP7)
Last_Column: This is column CW that I want to use in the code. Autofill would be up to this column and down to the last row.
After searching in different threads, I came up with the following code that seems to work fine. How can I change column CW to a named range? Or do I need to change the code?
Dim Lr As Integer
Lr = Range("First_Date").End(xlDown).Row 'Searching last row
Rows(Lr).Insert Shift:=xlDown 'Inserting new row
Range("Second_Row").AutoFill Destination:=Range(Range("Second_Cell"), Range("CW" & Lr))
Can anyone assist me here please?
This will get the job done :-)
Sub RangerFiller()
'The Cell that holds the formula B1
OriginalFormula = Cells(1, 2).Formula
'copies formula down to the last column next to B but use can use another column as
'a counting column....the column that hold the last value
Range("B2:B" & Cells(Rows.Count, "A").End(xlUp).Row).Formula = OriginalFormula
End Sub
Someone gave me the solution:
Change
Range("CW" & Lr)
To
Cells(Lr, Range("Last_Column").Column)
I faced a similar problem because I don't want to hard code the cell reference. I found this solution below to be useful, by using "& ______ &" to replace the cell number that can be calculated using input box or formula.
Eg.
cell1 = last row of column A
Range("CW " & cell1 &" :CW & Lr),
where cell1 = any number that can be added via input box/formula.
Hope this helps!