Excel VBA cannot populate cells with "=(" - excel

I'm trying to dynamically populate a couple of cells in Excel with VBA with some simple formulas.
I need a formula like
=(B1-C2)
In Excel
Which is very simple to code... But I get a application or object defined error '1004'. With this simple loop.
For i = 1 To 3
Range("A" & i).Value = "=("
Next i
Therefore I tried to first declare a variable like
dim tmp as string
tmp = "=("
For i = 1 To 3
Range("A" & i).Value = tmp
Next i
But it did not work.
I have also tried to declare it as variant but with no luck.
Does anyone why this happens and how to fix it? Thanks

Related

Extract text string from another cell formula problem

I’m currently trying to include a VBA code into a larger Macro however one particular line (in Part B) is throwing out an error with the FIND function. The objective of the code is to insert two formulas into two diiferrent cells that extract text string from other cell (this formula is copied down accordingly). Below is what I have to date
' Part A
x = Range("A" & Rows.Count).End(xlUp).Row
sConc = "=B2&"" - ""&XLOOKUP(B2,Table4[Code], Table4[Description])"
Range("J2").Resize(x - 1).Formula = sConc
' Part B
y = Range("B" & Rows.Count).End(xlUp).Row
sConc = (LEFT(G1,FIND(" - ",G1)+1)) ' THIS LINE
Range("K2").Resize(y - 1).Formula = sConc
I gather the FIND function will not work within VBA and I need to use the INSTR function however I cannot figure out how to extract the required text ( before the " - ") from the cell using the INSTR function.
Any help on this would be greatly appreciated
Thanks

How do I prevent an excel table's formatting from overwriting a range's existing formatting in VBA?

I'm working with a spreadsheet that requires the use of a table. I resize it to only include the headers and the first line of data I'm working with, but when I then refit it to cover all of my existing data, it overwrites the formatting and resets the color back to default white. I need these colors as they are referenced later in the code. Is there a way to prevent the table from doing this?
Dim FLF As Worksheet
Set FLF = Workbook("My Workbook").Sheets("FLF")
Dim x As Long
Dim lng As Long
With FLF
FLF.Activate
.ListObjects("Table1").Resize Range("$A$6:$K$7")
lng = .Cells(.Rows.count, "D").End(xlUp).Row
.Range("E7:G" & lng).NumberFormat = "0.00%"
.ListObjects("Table1").Resize Range("$A$6:$K$" & lng)
For x = 7 To lng
If .Range("A" & x).Interior.ColorIndex = 46 Then
TopPercent = .Range("K" & x).Value
Do
x = x + 1
.Range("K" & x) = TopPercent * .Range("F" & x).Value
.Range("K" & x).Font.FontStyle = "Italic"
Loop While .Range("A" & x + 1).Interior.ColorIndex = 36
End If
Next x
I tried to recreate the thing you are describing. I think I'm missunderstanding whats happening.
I made a table, put some data below the table, colored the cells yellow and then dragged that little blue thing in the bottom right down over my yellow data to include it in the table. For me it keeps the formatting.
The only thing I can think of, is that you manually applied some form of filling to the table? You could change the table style and formatting. If you want to keep your table, the only thing I can think of is to save the colorindex of the important range before expanding the table and then reformatting that range after you have expanded it. This could be done in several ways but since we are already in VBA, how about this?
'I don't know how to Dim this in one line, sorry
Dim ColorIndexArray()
ReDim ColorIndexArray(ThisWorkbook.Sheets("FLF").Cells(ThisWorkbook.Sheets("FLF").Rows.Count, "A").End(xlUp).Row)
For i = 1 To ThisWorkbook.Sheets("FLF").Cells(ThisWorkbook.Sheets("FLF").Rows.Count, "A").End(xlUp).Row
ColorIndexArray(i) = ThisWorkbook.Sheets("FLF").Range("A" & i).Interior.ColorIndex
Next
'Do your stuff
For i = 1 To ThisWorkbook.Sheets("FLF").Cells(ThisWorkbook.Sheets("FLF").Rows.Count, "A").End(xlUp).Row
ThisWorkbook.Sheets("FLF").Range("A" & i).Interior.ColorIndex = ColorIndexArray(i)
Next
Edit: I tried loading all indices into the array at once and failed, hence the loop. Also, if you have tens or hundreds of thousands of rows, and you already have an array now, you could do your calculations on it instead to speed things up? But if its only a couple lines it shouldnt matter.

VBA,Combine .Formula with VBA

I have the following vba code which puts a formula in a cell.
.Range("D" & lastrow + 1).Formula = "=INDEX(Spread!$C:$C, MATCH(1,INDEX((A330 = Spread!$A:$A) * (""Stack"" =Spread!$B:$B),),0))"
How can I change A330 in my formula to vba code like Worksheets("Manager").Range("C2").Value
Just concate it like a normal string using the & operator. Since you are not joining text ,but rather a VBA object, do not wrap quotes around the object.
"=INDEX(Spread!$C:$C, MATCH(1,INDEX((" & Worksheets("Manager").Range("C2").Value & "= Spread!$A:$A) * (""Stack"" =Spread!$B:$B),),0))"
You can also store the data in a variable and use it instead of A330. This way you can always use newly created variable inside your code anytime anywhere in your code
myData = Worksheets("Manager").Range("C2").Value
.Range("D" & lastrow + 1).Formula = "=INDEX(Spread!$C:$C, MATCH(1,INDEX((myData = Spread!$A:$A) * (""Stack"" =Spread!$B:$B),),0))"

