Excel - How to lock whole row if cell in Column G contains * - excel

I have a rather large excel sheet (4500+ rows) which is an output from a powershell script. when the script couldn't find the answer and we have manually found information have column N (lock Cell) show TRUE .
What I would like to do is lock the entire row if the cell in column N contains text TRUE.
I would assume this is done via VBA but I not versed in VBA so really don't have a clue on it. I am good at following instructions if someone can write some steps.
The end goal is that we would like to be able to run our powershell script in the future and not overwrite manually found information hence the need to lock rows containing a *
Is this possible?
As always thank you for your help
_____________UPDATE _____________
I have adjusted my sheet to have a new column and then created the following VBA script
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("N:N") = "TRUE" Then
Range("2:10000").Locked = True
ElseIf Range("N:N") = "FALSE" Then
Range("2:10000").Locked = False
End If
End Sub

Right click on the tab you wish to protect>>>> View code.
Then I inserted this
Private Sub Worksheet_Calculate()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Me.Unprotect
For i = 1 To LR
With Range("N" & i)
If .Value = "TRUE" Then .EntireRow.Locked = True
End With
Next i
Me.Protect
End Sub
It appears to be working, now to figure out the next part.

Related

Excel: Copying a row to a new sheet based on a selection from a drop down menu

I am asking what might be a basic question for Excel / VBA, but I am trying to copy rows from one worksheet (StrategicPlanning) to another worksheet (Experiment) based on what is selected in E7 of Worksheet Experiment.
How the Experiment worksheet looks -- please ignore the other cells
My code is not working, and I have a feeling I'm either missing something obvious or I am on the wrong path entirely.
a = Worksheets("StrategicPlanning").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("StrategicPlanning").Cells(i, 5).Value = Worksheets("Experiment").Cells(7, 5) Then
Worksheets("StrategicPlanning").Rows(i).Copy
Worksheets("Experiment").Activate
b = Worksheets("Experiment").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("StrategicPlanning").Activate
End If
Next
End Sub
I appreciate any and all help! Many thanks!
This one uses Worksheet_Change Event, but can be done from a button etc.
Remarks:
I have set Experiment E7 as a "named range" Exp_Title.
Given "codenames" to both sheets: Experiment and StrPlan
CurrentRegion is the same as if you select the cell/range and pressing CTRL + *
You will have to modify to your needs, but will give you an idea. Ask away anything confusing. And definitely read what Chris Neilsen has linked.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Intersect(Target, Experiment.Range("Exp_Title")) Is Nothing) Then
Dim rngStrPlan As Range
Set rngStrPlan = StrPlan.Range("A1").CurrentRegion
Set rngStrPlan = rngStrPlan.Offset(1, 0).Resize(rngStrPlan.Rows.Count - 1, 1)
Dim i As Long
For i = 2 To 2 + rngStrPlan.Rows.Count
If StrPlan.Cells(i, 1).Value = Experiment.Range("Exp_Title").Value Then
StrPlan.Rows(i).EntireRow.Copy Experiment.Range("A10")
Exit For
End If
Next i
Set rngStrPlan = Nothing
End If
End Sub

How can I do a Calculation in Microsoft Excel VBA?

