create drop down in column and lock other cells

Halley yenn

New Member
Joined
Mar 17, 2021
Messages
32
Office Version
  1. 365
Platform
  1. Windows
Hey Folks,

I have a excel with some columns data where my requirement is to create dropdown in column c where the list should contain 1 to 5 in the drop down and the data from column D till column end where the data is present must be locked.

Can someone help me to acheive this via macro code.

Regards,
Halley
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi Halley yenn,

You can try this below, but it will need some tweak for your needs.
Note: I have assumed :
1) you have a list like Col I & J, 2) you want only the cell C1 unlocked and 3) it is on change, when an user selects a number from the drop down

1646269607432.png


Module
VBA Code:
Dim LastRow, IndexValue As Integer
Dim rng, cell As Range

Sub ReturnList()
    Sheets("sheet1").Unprotect 'Take out the protection to overwrite cells
    Set rng = Range("i1:i21") 'I supposed you have a list somewhere
    Range("D1:D100").ClearContents 'I guess you want  to clear-out the data everytime
    LastRow = Cells(Rows.Count, "D").End(xlUp).Row 'Last row, to display list

    IndexValue = Range("C1").Value 'Store the chosen value in a variable
    
    'This part, I suppose you have a list you want to return, like a vloopup
    For Each cell In rng
        If cell.Value = IndexValue Then
            Cells(LastRow, "D").Value = cell.Offset(0, 1).Value
            LastRow = Cells(Rows.Count, "D").End(xlUp).Row + 1
        End If
    Next cell
    
   Sheets("Sheet1").Range("C1").Locked = False 'Range NOT protected
   Sheets("Sheet1").Protect 'possibility to add Password:= "xxxx"
End Sub

Worksheet
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("C1")) Is Nothing Then
        Call ReturnList
    End If
End Sub

I didn't include the pwd for protection, you will be able to unprotect it any time with

1646270109652.png



ThanP
 
Upvote 0
Hi Halley yenn,

You can try this below, but it will need some tweak for your needs.
Note: I have assumed :
1) you have a list like Col I & J, 2) you want only the cell C1 unlocked and 3) it is on change, when an user selects a number from the drop down

View attachment 59194

Module
VBA Code:
Dim LastRow, IndexValue As Integer
Dim rng, cell As Range

Sub ReturnList()
    Sheets("sheet1").Unprotect 'Take out the protection to overwrite cells
    Set rng = Range("i1:i21") 'I supposed you have a list somewhere
    Range("D1:D100").ClearContents 'I guess you want  to clear-out the data everytime
    LastRow = Cells(Rows.Count, "D").End(xlUp).Row 'Last row, to display list

    IndexValue = Range("C1").Value 'Store the chosen value in a variable
   
    'This part, I suppose you have a list you want to return, like a vloopup
    For Each cell In rng
        If cell.Value = IndexValue Then
            Cells(LastRow, "D").Value = cell.Offset(0, 1).Value
            LastRow = Cells(Rows.Count, "D").End(xlUp).Row + 1
        End If
    Next cell
   
   Sheets("Sheet1").Range("C1").Locked = False 'Range NOT protected
   Sheets("Sheet1").Protect 'possibility to add Password:= "xxxx"
End Sub

Worksheet
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("C1")) Is Nothing Then
        Call ReturnList
    End If
End Sub

I didn't include the pwd for protection, you will be able to unprotect it any time with

View attachment 59195


ThanP
Hey Than,
I have the validation list in other worksheet in column F from f2,how to call it here ??

Thanks,
H
 
Upvote 0
Change this line :
Set rng = Range("i1:i21") 'I supposed you have a list somewhere
to
VBA Code:
      Set rng = Sheets("Sheet2").Range("F2:F21") 'Replace Sheet2 for your WS name and F21 to your last row
 
Upvote 0
Hi ThanP,

This is working well for dropdown, for locking the columns let me make it clear .

I have 10k excels in a folder where I have to open each excel and Lock Columns : Col C , Col D, Col E, Col F, Col G, Col H, Col I , Col J , Col K, Col L, Col M, Col N and apply password say "GRUG" and make Col A and Col B editable or Unlocked.

The no of rows in each column will be around 20k rows and I recieve 10k files on weekly basis.

Any macro to open each excel and format accordingly will be appreciated.

Thanks
H
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,944
Members
449,095
Latest member
nmaske

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