Set Excel protect properties From HTA - vba

How do I use the following in a vbscript on HTA.
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True
If I try the same with ":=" , it throws page error.
Thanks,
Anand

To 'port' VBA code like
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True
to VBScript to be used in .HTAs (host: mshta.exe) or .VBSs (host c/wscript.exe) you have to
Create an Excel.Application COM object
Use that and it's Workbook/Worksheet collections to drill down to the object you want to call the method on
Convert the named arguments of VBA to positional arguments of VBScript (based on the docs for that method)
Define the xlXXXX constants
Start your research here.

I found an alternative way. I Inserted the required code into VB code module of the object Excel.
Something like below.
With myReport.VBProject.VBComponents("ThisWorkbook").CodeModule
.InsertLines .CountOfLines + 1, _
"Private Sub Workbook_Open()" & Chr(13) & _
" ProtectMe(1)" & vbNewLine & _
"End Sub" & vbNewLine & _
"Sub ProtectMe(Status)" & vbNewLine & _
" Dim mySheet As Worksheet" & vbNewLine & _
" Dim myPassword " & vbNewLine & _
" myPassword = ""IamGenius""" & vbNewLine & _
" For Each mySheet In ThisWorkbook.Worksheets" & vbNewLine & _
" mySheet.Protect Password:=myPassword, DrawingObjects:=True, _" & vbNewLine & _
" Contents:=True, Scenarios:=True, AllowFormattingCells:=True, _" & vbNewLine & _
" AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowFiltering:=True" & vbNewLine & _
" mySheet.EnableSelection = xlUnlockedCells" & vbNewLine & _
" Next mySheet" & vbNewLine & _
"End Sub"
End With
Thanks,
Anand:)

Related

How to Copy a Button to a New Sheet with a Macro

