VBA code and merged cells

jdluke6545

New Member
Joined
Jul 30, 2021
Messages
47
Office Version
  1. 2019
Platform
  1. Windows
I am needing a VBA code to unlock cells but when looking for an answer I came across a thread that said VBA and merged cells don't work.
Well I have merged cells and there is no way I can redo the sheet and not have merged cells.
is there any way to get VBA code to play nice with merged cells?
I can post images of my sheet that I need help with VBA code If anyone has a possible solution to this problem first.
Or I can try the XL2BB thing.... never used that before tho....
 
Thanks Guys!!
I will give these a try and report back as soon as I can. It may be tomorrow tho...
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
@Michael M, you just snuck in before me. Let me know if I haven't gotten this right.

@jdluke6545
See if this does the job.
The sheet will need to be unprotected first.

VBA Code:
Sub FindAndUnlockRelated()

    Dim searchRng As Range
    Dim ws As Worksheet
    Dim findStr As String
    Dim foundCell As Range
    Dim firstMatch As String
    Dim findArr() As String
    Dim i As Long
   
    findArr = Split("NT,ST", ",")

    Set ws = ActiveSheet
   
    Set searchRng = ws.Range("A1:A" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
   
    For i = LBound(findArr) To UBound(findArr)
        findStr = Trim(findArr(i))
        Set foundCell = Cells.Find(what:=findStr, LookIn:=xlFormulas, MatchCase:=False, lookat:=xlWhole)
        firstMatch = foundCell.Address
       
        Do While Not foundCell Is Nothing
            Set foundCell = Cells.FindNext(foundCell)
            foundCell.Offset(0, 6).MergeArea.Locked = False
            If firstMatch = foundCell.Address Then Exit Do   
        Loop

    Next i
End Sub

XL2BB of the sample data

20210731 VBA Merged Cells Locked Cells and Identification.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAH
1ID #Description
255This cell to remain locked
3This cell to remain locked
4NTThis cell to remain UNLOCKED
522This cell to remain locked
6STThis cell to remain UNLOCKED
7This cell to remain locked
855This cell to remain locked
9This cell to remain locked
10NTThis cell to remain UNLOCKED
1122This cell to remain locked
12STThis cell to remain UNLOCKED
13
Data
Just finished golf....and cracking a Shiraz....mainly to drown my sorrows !!
 
Upvote 0
So,, neither of those worked.
Could we sorta forget about the fact that the cells are merged (because excel uses the furthest left cell of a merged group for referencing data) and try a simple IF statement, something like this:
IF A CELL IN THE RANGE OF (A1:A35) = ST or NT THEN
SELECT THE CELL THAT IS OFFSET (0,6) FROM THE THAT TARGET CELL and use something like .LOCKED=FALSE
Does this have any close chance of possibly working? If so, how would I write this?
I did record a macro on the same sheet with these merged cells where I selected a select bunch of cells and cleared the data. I then Linked that macro to a button and it works.
So, that kinda tells me that there must be a way for this to work.
I can upload that code if it would help...??
But like I said, that code just deletes the data so I can start with a clean sheet instead of spending 30 minutes manually selecting the all the pertinent cells and deleting.
 
Upvote 0
This might get us started
VBA Code:
Sub MM1()
 Dim r As Long
 For r = 2 To Cells(Rows.Count, "B").End(xlUp).Row
    If Cells(r, 1).Value = "ST" Or Cells(r, 1).Value = "NT" Then
        Cells(r, 2).MergeArea.Locked = False
    End If
Next r
End Sub
Ok, Is the column with ST or NT column "A" ?
Is ST or NT the only text in the cell ?....no spaces, special characters, etc.
If the merge area to be unlocked starts at any other colu other than "B", my code won't work......
Change the 2 in this line to whatever column is the first one in the merged aresa
Rich (BB code):
 Cells(r, 2).MergeArea.Locked = False
 
Upvote 0
VBA Unlock Test.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBE
3ID #DESCRIPTION QTY COST
4
555THIS CELL TO REMAIN LOCKED
6THIS CELL TO REMAIN LOCKED
7NTTHIS CELL SHOULD REVERT TO UNLOCKED
822THIS CELL TO REMAIN LOCKED
9STTHIS CELL SHOULD REVERT TO UNLOCKED
10THIS CELL TO REMAIN LOCKED
11THIS CELL TO REMAIN LOCKED
12THIS CELL TO REMAIN LOCKED
13THIS CELL TO REMAIN LOCKED
14THIS CELL TO REMAIN LOCKED
15THIS CELL TO REMAIN LOCKED
16THIS CELL TO REMAIN LOCKED
17THIS CELL TO REMAIN LOCKED
18THIS CELL TO REMAIN LOCKED
19THIS CELL TO REMAIN LOCKED
20THIS CELL TO REMAIN LOCKED
21THIS CELL TO REMAIN LOCKED
22THIS CELL TO REMAIN LOCKED
23THIS CELL TO REMAIN LOCKED
24THIS CELL TO REMAIN LOCKED
25THIS CELL TO REMAIN LOCKED
26THIS CELL TO REMAIN LOCKED
27THIS CELL TO REMAIN LOCKED
28THIS CELL TO REMAIN LOCKED
29THIS CELL TO REMAIN LOCKED
30THIS CELL TO REMAIN LOCKED
31THIS CELL TO REMAIN LOCKED
32THIS CELL TO REMAIN LOCKED
33THIS CELL TO REMAIN LOCKED
34
TEST SHEET
 
Upvote 0
Short answer is NO....VBA and merged cells are kryptonite in Excel.
Merged cells should have been removed from Excel at birth.
Another option to merged cells is.....Highlight the cells >>RClick>>Format cells>>Alignment tab>>Horizontal Dropdownbox>>Center across selection
I don't typically use merged cells, but this is good to know.
 
Upvote 0
@Michael M
Yes, the MERGED cells that will have only the letters NT or ST in them and does start in column A and is merged thru column F. But it does start in column A.
The letters do not and will not contain any spaces or other characters.
BUT the merged cells needing to be unlocked starts in cell G and is merged thru AL.
So I need to count the number of columns that COLUMN AL is and put that number in place of the 2?
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,826
Members
449,190
Latest member
rscraig11

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