Although neocortex underlies higher-order brain functions, little is known about the synaptic interactions that drive neocortical microcircuit function in vivo. The neocortex is spontaneously active in vivo so I explored how such spontaneous network activity affects in vivo the interactions between excitatory and inhibitory neurons in the barrel cortex. To this aim, I have focused on the layer 2/3 microcircuit of the C2 barrel column driving sensory processing for the C2 whisker, and combined in vivo whole-cell patch-clamp recordings in anesthetised and awake head-restrained mice with optogenetic stimulation, that allows controlling the activity of genetically defined neurons with light. I expressed channelrhodopsin-2 (ChR2), a light-gated cation channel to evoke action potentials in excitatory neurons with brief light pulses. The impact of this optogenetic stimulus was measured on the membrane potential dynamics of their postsynaptic target neurons. The optogenetic stimulus drove reliable action potentials in ChR2-expressing excitatory neurons, independent of the state of ongoing spontaneous cortical activity. In contrast, I found a marked modulation of light-evoked postsynaptic responses in ChR2-non-expressing excitatory neurons by cortical activity. During spontaneously hyperpolarised periods (DOWN state), optogenetic stimulation resulted in depolarisations of postsynaptic excitatory neurons, while during depolarised periods (UP state), the response was significantly smaller and shorter. The optogenetic stimulus drove the membrane potential of postsynaptic excitatory layer 2/3 neurons towards reversal potentials, which were hyperpolarised relative to action potential threshold, leading to a reduction in firing rates. Targeting whole-cell recordings to GFP-labelled GABAergic layer 2/3 neurons with two-photon microscopy revealed that both fast-spiking and non-fast-spiking inhibitory postsynaptic neurons were rapidly driven to fire action potentials, preferentially from UP states. Such rapid activation of inhibitory neurons in the UP state is likely to be responsible for decreasing firing rate in excitatory neurons. As a result, optogenetic stimulation of a given set of excitatory neurons results in a state-dependent recruitment of inhibitory neurons that limit the spread of excitation within the layer 2/3 microcircuit. The data presented here indicates that a powerful inhibitory loop within the cortical microcircuit rapidly balances excitation with inhibition and enforces sparse coding of layer 2/3 excitatory neurons.