SUMIF formula workbook name is unknown - vba

I have to read to use SUMIF to check and compare the supplier number from different workbooks and if it is same then copy the Prices(using SUMIF). Everytime time the workbook can be different from which I take the prices but the sheet names and their layout will be same.So how can I write the formula in SUMIF? Can anyone help me please?
I'm stuck with this code since 2 days but couldn't figure out whats wrong.
Windows(wb_name).Activate
Range("AW18", Range("AW18").Offset(0, -44).End(xlDown).Offset(0, 44)).Formula = _
"=SUMIF('[" & dest_name & "]" & "!" & "Cu Part PVO L",$M$10:$M$2000,C19, _
"[" & dest_name & "]" & "Cu Part PVO L" & "'" & "!",$AD$10:$AD$2000)"

It looks like you have the exclamation mark in the wrong place, as well as also having too many commas.
Range("AW18", Range("AW18").Offset(0, -44).End(xlDown).Offset(0, 44)).Formula = _
"=SUMIF('[" & dest_name & "]Cu Part PVO L'!$M$10:$M$2000,C19," & _
"'[" & dest_name & "]Cu Part PVO L'!$AD$10:$AD$2000)"

Related

How to reference cell from another sheet in Module - Excel VBA?

I have a macro in an Excel workbook that updates date from Sheet1 into SQL by clicking a button in Sheet3. Sheet3 also have cells used as parameters during the update process.
Currently, the macro is placed in a module and my SQL statement is as follows:
sSQLUpd = "update [table1].[dbo].[Plan] set [Plan_QTY] = " & Plan & "_
where [MacID] = " & Mac & " and [ModelID] = " & Mdl & " and [Date] = " & dt & "_
and DATEPART(year,[Date])= " & Sheets("Sheet3").Cells(3, 3).Value & "_
and DATEPART(month,[Date])= " & Sheets("Sheet3").Cells(3, 6).Value & ""
conn.Execute sSQLUpd
But when I test the code I keep getting "Subscript out of range" in my error handler.
The SQL structure is fine, since I tested it by replacing the parts:
DATEPART(year,[Date])= " & Sheets("Sheet3").Cells(3, 3).Value & "
and
DATEPART(month,[Date])= " & Sheets("Sheet3").Cells(3, 6).Value & "
with actual numbers and the data can pass.
So it's safe to assume that the codes referencing the cells in Sheet3 have issue. Perhaps it doesn't want to play nice when placed in a module?
I even tried different variations as well:
DATEPART(year,[Date])= " & Sheets("Sheet3").range("C3").Value & "
No dice....
Anything I can do to modify it?
Wait... Never mind. I figured out what went wrong. All I needed to do was change
Sheets("Sheet3") into Sheet3 only:
DATEPART(year,[Date])= " & Sheet3.Cells(3, 3).Value & "
Its always the simple stuff that screws with me. :p

Can VBA attempt to assign to a cell a formula that is too long?

I am on Windows, if that matters. This code seems to attempt to assign a formula that is too long:
ActiveSheet.ListObjects("SegmentValues_1").ListColumns("Change").DataBodyRange.Formula = "=IFERROR(IF(INDEX(SegmentValues_" & (ActiveWorkbook.Sheets.count - 2) & "[CODE_ZONE],MATCH([#Modules],SegmentValues_" & (ActiveWorkbook.Sheets.count - 2) & "[Modules],0))=0,""Last total was ZERO"",([#CODE_ZONE]-INDEX(SegmentValues_" & (ActiveWorkbook.Sheets.count - 2) & "[CODE_ZONE],MATCH([#Modules],SegmentValues_" & (ActiveWorkbook.Sheets.count - 2) & "[Modules],0)))/INDEX(SegmentValues_" & (ActiveWorkbook.Sheets.count - 2) & "[CODE_ZONE],MATCH([#Modules],SegmentValues_" & (ActiveWorkbook.Sheets.count - 2) & "[Modules],0)))),""Did not exist previously"")"
Is such a thing possible? If so, how to solve it?
The long formulas work in Excel VBA quite well. Take a look at this 24 nested IF():
Sub TestMe()
Range("A1").Formula = "=IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1," & _
"IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1," & _
"IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1," & _
"IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1," & _
"IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1,IF(1=1," & _
"IF(1=1,2))))))))))))))))))))))))))))))))))))))))))))))"
End Sub
It is translated really well into Excel. Thus, take a good look at your formula, the problem should be there.

Excel VBA - insert formula in a set of rows with variable reference directly or replacing a string for example "\=" with "="

