Getting TYPE MISMATCH error in VBA

Fusky

New Member
Joined
Nov 17, 2016
Messages
15
hiii, i have a code something like this :


Public Sub Match()




ThisWorkbook.Sheets("Sheet1").Activate


Range("Data!H8") = Application.Sum(Application.Index(Range("A:GH"), 0, Application.Match("ORDERS" & "Country", Range("B2:B100") & Range("A2:GH2"), 0)))


End Sub


When i run this code i'm getting a error "Type Mismatch", can anybody help me with this, Thankss
 
Country
ORDERSINWIEGAFEA
ORDERS100500671575
ORDERS123490287744

ORDERS129879956763
ORDERS-PR789675623513
ORDERS-PR4599011234178
ORDERS-PR76652265410
ORDERS-PR200473254876
ORDERS-PR1003245437656

<tbody>
</tbody>
The numbers i getting whenn i used the formula were not really the sum of the columns thats what i meant by weird. Sheet1 is the file with all the datas are in,"Data" is the sheet where i would like to get the data from Sheet1, The data is large thats why i would like to use vba,
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You are using VBA to just put a formula into a cell... It has the same effect as actually putting the formula into the cell.
 
Upvote 0
This is your data...

Country
ORDERSINWIEGAFEA
ORDERS100500671575
ORDERS123490287744
ORDERS129879956763
ORDERS-PR789675623513
ORDERS-PR4599011234178
ORDERS-PR76652265410
ORDERS-PR200473254876
ORDERS-PR1003245437656

<tbody>
</tbody>


What do you expect to see in H3? Be specific.
 
Upvote 0
I need to SUM numbers in a column if the row heading = ORDERS and the column header = IN. the formula to "scan" the column headings, find the one that says "IN", and then spit back the sum of those numbers in column heading named ("IN") with a row heading of "ORDERS".


and similarly want to get the sum for the other column headings like WI,EG etc

 
Upvote 0
Perhaps something like this?

Code:
Sub SumCountry()


Dim sCountry As String
Dim colnum As Long

sCountry = "IN" [COLOR=#ff0000]'you can set this via a cell reference, or a user input.. or even inside a loop if you change Sheets("Data").Range("H8") to something with the loops variable...[/COLOR]


With Sheets("Sheet1")
   colnum = Application.Match(sCountry, .Range([COLOR=#ff0000]"A1:G1"[/COLOR]), 0)[COLOR=#ff0000] 'your range of the header row with all the country names... always starting with A...[/COLOR]
   Sheets("Data").Range("H8").Value = Application.Sum(.Range(.Cells(2, colnum), .Cells(100, colnum)))
End With


End Sub
 
Last edited:
Upvote 0
sorry i forgot the IF part of your sum...

Code:
Sub SumCountry()


Dim sCountry As String
Dim colnum As Long

sCountry = "IN" [COLOR=#ff0000]'you can set this via a cell reference, or a user input.. or even inside a loop if you change Sheets("Data").Range("H8") to something with the loops variable...[/COLOR]


With Sheets("Sheet1")
   colnum = Application.Match(sCountry, .Range([COLOR=#ff0000]"A1:G1"[/COLOR]), 0)[COLOR=#ff0000] 'your range of the header row with all the country names... always starting with A...[/COLOR]
   Sheets("Data").Range("H8").Value = Application.[COLOR=#0000ff]SumIf(.Range("B2:B100"), "ORDERS", .[/COLOR]Range(.Cells(2, colnum), .Cells(100, colnum)))
End With


End Sub
 
Upvote 0
Thanks for ur efforts tygrrboi :D , I have tried both codes the first one working correctly except it didnt consider the ORDERS. but when i try the second part it is return Value of 0,
 
Upvote 0
I am not sure why you are getting 0...

It it working fine on my end.

Could you maybe upload a sample file onto a filesharing website like Dropbox or Google Drive so I can see what the issue is with your actual data?
 
Upvote 0
You are getting 0 because there is no row that has just "ORDERS"

ORDERS_PR
ORDERS_PR
ORDERS_PR
ORDERS_PR
ORDERS_PR
ORDERS_PR
ORDERS_PR
ORDERS_PR
ORDERS_PR
ORDERSRfcst
ORDERSRfcst
ORDERSRfcst
ORDERSRfcst
ORDERSRfcst
ORDERSRfcst
ORDERSRfcst
ORDERSRfcst
ORDERSRfcst

<tbody>
</tbody>


You will either need to change it to ORDERS_PR or ORDERSRfcst....

As for your columns... I do not know what they mean... there seem to be two headers. Perhaps you can work it out once you see how SUMIF works when you put in the correct condition, not just "ORDERS"...




Additionally: Sharing your file link in the post itself instead of in a private message will allow for others to possibly help you. I try my best but I'm by no stretch of the imagination as good as some of the other folks on here.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,365
Messages
6,124,512
Members
449,167
Latest member
jrob72684

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top