VBA - Excel - Filling a cell with a nested IF - excel

I'm using VBA to hack and splice together a macro to update a quickbooks exported report. I'm using a nested IF formula to create a filter for myself.
The formula I'm using is:
=IF(ISBLANK(I2),IF(ISBLANK(I3),IF(ISBLANK(H2),IF(ISBLANK(H3),"X",""),""),""),"")
(This will fill a cell with an X, if the 4 referenced cells are blank, and do nothing if any of them have a value).
The code I tried is this:
Range("N2").Formula = "=IF(ISBLANK(I2),IF(ISBLANK(I3),IF(ISBLANK(H2),IF(ISBLANK(H3),"X",""),""),""),"")"
The problem I'm having is that the " before the X is being read as the end of the code (I think); I'm getting a compile error.
Anyone able to help me out a little?

When you are using excel formula in VBA code, you have to careful with double quote:
Check the below code, it will work for you:
Range("N2").Formula = "=IF(ISBLANK(I2),IF(ISBLANK(I3),IF(ISBLANK(H2),IF(ISBLANK(H3),""X"",""""),""""),""""),"""")"

Related

Excel Adds "#" Sign Into Formula Created by XLSXWriter?

In XLSXWriter, I'm using the following code to add a formula to a workbook:
worksheet.write_formula(row, col, '=MAX(IF(AL3:AL7<=$AI$8,AL3:AL7))')
When I open the workbook in Excel, it has an "#" sign inserted into it, and throws a "#VALUE!" error.
=MAX(IF(#AL3:AL7<=$AI$8,AL3:AL7))
How can I correct this?
Found it. I had to write the formula using write_array_formula instead of write_formula.

How to force a full recalculation of an Excel sheet using vb.net?

I am a beginner at visual basic and this is my first stack overflow question, so I'm sorry if this is a mess. I am trying to take values entered from a windows form and enter them into an excel sheet. I used search and replace to achieve this. I have been able to successfully get that far. My only problem now, is that excel will not calculate the values entered into the sheet. Excel will only calculate those functions if I manually go in and reset each cell (with F2). I have my calculation on automatic as well.
Dim excelReplacements As New List(Of Array)
' Create list to hold hold excel replacements
excelReplacements.Add({"utilCost",
ExclusiveUtilityRate.Value + SharedUtilityRate.Value})
excelReplacements.Add({"intCost", Internet.Value})
excelReplacements.Add({"pCost", PhoneCost})
excelReplacements.Add({"pUnits", PhoneQty.Value})
'Select sheet 2
workbook.Worksheets("Sheet2").Activate()
Try
'loop though excelReplacements and replace every key with it's value
For Each rep In excelReplacements
Dim ranges As CellRange() =
sheet.FindAllString(Convert.ToString(rep.GetValue(0)), False, False)
For Each range As CellRange In ranges
If rep.GetValue(0) Is "pCost" Then
sheet.Replace("pCost", Convert.ToDouble(PhoneCost.Value))
ElseIf rep.GetValue(0) Is "pUnits" Then
sheet.Replace("pUnits", Convert.ToDouble(PhoneQty.Value))
Else
Dim value As Long = Convert.ToDouble(rep.GetValue(1))
sheet.Replace(range.Value, value)
End If
Next
Next
Catch e1 As System.Exception
'if couldn't find the word,give a message
Console.WriteLine("The text doesn't exist the word you want to replace")
End Try
Output.AppendText("Changes Made...")
' save workbook
workbook.SaveToFile(xlssave, FileFormat.Version2013)
workbook.Worksheets.Remove("Evaluation Warning")
xlsApp.Quit()
Output.AppendText("Worksheet Saved" & Environment.NewLine)
I have done a lot of research, but I have only found information on vba, which I am not using. I
I have the following imports:
Imports System.IO
Imports System.Runtime.InteropServices
Imports Newtonsoft.Json
Imports Excel = Microsoft.Office.Interop.Excel
Imports Spire.Xls
Imports System
Imports Microsoft.Office.Interop
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
If you need more of my code I will gladly show you. If there is any way to force a full calculation using vb.net (praying there is) or if you see an error in my code that is causing excel to not calculate the values, please let me know.
Try xlsApp.CalculateFull. The calculation methods work at the application level, not the workbook level.
The .Calculate method also works at the Worksheet and Range level. This may also help as calculating a smaller range will be quicker than doing the full calculation across all open workbooks in the application.
If you modify the VB code, the Excel spreadsheet using it does not realize it's changed and so does not recalculate. However, anything that convinces Excel to recalculate a cell containing a call to the now modified VB function will force it to execute for that cell. Also editing the cell, even just hitting F2 and Enter, will work. However, if you have a lot of cells using the function, it would be a pain. A Solution I've found: do a Find and Replace of the (full) function name and click Replace All... bingo!
I would add the .Calculate Method in order to force calculate.

Deleting watermark in Word from Excel VBA (redux) Difference between Word & Excel VBA

[There is another recent question with a similar title that has not yet been answered satisfactorily, and gave me no insight :-( ]
This is my first attempt to integrate a little Word automation with Excel VBA, so I started with a macro recording in Word VBA to have a clue. Unfortunately, when I try to translate this into Excel, even making allowances for having to use the Excel local wrdDoc to qualify references, the code that works in Word VBA does not work from Excel VBA. Deeply frustrating.
Here is the recorded code snippet from Word that I started with:
[Shape name manually edited after recording to match assigned name for the Watermark image]
ActiveDocument.Sections(1).Range.Select
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.Shapes("US-CA watermark").Select
Selection.Delete
Here is the code snippet from Excel I derived from that:
[sWatermark string predefined to match the picture name]
With wrdDoc
.Sections(1).Range.Select
.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
.Selection.HeaderFooter.Shapes(sWatermark).Select
.Selection.Delete
End With
Result: VBA chokes every time when I try to select the watermark (line 3 within the With block).
I have tried the problem line with and without the . prefix [i.e. as wrdDoc.Selection and just Selection (as in the original Word macro) ].
I have tried accessing the watermark as .HeaderFooter.shapes(1) after verifying that there is only 1 item in the range. Again, this seems to work quite satisfactorily in Word VBA, but never in Excel VBA.
WHY?!?!?!
Avoid using the Selection when possible, as it can be changed during macro execution. For example:
wrdDoc.StoryRanges(wdPrimaryHeaderStory).ShapeRange(1).Delete

How to set a formula in particular cells in excel using VBA?

I want to set the following formula(its already working fine) in first 1000 rows in excel.Actually I am looking in VBA. But, I am not familiar in VBA code.
=IFERROR(VLOOKUP(DD,HDR_COLMN,COLUMN(),0),"")
DD - getting data from another sheet
HDR_COLMN - getting table header from another sheet(Sheet2) and showing as drop down values in Sheet1
Please any one can help me. Advance thanks
The only tricky part is the double-quotes.
Say we want to deposit this with VBA
=IFERROR(VLOOKUP(A1,B1:C10,2,TRUE),"")
This will do it:
Sub luxation()
Range("A10").Formula = "=IFERROR(VLOOKUP(A1,B1:C10,2,TRUE),"""")"
End Sub

How to remove #N/A that appears through out a worksheet? (VBA for excel Required)

I have to clear #N/A that happens through out my worksheet when ever i run my code. I'm not sure why and have been debugging for a while but to no avail. What i could do to remedy this problem is to delete it entirely from the page, where they happens randomly. If anyone know how to, do share a VBA code with me.
Codes of doing a simple copy and paste into another sheet
thevaluestocopy = Sheets("pivot").Cells(thefirstrow, 1) _
.Resize(thelastrow - thefirstrow, 1)
Worksheets("summary").Cells(3, 16) _
.Resize(thelastrow - thefirstrow + 1, 16) = thevaluestocopy
I have nested that code with different Column because my pivot table changes most of the time. And when i copied for the 2nd time, #N/A appears.. Have no idea why and i believe this works work fine.
You can run this line to remove any #N/A error values in the worksheet:
Cells.Replace "#N/A","",xlWhole
If the worksheet contains formulas you can try this:
Cells.SpecialCells(xlCellTypeFormulas,xlErrors).Clear
I don't know, but how about doing something like this instead? This is how I generally copy and paste, without problems.
Sheets("pivot").Range("your range").Copy
Worksheets("summary").Range("your range").PasteSpecial
UPDATE
If you still want to simply remove all the #N/As with your current code, you can use some code like this.
If WorksheetFunction.IsNA(Cells(row, column)) Then Cells(row, column).ClearContents

Resources