Excel VBA macro reading one column with differing text

I was tasked with creating a code that will check to see if internal hyperlinks in an excel spreadsheet worked. This code first changes the formulas that were on the spreadsheet and makes them actual hyperlinks (they were originally formulas linking the locations together). The problem that I have now is that I want to create hyperlinks ONLY if Column S has text. If it doesn't, I don't want the "E-COPY" text to be displayed. All of the text in Column S varies (not one line has the same characters), which is why I'm drawing a blank is to how I tell the program to only continue if it has any text, not anything specific. I am working with Excel 2016.
Also, I am doing this to 71935 and counting rows; is there a limit to how many it can go through? If so, what can I do about it?
Thank you!
Sub CreateHyperlinks()
Dim FN As Variant
Dim Path As Variant
Dim count As Variant
Sheets(1).Activate
count = WorksheetFunction.CountA(Sheets(1).Range("A:A"))
For i = 2 To count
If Range("AM" & i).Value = "Yes" And Columns("S") = Then
Range("E" & i).Value = ""
Path = Sheets(1).Range("R" & i).Value
FN = Sheets(1).Range("S" & i).Value
Sheets(1).Range("E" & i).Select
Selection.ClearFormats
Selection.Hyperlinks.Add Anchor:=Selection, Address:=Path & FN, TextToDisplay:="E-COPY"
Range("AM" & i).Value = " "
End If
Next i
End Sub
If you just need to check for any content in ColS then:
If Range("AM" & i).Value = "Yes" And Len(Range("S" & i).Value) > 0 Then
Few things:
'make a reference to the sheet you're working with
Dim ws As Worksheet
Dim wb As Workbook
Set wb = Excel.Application.ThisWorkbook
Set ws = wb.Worksheets(1)
'gets the absolute last row with data in it // ignores empty cells
count = ws.UsedRange.Rows.Count
personally, i hate working with named ranges, so i would suggest setting range references like so
what you wrote
Path = Sheets(1).Range("R" & i).Value
what i believe it should look like
Path = ws.Cells(i, 18).Value
if you want to test the type when working with variants, try this:
'tests the type associated with the variant. an 8 = string
If VarType(ws.Cells(i, 19).Value) = 8 Then
'do your thing
'tests if the value is null
ElseIf VarType(ws.Cells(i, 19).Value) = 0 Then
'do your other thing
here's a list of the vartype enumeration to help you out.
hope it helps!

Unable to get Averageifs property of WorksheetFunction class

I created a macro which summarizes a series of Excel forms into outputs, organized by sheets, rows and columns. The program creates a sheet, populates a table, and repeats.
The only thing I have left to do is to populate the cells with a formula. It's a very simple AverageIfs formula and I am getting the "Unable to get Averageifs property of WorksheetFunction class" error. I tried a bunch of things that I read, but to no avail.
So far I
Copied and pasted the formula itself into a separate macro (it ran, so it isn't a typo)
Removed the formula and checked to make sure every variable in the formula is the value I expect it to be at this point in the program (each one is)
Here is the relevant excerpt of my code. I am omitting a large portion, but I can assure all variables are defined and the loop works properly if I replace the AverageIfs function with a hard value (e.g. "1").
'Start Loop
VBid = 0
ZEval = 0
Do While VBid <> NumBidder
ActiveBid = ListInputSheet.Range("H" & 7 + VBid).Value
Set AnalysisSheet = AnalysisBook.Sheets.Add
AnalysisSheet.Name = ActiveBid
XRow = 4
AnalysisSheet.Range("C" & XRow).Value = ActiveBid
ZEval = 0
'Loop all evaluators down rows
Do While ZEval <> NumEval
ActiveEval = ListInputSheet.Range("E" & 7 + ZEval).Value
AnalysisSheet.Range("D" & XRow).Value = ActiveEval
AnalysisSheet.Range("D" & XRow).Select
WSub = 0
'Loop all Category Averages across row in columns
Do While WSub <> NumCategories
ActiveCell.Offset(0, 1).Select
Form = Application.WorksheetFunction.AverageIfs(SummarySheet.Range("F1:F10000"), SummarySheet.Range("A1:A10000"), ActiveEval, SummarySheet.Range("B1:B10000"), ActiveBid, SummarySheet.Range("D1:D10000"), WSub + 1)
ActiveCell.Value = Form
WSub = WSub + 1
Loop
XRow = XRow + 1
ZEval = ZEval + 1
Loop
'End evaluator loop
VBid = VBid + 1
Loop
'End Bidder loop
I have encountered the same error and couldn'd find a reason for a while, but later figured out that it is a issue with the data. It seems for the average function we need more than one row of data to work, so when I added additional rows of data it started working fine without any issue.
Here's an alternative if you cannot get it going. Just swap this line of code for you AverageIfs line:
Form = SummarySheet.Evaluate("AverageIfs(F1:F10000,A1:A10000," & ActiveEval & ",B1:B10000," & ActiveBid & ",D1:D10000," & WSub + 1 & ")")

Resources