I have a Form in VBA that has a button that will create a new sheet within the workbook.
On that new sheet, I need 4 buttons to be on there with their code already in place.
When I hit the 'create new sheet' button, I have the following code for updating the new buttons on the new sheet:
'Update quantity button
ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
, DisplayAsIcon:=False, Left:=150, Top:=20, Width:=123.75, Height _
:=23.25).Select
ActiveSheet.OLEObjects(1).Object.Caption = "Update Quantity"
'update quantity code
With ActiveWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
LineNum = .CountOfLines + 1
.InsertLines LineNum, _
"Private Sub CommandButton1_Click()" & vbLf & _
"Dim ComponentAmt As Double" & vbLf & _
"ComponentNum = Application.InputBox(""Please provide a component number"", ""Component Number"", Type:=1)" & vbLf & _
"ComponentAmt = Application.InputBox(""Quantity received of the component"", ""Quantity Received"", Type:=1)" & vbLf & _
"Set found = Range(""A:A"").Find(what:=ComponentNum, LookIn:=xlValues, LookAt:=xlWhole)" & vbLf & _
"If found Is Nothing Then" & vbLf & _
"MsgBox ""Your component number was not found" & vbLf & _
"Else" & vbLf & _
"found.Offset(0, 2).Value = found.Offset(0,2).Value + ComponentAmt" & vbLf & _
"End If" & vbLf & _
"End Sub"
End With
'Archive button
ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
, DisplayAsIcon:=False, Left:=400, Top:=200, Width:=123.75, Height _
:=23.25).Select
ActiveSheet.OLEObjects(2).Object.Caption = "1. Export PO"
'Archive Code
With ActiveWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
LineNum2 = .CountOfLines + 1
.InsertLines LineNum2, _
"Private Sub CommandButton2_Click()" & vbLf & _
"ActiveSheet.Copy" & vbLf & _
"With ActiveSheet.UsedRange" & vbLf & _
".Copy" & vbLf & _
".PasteSpecial xlValue" & vbLf & _
".PasteSpecial xlFormats" & vbLf & _
"End With" & vbLf & _
"Application.CutCopyMode = False" & vbLf & _
"ActiveWorkbook.SaveAs ""Full Path/""" & vbLf & _
"End Sub"
End With
'Hide button
ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
, DisplayAsIcon:=False, Left:=400, Top:=250, Width:=123.75, Height _
:=23.25).Select
ActiveSheet.OLEObjects(3).Object.Caption = "2. Done"
'hide button Code
With ActiveWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
LineNum4 = .CountOfLines + 1
.InsertLines LineNum4, _
"Private Sub CommandButton3_Click()" & vbLf & _
"ActiveSheet.Select" & vbLf & _
"ActiveWindow.SelectedSheets.Visible = False" & vbLf & _
"End Sub"
End With
'View price button
ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
, DisplayAsIcon:=False, Left:=200, Top:=20, Width:=123.75, Height _
:=23.25).Select
ActiveSheet.OLEObjects(4).Object.Caption = "View Price"
'View price code
With ActiveWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
LineNum4 = .CountOfLines + 1
.InsertLines LineNum4, _
"Private Sub CommandButton4_Click()" & vbLf & _
"Range(""I10"").Select" & vbLf & _
"ActiveCell.FormulaR1C1 = ""=VLOOKUP(R[-8]C[-7],KitList!C[-8]:C[11],17,FALSE)""" & vbLf & _
"Range(""J10"").Select" & vbLf & _
"ActiveCell.FormulaR1C1 = ""=VLOOKUP(R[-8]C[-8],KitList!C[-9]:C[10],18,FALSE)""" & vbLf & _
"Range(""I11"").Select" & vbLf & _
"ActiveCell.FormulaR1C1 = ""=VLOOKUP(R[-9]C[-7],KitList!C[-8]:C[11],19,FALSE)""" & vbLf & _
"Range(""J11"").Select" & vbLf & _
"ActiveCell.FormulaR1C1 = ""=VLOOKUP(R[-9]C[-8],KitList!C[-9]:C[10],20,FALSE)""" & vbLf & _
"End Sub"
End With
The buttons then show on the new worksheet, but when I click on them, nothing happens.
Also, when clicking on the sheet in VBA I have the following code that is supposed to be for the buttons.
Private Sub CommandButton1_Click()
'update quantity
Dim ComponentAmt As Double
ComponentNum = Application.InputBox("Please provide a component number", "Component Number", Type:=1)
ComponentAmt = Application.InputBox("Quantity received of the component", "Quantity Received", Type:=1)
Set found = Range("A:A").Find(what:=ComponentNum, LookIn:=xlValues, LookAt:=xlWhole)
If found Is Nothing Then
MsgBox "Your component number was not found"
Else
found.Offset(0, 2).Value = found.Offset(0, 2).Value + ComponentAmt
End If
End Sub
Private Sub CommandButton2_Click()
'export PO
ActiveSheet.Copy
With ActiveSheet.UsedRange
.Copy
.PasteSpecial xlValue
.PasteSpecial xlFormats
End With
Application.CutCopyMode = False
ActiveWorkbook.SaveAs "Full Path/"
End Sub
Private Sub CommandButton3_Click()
'hides the PO in the document
ActiveSheet.Select
ActiveWindow.SelectedSheets.Visible = False
End Sub
Private Sub CommandButton4_Click()
'view price
Range("I10").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(R[-8]C[-7],KitList!C[-8]:C[11],17,FALSE)"
Range("J10").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(R[-8]C[-8],KitList!C[-9]:C[10],18,FALSE)"
Range("I11").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(R[-9]C[-7],KitList!C[-8]:C[11],19,FALSE)"
Range("J11").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(R[-9]C[-8],KitList!C[-9]:C[10],20,FALSE)"
End Sub
Should I make the code for each button as a sub then on the buttons call the sub?
If I create more then one new sheet, are the button names going to change?

Put formula with if statements in the entire columns

