I'm very new to Excel VBA and can't seem to figure out why I keep getting error 80010108 "Method 'Value' of object 'Range' failed." If anyone can help me out it would be very much appreciated!!
I created a UserForm ("MainView") with a command button ("Add New"), which brings up another UserForm ("AddNewWorkOrder") that contains another command button ("Enter"). I want this Enter command button to enter all data typed in this userform's textboxes into the corresponding columns in the active worksheet.
**I need to add rows to a table in the worksheet
Now when I run MainView and then click "Add New" to bring up AddNewWorkOrder, I'm then unable to enter the data because clicking "Enter" will throw up the error 80010108 and crash Excel (prompted to restart Excel).
HOWEVER if I run the AddNewWorkOrder UserForm directly, without going through MainView, it will enter the data exactly as needed.
This is the code I have for MainView's "Add New":
Private Sub AddNew_Click()
Me.Hide
AddNewWorkOrder.Show
End Sub
This is the code I have for "Enter" (including just one textbox for simplicity):
Private Sub EnterCommandButton_Click()
Range("A" & Rows.Count).End(xlUp).Select
HoldVal = ActiveCell.Value + 1
MsgBox HoldVal
ActiveCell.Offset(1, 0).Value = HoldVal
ActiveCell.Offset(1, 1).Select
ActiveCell.Value = Me.ItemSizeTextBox
Me.Hide
MainView.Show
End Sub
Edit: the line "ActiveCell.Offset(1, 0).Value = HoldVal" is where I'm getting an error
Related
I am new to using Macros/VB in excel and in need of some help (in the simplest way you can instruct me)
I have a workbook with two sheets. I have already created a form on sheet 1 which will allow users to enter data which then is populated within a hidden sheet (sheet2).
I would like to add a button on sheet 1 for the user to view the hidden "list data" they have entered in sheet 2 but they cannot be able to edit the data - only view it.
Any help would be greatly appreciated.
Insert a shape (I prefer this over an ActiveX control) on the sheet with your form. I named the shape "Show data" and gave that same caption to the button. Now add the following code to a standard code module (one which VBE names like "Module1").
Option Explicit
Sub ShowData_Click()
' 25 Jul 2017
With ThisWorkbook.Worksheets("Jeanette")
.Visible = xlSheetVisible
.Activate
End With
End Sub
Change the name of the worksheet with the data to the name you have given that sheet in your project. Then right-click on the button (shape) and select "Assign macro". Assign the "ShowData_Click" macro. Now, when you click the button the hidden sheet will become visible and be activated.
In the code sheet for the data sheet (which is normally hidden), add the following to procedures.
Option Explicit
Dim ThisSheet As String
Private Sub Worksheet_Activate()
' 25 Jul 2017
ThisSheet = ActiveSheet.Name
End Sub
Private Sub Worksheet_Deactivate()
' 25 Jul 2017
On Error Resume Next
Worksheets(ThisSheet).Visible = xlSheetVeryHidden
End Sub
The first one will run whenever the sheet is activated which happens when the new button is pressed. It will remember the name of the sheet. This is so that you don't need to hard-code the name.
The second procedure will run whenever you activate another sheet in the workbook. It will hide the sheet again. So, you show the sheet by pressing the button and hide the sheet by selecting another sheet.
I'm not a friend of Excel's protection. So, I suggest another way to prevent users from modifying the data. Here is the code. Install it on the same code sheet where you already have the Activate and Deactivate procedures.
Private Sub Worksheet_Change(ByVal Target As Range)
' 25 Jul 2017
Static ShowMsg As Integer
With Application
.EnableEvents = False
.Undo
ShowMsg = ShowMsg + 1
If (ShowMsg Mod 3) = 1 Then
MsgBox "Data in this sheet may not be modified." & vbCr & _
"Your changes have been removed.", _
vbInformation, "Invalid action"
End If
.EnableEvents = True
End With
End Sub
This code will undo any change the user makes. The user will receive a message to this effect each third time he/she tries to modify something.
I'm having a 1004 error in the code below. Its such as simple routine, I can't figure out what's wrong. This is only one sub of many in Module 1. Note that UserForm2's code works fine when I run it from the forms Sub's. But when I call it from here, I get the 1004 error. I don't really understand what causes that error. Help appreciated. Note that ChosenString and Report is a Public variable. You have several 1004 error entries, but I can't find one that fits this problem. Error is on UserForm2.Show.
Sub ChooseReport()
' Display a selection box of reports and run the report
'
ChoseCancel = 0
Sheets("Codes").Activate
UserForm2.Show ' Displays selection box of reports
'
' If the Cancel button was selected, exit the sub
If ChoseCancel = 1 Then
Sheets("Reports").Activate
Exit Sub
End If
'
' Trim returned value of the comma
StringLength = Len(ChosenString)
Report = Left(ChosenString, StringLength - 2)
'
End Sub
This is my UserForm2 Code. It works fine.
Sub UserForm_Initialize()
' Fill the list box with appropriate values
'
UserForm1.ListBox1.ListStyle = fmListStylePlain
Sheets("Codes").Activate
Range("O4").Select
'
' Fill List box with appropriate cells entries for Reports
With ListBox1
Do While ActiveCell.Value <> Empty
.AddItem ActiveCell.Value
ActiveCell.Offset(1, 0).Select
Loop
End With
End Sub
Found it. I had copied the code from UserForm1, and hadn't changed a reference to UserForm1 to UserForm2. My error went away.
I am fairly new to VBA, and have been searching the forum for one particular code.
I have a workbook with multiple different graphs. I also have a UserForm with a popbox box where the User can select the city and click enter. What I would like to do is Hide all the Worksheets with the graphs, and when the user selects one particular city, I would like Excel to show that particular Graph. For Example, The user Selects Vancouver, and Excel will show the worksheet Pertaining to Vancouver.
I would also Like to Have a done button, where if the user clicks that, it will go back to the sheet where the UserForm is.
Private Sub ENT_Click()
'Error if you do not select the City
If Me.City.Value = "" Then
MsgBox "Please Select a City", vbExclamation, "Select a City"
Me.City.SetFocus
Exit Sub
End If
If Me.City.Value = "Mississauga" Then
Sheets("Sheet4").Visible = True
Sheets("Sheet4").Activate
End If
End Sub
Every time I run the code, with the Worksheets hidden, Or UnHidden, I keep getting a Subscript out of Range error.
Thank you again for the help
It was an error with the way I was labeling my Worksheet. The Code should have actually been:
Private Sub ENT_Click()
'Error if you do not select the City
If Me.City.Value = "" Then
MsgBox "Please Select a City", vbExclamation, "Select a City"
Me.City.SetFocus
Exit Sub
End If
'Change "Mississauga" to the item in your ComboBox
If Me.City.Value = "Mississauga" Then
Worksheets(4).Visible = True
Worksheets(4).Activate
End If
End Sub
I'm trying to write an Excel 2007 macro for a coworker, but my VBA skills are pretty basic (pardon the pun). Essentially, what needs to happen is, when a checkbox is clicked, the neighboring cell to the right is filled with the username of the person logged in.
So far, here's the code I've come up with that allows me to do that:
Sub CheckBox1_Click()
Range("J4").Activate
If ActiveCell.Offset(0, 18).Value = True Then
ActiveCell.Offset(0, 1).Value = Environ("UserName")
Else
ActiveCell.Offset(0, 1).Clear
End If
End Sub
Just for the sake of reference, that "ActiveCell.Offset(0,18)" refers to a cell that is linked to the checkbox in question and contains its true/false value.
(EDIT: Also, the reason cell J4 is activated is because in this case, it's the cell containing the ActiveX checkbox)
That works perfectly, but that's not my problem. My problem is this: there are 49 more checkboxes in that row, and three more rows on this sheet, and 45 more sheets in this book. I do NOT want to have to copy paste the same code into a unique macro just to change the active cell. More importantly, as a good programmer, I shouldn't be repeating code like that. How should I write this so that I don't have to refer to a distinct cell every time?
EDIT 2: Holy smokes, Lance just helped me realize I was mistaken. The sheet uses form controls, not ActiveX controls. Greatly sorry, everyone.
While this is easy to do with a Sheet object, it's pretty hard to do with an ActiveX Control object. You can't self-reference the name of an ActiveX Control in its event, unless it's passed to it, and you also can't reference the name of the event subroutine to extract the name, and you can't reference the name of the routine that called a routine.
I also attempted to trigger off of the Worksheet Change and SelectionChange events, but those don't trigger off of a checkbox change, even if it has a LinkedCell that changes
What I finally came up with was the somewhat generic wrapper for the click event, that you'll have to modify the string to match the Checkbox name:
Private Sub CheckBox1_Click()
NameCopy Me, "CheckBox1"
End Sub
and then a Namecopy function that sets the cell -7 to the left of the LinkedCell to the name value.
Public Sub NameCopy(wsheet As Worksheet, cname As String)
If wsheet.OLEObjects(cname).Object.Value = True Then
Range(wsheet.OLEObjects(cname).LinkedCell).Offset(0, -7).Value = Environ("UserName")
End If
End Sub
It's easier with a Forms checkbox, you can use this Macro for all your checkboxes. Just remember to set the Macro to this:
Public Sub NameCopy()
Dim shp As Shape
Set shp = ActiveSheet.Shapes(Application.Caller)
If shp.ControlFormat.Value = xlOn Then
ActiveSheet.Range(shp.ControlFormat.LinkedCell).Offset(0, -7).Value = Environ("UserName")
End If
End Sub
Since you are using form controls, this is really easy. You can use Application.Caller to have the code access the clicked checkbox, and then use it's TopLeftCell property to get where the checkbox is located, and then you can perform whatever operation you want. In your case, something like this I'm guessing:
Sub Checkbox_Click()
With ActiveSheet.CheckBoxes(Application.Caller)
If .Value = 1 Then 'Checkbox is checked
.TopLeftCell.Offset(, 1).Value = Environ("UserName")
Else
.TopLeftCell.Offset(, 1).ClearContents
End If
End With
End Sub
I am validating input in an Excel worksheet. When the user enters invalid data a MsgBox is shown, giving the option to Abort/Retry/Ignore.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If IsNumeric(Range("i17")) Then
If [I17] < 0 Then
result = MsgBox("Critical Error Encountered", vbAbortRetryIgnore + vbCritical, "Error Encountered")
If result = 3 Then
ActiveCell.Value = Empty
End If
End If
End If
End sub
Since I have written this code in SelectionChange when I click enter then only it show the message box and since I'm writing Active.Cell = Empty it is deleting the next cell, but I mean it to clear the cell which contains invalid data.
If you use the SelectionChange event, the Target range is the cell just selected and not the cell which has just been changed. If you use the Change event of the worksheet, then Target refers to the cell that has been changed. So, assuming that cell I17 is some kind of subtotal/validation cell and not the cell into which the user enters data, try this:
Private Sub Worksheet_Change(ByVal Target As Range)
If IsNumeric(Range("i17")) Then
If [I17] < 0 Then
result = MsgBox("Critical Error Encountered", vbAbortRetryIgnore + vbCritical, "Error Encountered")
If result = 3 Then
Target.Value = Empty
Target.Select
End If
End If
End If
End Sub
This code will clear the cell just changed and change the selection back to that cell if the user selects the Abort button in the message.