I'm 15 and I'm doing a Internship as a Developer and I've got a kinda hard exercise.
I have a Table with 3 columns, A is "Number" B is "percent" and C is "Value". The column "value" is blank and I Need to calculate the value with a macro button. I've tried this, but it was wrong because I didn´t calculate it in VBA:
Public Sub PushButton ()
Range("C2:C11").Formula = "=A2*B2/100"
Range("C2:C11").Value = Range("C1:C6).Value
End Sub
How do I solve this?
You are using a defined range, you could do it with a dynamic range like this:
Option Explicit
Sub PushButton()
Dim i As Long, LastRow As Long
With ThisWorkbook.ActiveSheet
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'first you need to find the last row on the active sheet
For i = 2 To LastRow 'then iterate through all the rows starting from 2, if row 1 has headers
.Cells(i, 3) = .Cells(i, 1) * .Cells(i, 2) / 100
Next i
End With
End Sub
If you need help understanding this code, let me know.
Edit: Explanation
Well, the first thing you must do is Dimension all your variables, and to help that you can use the Option Explicitright above all your code.
I've dimensioned 1 variable for the loop and another one to find the last row with text.
To find the last row what you are actually doing is going to excel, select the last row (1048576) and the column where it will have text, in this case 1 or column "A" and then pushing ctrl+Up excel and vba will get you to the last cell with text.
To do that you use Cells(Row, column) instead of manually inserting row 1048576 you can just use rows.count and it will be the same.
Once you get the last row you just iterate with a For iloop meaning For a variable called i which equals 2 (For i = 2) To LastRow (to the last row you calculated) VBA will repeat the code in between the ForAnd Next adding 1 number to i everytime the loop restarts.
In this case is just adding a number to the rows on Cells(i, 3) so you can modify that cell depending of its i value.
I think you need to question why Excel needs to calculate on demand rather than automatically like normal. Failing that there are a few options
You could change your calculation method to Manual using the following in the ThisWorkbook object
Option Explicit
Dim xlCalcMethod As XlCalculation
Private Sub Workbook_Open()
With Application
' Store users current method for when closing the workbook
xlCalcMethod = .Calculation
.Calculation = xlCalculationManual
End With
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
' Reset calculation
Application.Calculation = xlCalcMethod
End Sub
and then when the button is pressed use the following code to calculate placed in a Module
Option Explicit
Public Sub Button_Click()
Application.Calculate
End Sub
Another option to do this without looping would be:
Sub CalculateRange()
Dim rng As Range
' Update for your Range
With ActiveSheet
Set rng = .Range("C2:C" & .Cells(.Rows.Count, "A").End(xlUp).Row)
End With
rng.Value2 = Evaluate(rng.Offset(0, -2).Address & "*" & rng.Offset(0, -1).Address & "/100")
End Sub
Finally, the way you've come up with is perfectly acceptable as VBA

VBA code to move entire rows from one sheet to 3 different sheets based on cell values from dropdown list

I am trying to use VBA code in order to move entire rows from sheet1 named "New Projects" to 3 different sheets, based on cell value picked from in cell dropdown list in sheet1.
I am not a coder but I could understand a little and fond a piece of code somewhere on the internet.
So far I found the code that can move a row from my sheet1 ("New Projects") to other sheet named "Prio1", if the cell value picked from dropdown list becomes "Prio 1" (meaning that I am moving that new project (entire row) to sheet "Prio1" because it has priority number 1.
But I have even the sheets named Prio2 and Prio3 where I need to move the rows when the value in the cell is "Prio 2" or "Prio 3", and I dont know how to do it.
If I just copy/paste same code in the editor and only change sheet names then I get some error message "Ambiguous name Worksheet_Change" and it doesn't work.
This is the piece of code that I found and it works for moving rows to Prio1:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim answer As Integer
Dim lngRow As Long, ws As Worksheet, nextrow As Long
If Target.Cells.Count > 1 Then Exit Sub
Application.ScreenUpdating = False
If Not Intersect(Target, Columns("M:M")) Is Nothing Then
If Target.Value = "Prio 1" Then
lngRow = Target.Row
On Error Resume Next
With ThisWorkbook
Set ws = Worksheets("Prio1")
If ws Is Nothing Then .Worksheets.Add().Name = "Prio1"
nextrow = Worksheets("Prio1").Cells(Rows.Count, "A").End(xlUp).Row + 1
End With
With Sheet1 'code name
answer = MsgBox("Ska Almin flytta ärendet till fliken Prio1?", vbYesNo + vbQuestion)
If answer = vbYes Then
.Range("A" & lngRow).EntireRow.Copy Destination:=Worksheets("Prio1").Range("A" & nextrow)
.Range("A" & lngRow).EntireRow.Delete shift:=xlUp
Else
Worksheets("Nya ärende").Range("M:M").ClearContents 'or do nothing
End If
End With
End If
End If
Application.CutCopyMode = False
Application.ScreenUpdating = True
Set ws = Nothing
End Sub
Now I need help to somehow add the equivalent code for "Prio2" anf "Prio3".
Can someone please help me out?
Almin
The error Ambiguous name Worksheet_Change means exactly what it sounds like (or what Google Translate would tell you it means -- tvetydig or mångtydig).
There is another procedure in your project that already has the name Worksheet_Change.
To find it from the VBA Editor, press CTRL+F and enter Worksheet_Change, click Current Project and use Find Next as required to figure out why the name is being used twice.
You'll have to either get rid of one of the procedures with duplicate names, or rename one, or combine the two together.
A good tutorial: Excel VBA For Complete Beginners

For Loop Return to Top of Column

I am an excel vba novice and I am trying to write some code to automate a specific task. I managed to find the code below and customized it to suite my needs. The code, activated by the command button, will look through column I for text FALSE starting with row 5. When it encounters a FALSE, it will stop and copy the cell in column C on the same row.
The code works for that purpose, but I would like to take it a step further. If the code reaches the last filled cell in the I column without encountering a FALSE, I would like it to go back to the top of the column and search starting with I1.
Is there a way to do something like this with a For Loop? I have been looking at Do Until and Do While loops but haven't seen an example that resembles what I am trying to do.
Any assistance would be greatly appreciated.
Picture of the Worksheet
Private Sub CommandButton1_Click()
Dim LR As Long, i As Long
With Sheets("Sheet1")
LR = .Range("I" & Rows.Count).End(xlUp).Row
For i = 5 To LR
With .Range("I" & i)
If .Text = ("FALSE") Then
.Offset(0, -6).Copy
Exit For
End If
End With
Next i
End With
End Sub
Declare some boolean variable: Dim isFound As Boolean, set it to false: isFound = False, if match is found, then set it to true: isFound = True (right after copying value). After For loop check if match was found If isFound Then... and check the remaining 5 cells: from I1 to I5 (because in preceding loop we checked the rest) or you can just add another loop which will go throguh I column.

Excel VBA: fill textbox based on combobox selection

First off all I am not very experienced with excel VBA. After searching on net and youtube I couldn't figure it out, so I hope you can help me out.
In my workbook I run a macro that gives a timestamp via =NOW() and an "unique" generated code via CHAR(RANDBETWEEN). This is then copy & pasted to a separate sheet("Dates")to create the list for my combobox, where the code is in column A and the timestamp in column B. Each time I run my macro a new timestamp and code is generated and put in Row 1 Column A & B again (so the older one is shifted down)
With the combobox I got my list of timestamp based on the code below:
Private Sub UserForm_Initialize()
Dim ws As Worksheet, lirow As Integer
Set ws = ThisWorkbook.Worksheets("Dates")
lirow = 1
While ws.Cells(lirow, 2) <> ""
Me.cboxDates.AddItem ws.Cells(lirow, 2).Value
lirow = lirow + 1
Wend
End Sub
What I want is to show the corresponding code in a textbox next to my combobox, so basically just an offset (0, -1).
Your help is appreciated.
Thanks in advance!
Kr, Marty
Private Sub cboxDates_Change()
Dim Found As Range, LastRow As Long
Set Found = Worksheets("Dates").Columns(2).Find(what:=Me.cboxDates.Text, LookIn:=xlValues, lookat:=xlWhole)
If Found Is Nothing Then Exit Sub
Me.TextBox1.Text = Worksheets("Dates").Cells(Found.Row, Found.Column).Offset(0, -1).Value
End Sub

Resources