vba if both active cells are in column 4

hajiali

Well-known Member
Joined
Sep 8, 2018
Messages
624
Office Version
  1. 2016
Platform
  1. Windows
Hello all needing to determine if both active cells are in column 4, only 2 cells will be selected and selected cell are not continuous so user can hold "CTRL" key to select them.

i currently have
VBA Code:
If ActiveCell.Column = 4 Then
but if one of them is selected is in column 4 it will run the code.

any help is greatly appreciated.

Thanks,
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
ActiveCell is not the same as Selection, you can only have one ActiveCell at any time.

This will identify if any of the selected cells are in column D regardless of whether or not that cell is active.
VBA Code:
If Not Intersect(Selection, Range("D:D")) Is Nothing Then
 
Upvote 0
using that still allows users to select cell from any column as long as one is in column D the macro runs. Here is what i have maybe this will help understand better

VBA Code:
End Sub
Sub Swap()
If Not Intersect(Selection, Range("D:D")) Is Nothing Then
    If Selection.Count <> 2 Then
         MsgBox "Select 2 cells (only) to swap."
         Exit Sub
    End If
    Set trange = Selection
    If trange.Areas.Count = 2 Then
         Temp1 = trange.Areas(2)
         Temp2 = trange.Areas(2).Offset(0, -1)
         Temp3 = trange.Areas(2).Offset(0, 1)
         Temp4 = trange.Areas(2).Offset(0, 2)
         Temp5 = trange.Areas(2).Offset(0, 3)
         trange.Areas(2) = trange.Areas(1)
         trange.Areas(2).Offset(0, -1) = trange.Areas(1).Offset(0, -1)
         trange.Areas(2).Offset(0, 1) = trange.Areas(1).Offset(0, 1)
         trange.Areas(2).Offset(0, 2) = trange.Areas(1).Offset(0, 2)
         trange.Areas(2).Offset(0, 3) = trange.Areas(1).Offset(0, 3)
         trange.Areas(1) = Temp1
         trange.Areas(1).Offset(0, -1) = Temp2
         trange.Areas(1).Offset(0, 1) = Temp3
         trange.Areas(1).Offset(0, 2) = Temp4
         trange.Areas(1).Offset(0, 3) = Temp5
    Else
         Temp = trange(1)
         trange(1) = trange(2)
         trange(2) = Temp
    End If
    Else
    MsgBox "TO SWAP VALUES ONLY SELECT VAUELS IN COLUMN D"
   End If
End Sub

I need the macro to not to run unless both selected cells are from D otherwise prompt MsgBox "TO SWAP VALUES ONLY SELECT VAUELS IN COLUMN D"
 
Upvote 0
To check if selection is only in col D, try:
VBA Code:
If Not Intersect(Selection, Range("D:D")) Is Nothing Then
    If Union(Selection, Range("D:D")).Address = Range("D:D").Address Then
        Debug.Print "YES"
    Else
        Debug.Print "NO"
    End If
End If
 
Upvote 0
Solution

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

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