Provided is a heterocyclic compound that can have an antagonistic action on an NMDA receptor containing the NR2B subunit, and is expected to be useful as a prophylactic or therapeutic agent for major depression, bipolar disorder, migraine, pain, peripheral symptoms of dementia and the like. A compound represented by the formula (I):wherein each symbol is as described in the DESCRIPTION, or a salt thereof.