I have recorded a macro with the following formula, but it gives me an error in the second line.
Expected end of statement.
I guess the issue is that its way to long. Please suggest how to make this work?
Sub Macro1()
Range("CG2").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC69=""High"",(IF(AND(RC4<>"""",RC5<>"""",RC6<>"""",RC7<>"""",RC8<>"""",RC10<>"""",RC11<>"""",RC12<>"""",RC13<>"""",RC14<>"""",RC16<>"""",RC17<>"""",RC18<>"""",RC19<>"""",RC20<>"""",RC21<>"""",RC22<>"""",RC23<>"""",RC24<>"""",RC25<>"""",RC26<>"""",RC27<>"""",RC28<>"""",RC29<>"""",RC30<>"""",RC31<>"""",RC32<>"""",RC33<>"""",RC34<>"""",RC35<>"""",RC36<>"""",RC37<>"""",RC38<>"""",RC39<>"""",RC40<>"""",RC41<>"""",RC42<>"""",RC43<>"""",RC44<>"""",RC45<>"""",RC46<>""""),""No"",""Yes""))," & Chr(10) & "(IF(RC69=""Medium"",(IF(AND(RC4<>"""",RC5<>"""",RC6<>"""",RC7<>"""",RC8<>"""",RC10<>"""",RC11<>"""",RC12<>"""",RC13<>"""",RC14<>"""",RC16<>"""",RC17<>"""",RC18<>"""",RC19<>"""",RC20<>"""",RC21<>"""",RC22<>"""",RC23<>"""",RC31<>"""",RC32<>"""",RC33<>"""",RC34<>"""",RC35<>"""",RC36<>"""",RC37<>"""",RC38<>"""",RC39<>"""",RC40<>"""",RC41<>"""",RC42<>"""",RC44<>"""",RC45<>"""",RC46<>""""),""No"",""Yes""))," & Chr(10) & "(IF(AND(RC4<>"""",RC5<>"""",RC6<>"""",RC7<>"""",RC8<>"""",RC10<>"""",RC11<>"""",RC12<>""""
""",RC14<>"""",RC16<>"""",RC17<>"""",RC18<>"""",RC19<>"""",RC20<>"""",RC31<>"""",RC32<>"""",RC33<>"""",RC34<>"""",RC35<>"""",RC36<>"""",RC37<>"""",RC38<>"""",RC39<>"""",RC41<>"""",RC42<>"""",RC45<>"""",RC46<>""""),""No"",""Yes"")))))"
End Sub
The issue here is the multiples ", and you also have a different kind of these : “ which do not work!
So replace the " by Chr(34) like this :
Sub formula()
Range("CO2:CO").formula = "=IF(OR(LEN($BN2)=0;$BN2=" & Chr(34) & "Not Performed" & Chr(34) & _
";LEN($BK2)=0;$BK2=" & Chr(34) & "Not Performed" & Chr(34) & _
";LEN($BL2)=0;$Q2=" & Chr(34) & "Not Performed" & Chr(34) & _
";LEN($BM2)=0);" & Chr(34) & "Yes" & Chr(34) & ";" & Chr(34) & _
"No" & Chr(34) & ")"
End Sub

Insert VBA into formula formatting?

Really quick question on how to format VBA in excel formulas. When you are inserting a formula into excel and you want to insert a variable from vba for example if b is a string you would use " & b & " is that the correct formatting? To illustrate the problem I have the code below and tried to use that formatting and well... I don't know why it wont work, I get a (Compile error: Expected: End of statement). Can anyone tell me where I am going wrong?
Dim HrsSTD As String
Dim HrsSAT As String
Dim HrsSUN As String
Dim HrsSTWN As String
Dim sdFormula
HrsSTD = ActiveCell.Address
Selection.Offset(0, 1).Select
HrsSAT = ActiveCell.Address
Selection.Offset(0, 1).Select
HrsSUN = ActiveCell.Address
Selection.Offset(0, 1).Select
HrsSTWN = ActiveCell.Address
sdFormula = "=IF((" & Range(NamedRange).Cells(2, 1).Address & _
"=""Please add a title"",0,VLOOKUP((" & Range(NamedRange).Cells(2, 1).Address & _
",'Tables (H)'!$H$2:$J$6,2,FALSE)* _
" & HrsSTD & "+VLOOKUP(" & Range(NamedRange).Cells(2, 1).Address & _
",'Tables (H)'!$H$2:$J$6,2,FALSE)* _
" & HrsSAT & "*1.25+VLOOKUP((" & Range(NamedRange).Cells(2, 1).Address & _
",'Tables (H)'!$H$2:$J$6,2,FALSE)*" & HrsSUN & "* _
1.5+VLOOKUP((" & Range(NamedRange).Cells(2, 1).Address & _
",'Tables (H)'!$H$2:$J$6,2,FALSE)*" & HrsSTWN & "*0.75)"
The code I would type into excel would be: But I want to change the A13's and the I16 (i.e. all the relative references) into variables in VBA
=IF(A13="Please add a title",0,VLOOKUP(A13,'Tables (H)'!$H$2:$J$6,2,FALSE)*F16+VLOOKUP(A13,'Tables (H)'!$H$2:$J$6,2,FALSE)*G16*1.25+VLOOKUP(A13,'Tables (H)'!$H$2:$J$6,2,FALSE)*H16*1.5+VLOOKUP(A13,'Tables (H)'!$H$2:$J$6,2,FALSE)*I16*0.75)
Is this what you are trying? Also I see that you haven't taken my advice from the previous answer.
One more tip. Break you code in simple parts. It is easier to understand.
The problem with your code is in the line
",'Tables (H)'!$H$2:$J$6,2,FALSE)* _
" & HrsSAT & "*1.25+VLOOKUP((" & Range(NamedRange).Cells(2, 1).Address & _
You can't write it like that. The first line doesn't have the ending ". You cannot carry it forward to the next line like that.
is this what you are trying?
Dim sFormula As String
Dim sAddr As String
sAddr = Range(NamedRange).Cells(2, 1).Address
sFormula = "=IF(" & sAddr & _
"=""Please add a title"",0,VLOOKUP(" & _
sAddr & ",'Tables (H)'!$H$2:$J$6,2,FALSE)*F16+VLOOKUP(" & _
sAddr & ",'Tables (H)'!$H$2:$J$6,2,FALSE)*G16*1.25+VLOOKUP(" & _
",'Tables (H)'!$H$2:$J$6,2,FALSE)*H16*1.5+VLOOKUP(" & _
sAddr & ",'Tables (H)'!$H$2:$J$6,2,FALSE)*I16*0.75)"

Excel VBA Type Mismatch for #N/A

I have this chunk of code
With Data.Cells(rowMatch, GWECol)
.Value = Cmp.Cells(i, GWENetPr)
.AddComment
.Comment.Text Text:=UCase(Environ("UserName")) & ":" & vbNewLine _
& "Comment: " & Cmp.Cells(i, CommCol) & vbNewLine _
& "Transaction: " & Cmp.Cells(i, QRTran) & vbNewLine _
& "QR Pr: " & Cmp.Cells(i, QRPr) & vbNewLine _
& "QR WD: " & Cmp.Cells(i, QRWD) & vbNewLine _
& "QR WD All: " & Cmp.Cells(i, QRWDA) & vbNewLine _
& "QR XPr: " & Cmp.Cells(i, QRXPr) & vbNewLine _
& "QR XAll: " & Cmp.Cells(i, QRXAll) & vbNewLine _
& "GWE Pr: " & Cmp.Cells(i, GWEPr) & vbNewLine _
& "GWE All: " & Cmp.Cells(i, GWEAll) & vbNewLine _
& "GWE XPr: " & Cmp.Cells(i, GWEXPr) & vbNewLine _
& "GWE XAll: " & Cmp.Cells(i, GWEXAll)
.Comment.Shape.TextFrame.AutoSize = True
End With
Where the Cmp.Cells(i, X) refers to cells that may have #N/A error (a failed VLOOKUP).
Is it possible to have the code just take in the #N/A as a string or just leave it empty? Right now, whenever one of the cells referenced is #N/A, the chunk will fail and no comment text will be added at all.
Thanks!
You're using the default property of the cell,
Debug.Print Cmp.Cells(i, QRXAll)
For example this always refers to the cells .Value property. The .Value is actually an error type, Error 2042 which I think you could avoid by checking
CLng(Cmp.Cells(i,QRXA11))
But this will result in 2042 instead of the #N/A text.
If you want to get the string #N/A: try using Cmp.Cells(i, QRXAll).Text which relies on the cell's .Text property instead of its .Value.
Debug.Print Cmp.Cells(i, QRXAll).Text
Disclaimer: I have done some VBA programming, but I wouldn't call myself an expert.
This may be overly simplistic, but you could just assign each value to a variable and then assign the variables to the comment. If any one value is N/A, at least the rest of your values will still be assigned to the comment. I perfer this kind of solution as it ensures that a single error will not derail the entire operation.
Dim vComment As String
Dim vTransaction As String
Dim vQRPr As String
Dim vQRWD As String
' Etc.
vComment = Cmp.Cells(i, CommCol).Text
vTransaction = Cmp.Cells(i, QRTran).Text
vQRPr = Cmp.Cells(i, QRPr).Text
vQRWD = Cmp.Cells(i, QRWD).Text
' Etc.
.Comment.Text Text:=UCase(Environ("UserName")) & ":" & vbNewLine _
& "Comment: " & vComment & vbNewLine _
& "Transaction: " & vTransaction & vbNewLine _
& "QR Pr: " & vQRPr & vbNewLine _
& "QR WD: " & vQRWD & vbNewLine
' Etc.
Edited: Thanks to David for pointing out that the .Text property should be used
use IsError to check to see if the cells has #N/A
if IsError(Cmp.Cells(i, GWENetPr)) then
'give it a valid value
else
'use the value int he cell
end if
'start with statement
example
With Data.Cells(rowMatch, GWECol)
If IsError(Cmp.Cells(i, GWENetPr)) Then
.Value = "" 'or #N/A
Else
.Value = Cmp.Cells(i, GWENetPr)
End If
.AddComment
.Comment.Text Text:=UCase(Environ("UserName")) & ":" & vbNewLine _
& "Comment: " & Cmp.Cells(i, CommCol) & vbNewLine _
& "Transaction: " & Cmp.Cells(i, QRTran) & vbNewLine _
& "QR Pr: " & Cmp.Cells(i, QRPr) & vbNewLine _
& "QR WD: " & Cmp.Cells(i, QRWD) & vbNewLine _
& "QR WD All: " & Cmp.Cells(i, QRWDA) & vbNewLine _
& "QR XPr: " & Cmp.Cells(i, QRXPr) & vbNewLine _
& "QR XAll: " & Cmp.Cells(i, QRXAll) & vbNewLine _
& "GWE Pr: " & Cmp.Cells(i, GWEPr) & vbNewLine _
& "GWE All: " & Cmp.Cells(i, GWEAll) & vbNewLine _
& "GWE XPr: " & Cmp.Cells(i, GWEXPr) & vbNewLine _
& "GWE XAll: " & Cmp.Cells(i, GWEXAll)
.Comment.Shape.TextFrame.AutoSize = True
End With
You can use IIf to use a specific value if there is an error:
& "Comment: " & IIf(IsError(Cmp.Cells(i, CommCol)),"",Cmp.Cells(i, CommCol)) & vbNewLine _

Error adding code to workbook via VBA

I am trying to use VBA in Excel to add conditional formatting to a column of a pivot table. The issue is that whenever the pivot table is refreshed, or a filter is changed, etc. the conditional formatting is lost. My solution was to add a macro to the pivot table update event in the workbook, which works ... kinda. It seems that when I run the code that creates the pivot table and adds the code to handle conditional formatting an error occurs but ONLY when the VBA window is NOT open. If the VBA window is open the code executes normally - despite no code changes or reference changes.
Private Sub setupConditionalFormattingForStatusColumn()
Dim thisSheetModule As vbcomponent
Dim formattingCodeString As String
On Error GoTo conditionalFormattingError
formattingCodeString = _
"Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)" & vbNewLine & _
" With Target.parent.Columns(" & harReportColumn("Status") & ")" & vbNewLine & _
" .FormatConditions.AddIconSetCondition" & vbNewLine & _
" .FormatConditions(.FormatConditions.Count).SetFirstPriority" & vbNewLine & _
vbNewLine & _
" With .FormatConditions(1)" & vbNewLine & _
" .IconSet = ActiveWorkbook.IconSets(xl4TrafficLights)" & vbNewLine & _
" .IconCriteria(1).Icon = xlIconYellowExclamation" & vbNewLine & _
vbNewLine & _
" With .IconCriteria(2) " & vbNewLine & _
" .Type = xlConditionValueNumber" & vbNewLine & _
" .value = -1" & vbNewLine & _
" .Operator = 5" & vbNewLine & _
" .Icon = xlIconGreenCircle" & vbNewLine & _
" End With" & vbNewLine & _
vbNewLine & _
" With .IconCriteria(3)" & vbNewLine & _
" .Type = xlConditionValueNumber" & vbNewLine & _
" .value = 1.05" & vbNewLine & _
" .Operator = 7" & vbNewLine & _
" .Icon = xlIconYellowCircle" & vbNewLine & _
" End With" & vbNewLine
formattingCodeString = formattingCodeString & vbNewLine & _
" With .IconCriteria(4)" & vbNewLine & _
" .Type = xlConditionValueNumber" & vbNewLine & _
" .value = 1.15" & vbNewLine & _
" .Operator = 7" & vbNewLine & _
" .Icon = xlIconRedCircleWithBorder" & vbNewLine & _
" End With" & vbNewLine & _
vbNewLine & _
" .ShowIconOnly = True" & vbNewLine & _
" End With" & vbNewLine & _
vbNewLine & _
" .HorizontalAlignment = xlCenter" & vbNewLine & _
" .VerticalAlignment = xlCenter" & vbNewLine & _
" End With" & vbNewLine & _
"End Sub"
Set thisSheetModule = ThisWorkbook.VBProject.VBComponents(harReportSheet.CodeName)
thisSheetModule.CodeModule.AddFromString formattingCodeString
Exit Sub
conditionalFormattingError:
errorLog.logError "WARNING: An error occured while applying the conditional formatting code for the ""Status"" column."
Err.Clear
Resume Next
End Sub
The line which generates the error is: thisSheetModule.CodeModule.AddFromString formattingCodeString but the error is only generated if the VBA window is closed.
Any ideas?
So I was able to find an answer to this issue. Evidently Excel does not properly initialize the codename property of newly created worksheets when the VBA window is not open (the why here is beyond me) but only when it recompiles. A work-around is to force Excel to recompile prior to any calls to the codename property. The solution which worked for me was to place the following code:
On Error Resume Next
Application.VBE.CommandBars.ActiveMenuBar.FindControl(ID:=578).Execute
On Error GoTo conditionalFormattingError
above the line beginning with Set thisSheetModule = ... . Oddly enough the line of code which forces the recompile also throws an error for me which I was able to safely ignore with the surrounding error handling.
More information can be found here: http://www.office-archive.com/2-excel/d334bf65aeafc392.htm
Hope that helps someone out there. :-)