samerickson89
New Member
- Joined
- Jun 13, 2019
- Messages
- 38
I'm trying to replicate what this code does...
...by creating two arrays, one for variables and one for the cells their values will be placed in, then having a For loop iterate through the arrays rather than writing the same code for each variable. This is the code I tried to accomplish that with:
but I keep running into errors no matter what tweaks I try. I want to apologize in advance for not having kept track of the changes I've made so far, but I'll make sure to do that from now on!
Any help is very much appreciated.
Code:
Sub Workbook_Open()
'unprotect sheet, start with all cells locked
ActiveSheet.Unprotect
ActiveSheet.UsedRange.Locked = True
'define variables
Dim InspNo As String
Dim DateRecd As String
Dim QtyRecd As String
Dim PurchNo As String
Dim SampSize As String
Dim Vendor As String
Dim VendNo As String
Dim PartNo As String
Dim Rev As String
Dim Descr As String
'prompts when workbook is opened
InspNo = InputBox("What is the inspection report number?", "Inspection Report Number")
DateRecd = InputBox("What date was part received?", "Date Received")
QtyRecd = InputBox("How many of this part were received?", "Quantity Received")
PurchNo = InputBox("What is the purchase order number?", "Purchase Order Number")
SampSize = InputBox("How many parts are being inspected?", "Sample Size")
Vendor = InputBox("What is the vendor name?", "Vendor Name")
VendNo = InputBox("What is the vendor code number?", "Vendor Code Number")
PartNo = InputBox("What is the part number?", "Part Number")
Rev = InputBox("What is the current revision level?", "Revision Level")
Descr = InputBox("What is the part description?", "Description")
'place values from prompts onto worksheet
Range("O2").Value = InspNo
Range("C3").Value = DateRecd
Range("O3").Value = QtyRecd
Range("C4").Value = PurchNo
Range("O4").Value = SampSize
Range("C5").Value = Vendor
Range("O5").Value = VendNo
Range("C6").Value = PartNo
Range("H6").Value = Rev
Range("K6").Value = Descr
'unlock blank fields in heading
'If IsEmpty(ActiveSheet.Range("O2")) Then ActiveSheet.Range("O2").Locked = False
'If IsEmpty(ActiveSheet.Range("C3")) Then ActiveSheet.Range("C3").Locked = False
'If IsEmpty(ActiveSheet.Range("O3")) Then ActiveSheet.Range("O3").Locked = False
'If IsEmpty(ActiveSheet.Range("C4")) Then ActiveSheet.Range("C4").Locked = False
'If IsEmpty(ActiveSheet.Range("O4")) Then ActiveSheet.Range("O4").Locked = False
'If IsEmpty(ActiveSheet.Range("C5")) Then ActiveSheet.Range("C5").Locked = False
'If IsEmpty(ActiveSheet.Range("O5")) Then ActiveSheet.Range("O5").Locked = False
'If IsEmpty(ActiveSheet.Range("C6")) Then ActiveSheet.Range("O2").Locked = False
'If IsEmpty(ActiveSheet.Range("H6")) Then ActiveSheet.Range("H6").Locked = False
'If IsEmpty(ActiveSheet.Range("K6")) Then ActiveSheet.Range("K6").Locked = False
'unlock measurement columns
Range("D9:M26").Locked = False
'protect sheet to prevent edits anywhere but unlocked cells
ActiveSheet.Protect
End Sub
...by creating two arrays, one for variables and one for the cells their values will be placed in, then having a For loop iterate through the arrays rather than writing the same code for each variable. This is the code I tried to accomplish that with:
Code:
'Dim Vars As Variant
Vars = Array("InspNo", "DateRecd", "QtyRecd", "PurchNo", "SampSize", "Vendor", "VendNo", "PartNo", "Rev", "Descr")
Dim Cells As Variant
Cells = Array("O2", "C3", "O3", "C4", "O4", "C5", "O5", "C6", "H6", "K6")
For i = LBound(Vars) To UBound(Vars)
Dim Vars(i) As String
Next i
but I keep running into errors no matter what tweaks I try. I want to apologize in advance for not having kept track of the changes I've made so far, but I'll make sure to do that from now on!
Any help is very much appreciated.