Change range formula to include multiple if statements VBA - vba

Bonjour!
I am trying to fill some cells with If functions.
In excel this would look like this:
=if(B11="Apple"; "nice";if(B11="Banana";"Also nice";"why?")
If I only use the first part of the if statement the code runs without problem
rng.formula = "=if(B11=""Apple"", ""Nice"", ""why?"")"
However, as soon as I add another if I get an
Run Time Error '1004' Application defined or object-defined error
rng.Formula = "=if(B11=""Apple"",""Nice"",if(B11=""Banana"",""Also nice"",""why?"")"
How can I include several

You are missing a parens:
Sub dural()
Set Rng = ActiveCell
Rng.Formula = "=if(B11=""Apple"",""Nice"",if(B11=""Banana"",""Also nice"",""why?""))"
End Sub

Related

Dynamic ActiveCell address in Excel VBA

Can someone help me debug this VBA macro?
I want the macro to set a value in a cell using a relative address. If the value in ActiveCell is "Primary", I want the macro to put the value "1.000" in the cell immediately to the right of the ActiveCell.
Sub SetSplitPercent()
If Worksheets("Reps To Users").ActiveCell.Value = "Primary" Then
col = ActiveCell.Column + 1
Cells(ActiveCell.Row, col) = "1.000"
End If
End Sub
The above macro throws the error "Application-defined or object-defined error" with the Cells() statement being the offending one.
Thanks in advance for the help.
I've tried several different approaches and haven't been able to get any of them to work without error. This approach seems like the closest I've gotten but still no dice.

Run-time error 1004 with Range.Autofilter in Excel VBA 2016

My code is
With ActiveSheet
.AutoFilterMode = False
.Range("23:23").AutoFilter
End With
This works fine in Excel 2010, but with Excel 2016 I get:-
Run-time error '1004'
AutoFilter method of Range class failed
Also, I can manually click the filter icon in the Ribbon (Under Data > Filter) but cannot do this with VBA code
Any ideas much appreciated.
The AutoFilter gives 1004 error, when you are trying to filter by an empty row. Try to put something on row 23 and to filter it again like this:
Public Sub TestMe()
With ActiveSheet
.AutoFilterMode = False
.Range("23:23").Cells(1) = 1
.Range("23:23").Cells(2) = 2
.Range("23:23").AutoFilter
End With
End Sub
If it works, then you simply do not have values in row 23, thus it cannot apply an autofilter.
In general, the AutoFilter in Excel has some strange behaviour. E.g., if you open a new Excel file and you run the following code:
Public Sub TestMe()
With ActiveSheet
.AutoFilterMode = False
'.Range("23:23").Cells(1) = 1
'.Range("23:23").Cells(2) = 2
.Range("23:23").AutoFilter
End With
End Sub
It will give you the 1004 error. Let's call this time momentum FirstTime.
Then, if you uncomment the two ranges and run it, the AutoFilter would appear.
Now the strange part - delete all cells from the sheet, comment back the two ranges and it really looks like the way it was, at the FirstTime. But if you run the code it will put an AutoFilter on the empty 23rd row without a problem.
Remove the Existing filer and Run it again

VBA Runtime error 1004 when trying to access range of sheet

I am building a small vba script that is merging tables from several workbook into one single worksheet of another workbook. The error is raised when I try to set the destination range's value:
wksPivotData.Range(wksPivotData.Cells(CurrentRow, 1)).Resize(tbl.ListRows.Count, tbl.ListColumns.Count).Value = _
tbl.Range.Value
The error: "Run-time error '1004': Application-Defined or object-defined error"
I went through similar questions, and the general answer is what I found in this one: The selected cell belongs to another worksheet than the one desired.
While this makes complete sense, I still can't figure why my code breaks as I'm only using numerical reference (CurrentRow is a Long) and Resize, which should prevent me from doing such a mistake.
Additionally, I ran a couple quick tests in the Immediate window and it turns out that while the worksheet wksPivotData exists and I can access its name and a cell value, the range function simply doesn't work:
Debug.Print wksPivotData.Name
PivotData
Debug.Print wksPivotData.Cells(1, 1).Value
123
Both of those work but the next one doesn't:
Debug.Print wksPivotData.Range(1, 1).Value
Your last line, Debug.Print wksPivotData.Range(1, 1).Value won't print because you're misuing Range(). I assume you want A1?
When using Range(1,1), you're referring to a non-existent range. If you want to do cell A1, you need
With wksPivotData
myData = .Range(.Cells(1,1),.Cells(1,1)).Value
End with
Since you're using multiple worksheets, I'd use the with statement as above. Another way to write the same thing is wksPivotData.Range(wksPivotData.Cells(1,1),wksPivotData.Cells(1,1)) (You need to explicitly tell Excel what sheet you want to refer to when using Range() and cells().
Finally, for your resize, if I recall correctly, you're going to have to add the same Cell() twice in your range:
wksPivotData.Range(wksPivotData.Cells(CurrentRow, 1),ksPivotData.Cells(CurrentRow, 1)).Resize(tbl.ListRows.Count, tbl.ListColumns.Count).Value = _
tbl.Range.Value
Or, for the same thing, but different way of doing it:
With wksPivotData
.Range(.Cells(currentRow, 1), .Cells(currentRow, 1)).Resize(tbl.ListedRows.Count, tbl.ListColumns.Count).Value = tbl.Range.Value
End With

Excel 2010 VBA: .Names.Add does not work after .Hyperlinks.Add

I have run into a problem in Excel 2010 VBA on Windows 7 64-bit version which I have not been able to solve. The issue can easily be recreated by pasting the code below in a module in a new workbook and run it.
What I want to do is to loop through a number of sheets and add a defined name and a hyperlink on each sheet.
Sub Test()
Dim i As Integer
Dim ws As Worksheet
Dim defName As String
For i = 1 To 2
Set ws = Sheets(i)
defName = "Name_" & ws.Name
ws.Names.Add Name:=defName, RefersToR1C1:="=OFFSET(Sheet3!R1C1,0,0,1)"
ws.Hyperlinks.Add Anchor:=ws.Range("A1"), _
Address:="", SubAddress:="=Sheet3!A1"
Next i
End Sub
Running the code gives the following error on the second iteration, on the ws.Names.Add call: Run-time error '1004: The formula you typed contains an error.
Doing any of the following makes the error disappear:
Change the for iteration to "i = 1 To 1" or "i = 2 To 2"
Put a debug breakpoint inside the for loop and pressing F5 when it has stopped
Change the cell reference to
ws.Names.Add Name:=defName, RefersToR1C1:="=Sheet3!R1C1", i.e. removing the OFFSET command
Adding DoEvents to the first line of the for loop or setting Application.EnableEvents = False does not solve the problem.
Does anyone know the cause of this error or how to get around it? I am thankful for any help.
Edit: The issue occurs no matter what the hyperlink links to. Changing the hyperlink to the following does not solve the issue
ws.Hyperlinks.Add Anchor:=ws.Range("A1"), Address:="http://www.google.com"
Edit2: Managed to recreate the issue with an even simpler code:
Sub Test()
With Sheets(1)
.Hyperlinks.Add Anchor:=.Range("A1"), Address:="http://www.google.com"
.Names.Add Name:="myDefName", RefersToR1C1:="=OFFSET(Sheet1!R1C1,0,0,1)"
End With
End Sub
I solved this by separating the for loops into two loops, one for the .names.add calls and one for the hyperlinks.add calls. This way all the names get defined before the first hyperlink is created.
Sorry if it is not correct of me to post this as an answer.

VBA Concatenating columns from same worksheet in Range

I am having trouble concatenating two columns (variable range) to another column in the same document. Is there something really obvious I'm missing?
Function ConcCol(ConVal_1 As String, ConVal_2 As String)
Range("V30:V500").Select
Application.CutCopyMode = False
With ActiveCell
.FormulaR1C1 = "=CONCATENATE(RC[5],"" "",RC[6])"
End With
Selection.AutoFill Destination:=Range(Destination), Type:=xlFillDefault
End Function
When running the above function, I get the following error:
Error: Run-time error '1004': AutoFill method of Range class failed
I assume it's something to do with only one cell being activated perhaps? My VBA knowledge is limited
Sub ConcCol()
Range("V3:V500").Select
For Each Cell In Selection
Cell.Value = "=CONCATENATE(RC[5],"" "",RC[6])"
Next Cell
End Sub
I think it'd be easier (if you're going with the selection idea), to just use each cell. I think your with statement doesn't want to loop at all, hence error. This works, a simple solution.
I think this is what you're looking for:
Sub tgr()
With Intersect(ActiveSheet.UsedRange.EntireRow, Range("V30:V" & Rows.Count))
.Formula = "=AA" & .Row & "&"" ""&AB" & .Row
End With
End Sub
Hey I think you are a bit confused. How do you plan to run this code? You need to provide more details. Here are three ways of running a vba code:
User Interface as a User Defined Function (UDF)
As a Function that is called/used by another VBA code
AS a macro and user need to run it from the macro list
For your purpose, why doesn't the user use CONCATENANTE function in the sheet and fill down?
Maybe use the Excel array functions?
I cannot post images, need 10 reputations :s