I have the goal to write a formula in a set of rows. Some references in the formula have to change each row.
I implemented the following script:
Dim i As Integer
Dim formcolM As String
Dim temprng As String
For i = 0 To 100
formcolM = "NUMBERVALUE(IF(Q" & i & "=""Bedarf kum."";A" & i & ";IF(Q" & i & "=""Ist"";OFFSET(A" & i & ";-1;0);IF(Q" & i & "=""Lz."";OFFSET(A" & i & ";-2;0);IF(Q" & i & "=""Ist+Lz.-Bedarf"";OFFSET(A" & i & ";-3;0);)))))"
Let temprng = "M" & i
Range(temprng).Select
ActiveCell.Value = "\=" & formcolM
next i
With this script I am writing a string each row in my excel table at column M.
I noticed that if the formula hasn't the symbol "\" , you can find an error .
In order to avoid the error I thought to leave the symbol "\" and to use a trick deleting it after (because I don't know how to solve with R1C1 formula. I read some answers on Stackoverflow, but unfortunately I did not understand )
The replacing script after the for cycle:
Columns("M:M").Replace What:="\=", Replacement:="=", LookAt:=xlPart
The strange thing is that the macro doesn't delete it.
Infact when the script finishes , it seems that nothing happened, without errors. But if I want substitute "\=" with another symbol, for example "*", the replacing script works.
I did not understand if the problem is :
the replace method did not recognized the symbol "=" to search
I cannot use the replace method because the symbol "=" disturbs in some way , I don't know in what.
OR, is there another simplest way to get this task done?
Someone could help me in order to fix? I should have the formula working in the column M , automatically with vba (not with another formula in the excel sheet) .
Thanks in advance for your time.
We can apply the formula directly. The issue is that vba is very US-EN Centric and all formula when using the .Formula needs to be in that format.
Also since your formula refers to values in a row 3 above the one in which it is put we need to start the loop at 4 not 0. There is no row 0
There are two ways, in US-En format with English functions and , as the deliminator using .Formula:
Dim i As Integer
For i = 4 To 100
Range("M" & i).Formula = "=NUMBERVALUE(IF(Q" & i & "=""Bedarf kum."",A" & i & ",IF(Q" & i & "=""Ist"",OFFSET(A" & i & ",-1,0),IF(Q" & i & "=""Lz."",OFFSET(A" & i & ",-2,0),IF(Q" & i & "=""Ist+Lz.-Bedarf"",OFFSET(A" & i & ",-3,0),)))))"
Next i
Or using .FormulaLocal and the formula as you would write it in your native tongue.
Dim i As Integer
For i = 4 To 100
Range("M" & i).FormulaLocal = "=NUMERO.VALORE(SE(Q" & i & "=""Bedarf kum."";A" & i & ";SE(Q" & i & "=""Ist"";SCARTO(A" & i & ";-1;0);SE(Q" & i & "=""Lz."";SCARTO(A" & i & ";-2;0);SE(Q" & i & "=""Ist+Lz.-Bedarf"";SCARTO(A" & i & ";-3;0);)))))"
Next i
By the time I got this worked out, Scott already had an answer. I just wanted to post your original code modified to work. I would suggest his method.
Sub TestScript()
Dim i As Integer
Dim formcolM As String
Dim temprng As String
For i = 4 To 100
formcolM = "NUMBERVALUE(IF(Q" & i & "=" & "Bedarf kum." & ";A" & i & ";IF(Q" & i & "=" & "Ist" & ";OFFSET(A" & i & ";-1;0);IF(Q" & i & "=" & "Lz." & ";OFFSET(A" & i & ";-2;0);IF(Q" & i & "=" & "Ist+Lz.-Bedarf" & ";OFFSET(A" & i & ";-3;0);)))))"
temprng = "M" & i
Sheets("Sheet1").Range(temprng).Select
ActiveCell.Value = " = " & formcolM
Next i
End Sub

VBA Set Cell value to text containing ' not working

I'm trying to get a macro to type out a heap of formulae for me. The formula goes something like this:
=COUNTIF('other_sheet'!A:A,"hello*")
The user can specify A and other_sheet, so I select the cell and then go
ActiveCell.Value = "=COUNTIF('" & othercell & "'!" & column & ":" & _
column & """,hello*"")"
But it keeps giving me errors like:
1004: Object Defined Error
I have tried using ActiveCell.Text, ActiveCell.Formula etc. and they all don't work.
It needs to be ActiveCell.Formula and ", is the wrong way around next to Hello. It should be:
ActiveCell.Formula= "=COUNTIF('" & othercell & "'!" & column _
& ":" & column & ",""hello*"")"
Apparently you can only put proper, completed answers into a cell.
i.e. if the output turns out to be "=sum(A:A" and a bracket isn't closed, then the application throws an error just as if you typed it in manually.

Insert formula from VBA

I'm attempting to have the following code insert the formula into the changed cell. I'm not receiving any errors but the code is not populating on the worksheet. Any insight into what I'm not doing?
formula_p1 = _
"=GETPIVOTDATA(""Max of " & [value_column_header] & ",Database!R1C14, & _
""Key"",& Key &)"
Debug.Print Cell.Address
Sheets("Cost Sheet").Cell.Formula = formula_p1
Thank you in advance.
Update
My current code is below I'm stuck on the concatenation in the final part. I'm not sure how to accomplish this in VBA. Also I need this to be a formula that will re evaluate on changes to the "Family" part that can be done on the worksheet. The change in "Family" is accomplished from a combo box selection.
formula_1 = _
"=GETPIVOTDATA(""Max of " & value_column_header & """,Database!$N$1,""Key""," & Concatenate(CurrentHFMFamily, G5, Left(B12, 4)) & ")"
This is the final result of what i was trying accomplish. It's not pretty but it's quick and stable.
=IF(INDIRECT(CONCATENATE("B",ROW()))=""," ",IFERROR(GETPIVOTDATA("Max of & _
"&INDIRECT((ADDRESS(11,COLUMN())))&"_ADJ",Database!R1C14,"Key",& _
LEFT(INDIRECT(CONCATENATE("B",SUM(ROW()-1))),4)&CurrentHFMFamily& & _
(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),-(SUM(ROW()-(5))),- & _
(MOD(COLUMN()+1,4))))),0))
Thanks to everyone for your ideas they helped in pointing me in the right direction.