Hi I am having a excel file. There is a macro in the excel file to clear the dependent drop down list . When we copy the value from one row to another row or one column to another column we are getting run time error 13. Could you please help us to resolve the issue
Code :
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row > 2 And Target.Row < 100 Then
If Target.Column = 2 And Target.Cells.Count = 1 And Target.Value = "Create Account_Personal" Then
Target.Offset(0, 3).Value = "NA"
Target.Offset(0, 4).Value = "NA"
Target.Offset(0, 5).Value = "NA"
Target.Offset(0, 6).Value = "NA"
Target.Offset(0, 7).Value = "NA"
Target.Offset(0, 8).Value = "NA"
Target.Offset(0, 9).Value = "NA"
Target.Offset(0, 10).Value = "NA"
Target.Offset(0, 11).Value = "NA"
Target.Offset(0, 19).Value = "NO"
Target.Offset(0, 22).Value = "NO THANKS"
ElseIf Target.Column = 2 And Target.Cells.Count = 1 And Target.Value <> "Create Account_Personal" Then
Target.Offset(0, 3).Value = "Select Country"
End If
If Target.Column = 5 And Target.Cells.Count = 1 And Target.Value <> "NA" Then
Target.Offset(0, 1).Value = "select State"
Target.Offset(0, 2).Value = ""
Target.Offset(0, 3).Value = ""
Target.Offset(0, 4).Value = ""
Target.Offset(0, 5).Value = ""
Target.Offset(0, 6).Value = ""
Target.Offset(0, 7).Value = ""
Target.Offset(0, 9).Value = ""
Target.Offset(0, 10).Value = ""
Target.Offset(0, 11).Value = ""
Target.Offset(0, 12).Value = ""
Target.Offset(0, 13).Value = ""
Target.Offset(0, 14).Value = ""
Target.Offset(0, 15).Value = ""
End If
End If
End Sub`enter code here`
Disable events by using Application.EnableEvents = False
For consecutive cells use …
Target.Parent.Range(Target.Offset(0, 3), Target.Offset(0, 11)).Value = "NA"
… to write NA between column 3 and 11. Which is much shorter and faster.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
On Error GoTo ENABLE_EVENTS
If Target.Row > 2 And Target.Row < 100 Then
If Target.Column = 2 And Target.Cells.Count = 1 Then
If Target.Value = "Create Account_Personal" Then
Target.Parent.Range(Target.Offset(0, 3), Target.Offset(0, 11)).Value = "NA"
Target.Offset(0, 19).Value = "NO"
Target.Offset(0, 22).Value = "NO THANKS"
ElseIf Target.Value <> "Create Account_Personal" Then
Target.Offset(0, 3).Value = "Select Country"
End If
End If
If Target.Column = 5 And Target.Cells.Count = 1 Then
If Target.Value <> "NA" Then
Target.Offset(0, 1).Value = "select State"
Target.Parent.Range(Target.Offset(0, 2), Target.Offset(0, 7)).Value = ""
Target.Parent.Range(Target.Offset(0, 9), Target.Offset(0, 15)).Value = ""
End If
End If
End If
ENABLE_EVENTS:
Application.EnableEvents = True
If Err.Number <> 0 Then Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
End Sub
Related
Please I have an issue, everytime a change occcurs on the sheet it affects all the rows instead of the row (i) concerned. Confused. Don't for-loops work for worksheet_change ? Pls help. Thanks.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LR As Long
'create a variable for last row of column C, LR
LR = Cells(Rows.Count, "C").End(xlUp).Row
For i = 2 To LR
If Cells(i, 6) = "Yes" And Cells(i, 7).Value = "Full" Then
Target.Value = Cells(i, 3).Value
Cells(i, 9).ClearContents
Cells(i, 10).Value = Cells(i, 8).Value + Cells(i, 9).Value
End If
If Not Intersect(Target, Range("G" & i & ":G" & LR)) Is Nothing And Range("F" & i) = "Yes"
And Target.Value = "Full" Then
Application.EnableEvents = False
Cells(i, 8).Value = Cells(i, 3).Value
Cells(i, 9).ClearContents
Cells(i, 10).Value = Cells(i, 8).Value + Cells(i, 9).Value
Application.EnableEvents = True
End If
If Not Intersect(Target, Range("G" & i & ":G" & LR)) Is Nothing And Range("F" & i) = "Yes" And
Target.Value = "Portion" Then
Application.EnableEvents = False
Cells(i, 8).Value = Cells(i, 3).Value
Cells(i, 10).Value = Cells(i, 8).Value + Cells(i, 9).Value
Application.EnableEvents = True
End If
Next i
End Sub
It seems you need to launch this event for the columns A-E. So, you can start your macro with:
IF Target.Column <= 5 THEN
...
END IF 'at the end of your macro
Like this, when you launch code like Cells(i, 8).Value = ..., Cells(i, 10).Value = ..., ... this macro will be called but it will be stopped immediately.
Apparently you are checking on column, maximum 10, which is in the range of the cells you are changing within your macro. Let's go for another approach:
At the very beginning of your macro, put this line:
Application.EnableEvents = False
At the very end of your macro, put this line:
Application.EnableEvents = True
(and remove the other occurences).
This will make sure you don't call your macro while running it.
I wrote some codes for the data validation column to automatically generate cells and it was seemed to be worked in the first time, but after i closed file and open again, it didn't work.
Thank you help
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ErrHandler
Application.EnableEvents = False
Dim catCode As String
If Target.Cells.Count <> 1 Then Exit Sub
If Target.Column = 2 Then
catCode = Target.Value
Select Case catCode
Case "AUTOMATIC LEVEL", "DIGITAL LEVEL"
Target.Offset(0, 7) = "19/99"
Target.Offset(0, 8) = 1
Case "BAROMETER", "THERMOMETER"
Target.Offset(0, 7) = "24/99"
Target.Offset(0, 8) = 1
Case "BRACKET BUBBLE"
Target.Offset(0, 7) = "23/99"
Target.Offset(0, 8) = 0.5
Case "CARPENTER LEVEL", "REFLECTOR POLE"
Target.Offset(0, 7) = "23/99"
Target.Offset(0, 8) = 0.5
Case "CLINOMETER", "TRIBRACH"
Target.Offset(0, 7) = "23/99"
Target.Offset(0, 8) = 1
Case "DIPMETER"
Target.Offset(0, 7) = "18/99"
Target.Offset(0, 8) = 1
Case "DIGITAL MEASURING POLE"
Target.Offset(0, 7) = "18/99"
Target.Offset(0, 8) = 1
Case "FIBRE GLASS / LENEN TAPE"
Target.Offset(0, 7) = "18/99"
Target.Offset(0, 8) = 1
Case "STEEL POCKET MEASURING TAPE"
Target.Offset(0, 7) = "18/99"
Target.Offset(0, 8) = 1
Case "STEEL TAPE", "STEEL RULER", "STILON TAPE"
Target.Offset(0, 7) = "18/99"
Target.Offset(0, 8) = 1
Case "DISTOMETER"
Target.Offset(0, 7) = "27/99"
Target.Offset(0, 8) = 2
Case "GPS"
Target.Offset(0, 7) = "21/99"
Target.Offset(0, 8) = 1
Case "HAND HELD LASER METER", "TOTAL STATION", "TOTAL STATION WITH REFLECTORLESS"
Target.Offset(0, 7) = "20/99"
Target.Offset(0, 8) = 1
Case "LEVELLING STAFF - TELESCOPIC", "LEVELLING STAFF - NON BARCODE", "LEVELLING STAFF"
Target.Offset(0, 7) = "22/99"
Target.Offset(0, 8) = 1
Case "MEASURING WHEEL"
Target.Offset(0, 7) = "3/00"
Target.Offset(0, 8) = 1
Case "PLANIMETER"
Target.Offset(0, 7) = "26/99"
Target.Offset(0, 8) = 1
Case "PLOTTER"
Target.Offset(0, 7) = "28/99"
Target.Offset(0, 8) = 1
Case "SPRING BALANCE"
Target.Offset(0, 7) = "24/99"
Target.Offset(0, 8) = 2
Case "EDM", "THEODOLITE"
Target.Offset(0, 7) = "N/A"
Target.Offset(0, 8) = 1
End Select
Else
Exit Sub
End If
ErrHandler:
Application.EnableEvents = True
End Sub
Moving the Application.EnableEvents = False inside the If Target.Column = 2 block will prevent your code from exiting without resetting the Application.EnableEvents flag. Managing the Application.EnableEvents flag can become a problem as your code changes in the future (especially when using Exit Sub statements). Another approach that avoids this problem is to handle the Application.EnableEvents flag management separately from your behavior logic...something like.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
On Error Resume Next
LoadCells Target
Application.EnableEvents = True
End Sub
Private Sub LoadCells(ByVal Target As Range)
Dim catCode As String
If Target.Cells.Count <> 1 Then Exit Sub
If Target.Column = 2 Then
catCode = Target.Value
Select Case catCode
Case "AUTOMATIC LEVEL", "DIGITAL LEVEL"
Target.Offset(0, 7) = "19/99"
Target.Offset(0, 8) = 1
Case "BAROMETER", "THERMOMETER"
Target.Offset(0, 7) = "24/99"
Target.Offset(0, 8) = 1
Case "BRACKET BUBBLE"
Target.Offset(0, 7) = "23/99"
Target.Offset(0, 8) = 0.5
Case "CARPENTER LEVEL", "REFLECTOR POLE"
Target.Offset(0, 7) = "23/99"
Target.Offset(0, 8) = 0.5
Case "CLINOMETER", "TRIBRACH"
Target.Offset(0, 7) = "23/99"
Target.Offset(0, 8) = 1
Case "DIPMETER"
Target.Offset(0, 7) = "18/99"
Target.Offset(0, 8) = 1
Case "DIGITAL MEASURING POLE"
Target.Offset(0, 7) = "18/99"
Target.Offset(0, 8) = 1
Case "FIBRE GLASS / LENEN TAPE"
Target.Offset(0, 7) = "18/99"
Target.Offset(0, 8) = 1
Case "STEEL POCKET MEASURING TAPE"
Target.Offset(0, 7) = "18/99"
Target.Offset(0, 8) = 1
Case "STEEL TAPE", "STEEL RULER", "STILON TAPE"
Target.Offset(0, 7) = "18/99"
Target.Offset(0, 8) = 1
Case "DISTOMETER"
Target.Offset(0, 7) = "27/99"
Target.Offset(0, 8) = 2
Case "GPS"
Target.Offset(0, 7) = "21/99"
Target.Offset(0, 8) = 1
Case "HAND HELD LASER METER", "TOTAL STATION", "TOTAL STATION WITH REFLECTORLESS"
Target.Offset(0, 7) = "20/99"
Target.Offset(0, 8) = 1
Case "LEVELLING STAFF - TELESCOPIC", "LEVELLING STAFF - NON BARCODE", "LEVELLING STAFF"
Target.Offset(0, 7) = "22/99"
Target.Offset(0, 8) = 1
Case "MEASURING WHEEL"
Target.Offset(0, 7) = "3/00"
Target.Offset(0, 8) = 1
Case "PLANIMETER"
Target.Offset(0, 7) = "26/99"
Target.Offset(0, 8) = 1
Case "PLOTTER"
Target.Offset(0, 7) = "28/99"
Target.Offset(0, 8) = 1
Case "SPRING BALANCE"
Target.Offset(0, 7) = "24/99"
Target.Offset(0, 8) = 2
Case "EDM", "THEODOLITE"
Target.Offset(0, 7) = "N/A"
Target.Offset(0, 8) = 1
End Select
Else
Exit Sub
End If
End Sub
I have this simple bit of code that automates some dates and stuff when adding line items to a sheet. It works well, but when I insert a line in to the spreadsheet [right-click the line name > insert] an error occurs.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Dim plusWeek
plusWeek = Now() + 7
For Each cell In Target
'========adds closed date, deleted date if status degenerates=========
If cell.Column = 13 And cell = "Closed" Then
Target.Offset(0, -2) = Format(Now(), "yyyy-mm-dd")
End If
If cell.Column = 13 And cell = "In-Progress" Then
Target.Offset(0, -2) = ""
End If
If cell.Column = 13 And cell = "Open" Then
Target.Offset(0, -2) = ""
End If
'========adds date added if date is embty and description is not empty========
If cell.Column = 8 And IsEmpty(Target.Offset(0, 1)) And Not IsEmpty(Target.Offset(0, 0)) Then
Target.Offset(0, 1) = Format(Now(), "yyyy-mm-dd")
Target.Offset(0, 2) = Format(plusWeek, "yyyy-mm-dd")
Target.Offset(0, 5) = "Open"
End If
'========deletes date added if description is empty========
'If cell.Column = 8 And IsEmpty(Target.Offset(0, 0)) Then
' Target.Offset(0, 1) = ""
'End If
Next cell
End Sub
if I paste a line, add a line or delete a line, error 1004 occurs. The debugger highlights this line, but I can't understand where the error comes from.
If cell.Column = 8 And IsEmpty(Target.Offset(0, 1)) And Not
IsEmpty(Target.Offset(0, 0)) Then
Something like this should work:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range, rng As Range
Dim plusWeek
plusWeek = Now() + 7
Set rng = Application.Intersect(Target, Me.Range("H:H,M:M"))
If rng Is Nothing Then Exit Sub
On Error GoTo haveError '<< make sure events don't get left turned off
Application.EnableEvents = False '<< turn events off
For Each cell In rng.Cells
'========adds closed date, deleted date if status degenerates=========
If cell.Column = 13 Then
Select Case cell.Value
Case "Closed": cell.Offset(0, -2) = Format(Now(), "yyyy-mm-dd")
Case "In-Progress", "Open": cell.Offset(0, -2) = ""
End Select
End If
'========adds date added if date is embty and description is not empty========
If cell.Column = 8 And IsEmpty(cell.Offset(0, 1)) And Not IsEmpty(cell) Then
cell.Offset(0, 1) = Format(Now(), "yyyy-mm-dd")
cell.Offset(0, 2) = Format(plusWeek, "yyyy-mm-dd")
cell.Offset(0, 5) = "Open"
End If
'========deletes date added if description is empty========
'If cell.Column = 8 And IsEmpty(Target.Offset(0, 0)) Then
' Target.Offset(0, 1) = ""
'End If
Next cell
haveError:
Application.EnableEvents = True
End Sub
I know that you can have only one private sub worksheet change event however I am struggling to combine code for the events I need. I am new to VBA so any assistance or recommendations are appreciated. Would it be more efficient to use select case?
First code needed:
Private Sub Worksheet_Change(ByVal target As Range)
Dim cell As Range
Set cell = Range("AK9:AR50")
Application.EnableEvents = False
If Not Application.Intersect(cell, target) Is Nothing Then
If target.Column = 37 Then
target.Offset(, 1).Value = target.Value / Range("V" & target.Row).Value
ElseIf target.Column = 38 Then
target.Offset(, -1).Value = WorksheetFunction.RoundUp((target.Value * Range("V" & target.Row).Value), -2)
End If
If target.Column = 39 Then
target.Offset(, 1).Value = target.Value / Range("V" & target.Row).Value
ElseIf target.Column = 40 Then
target.Offset(, -1).Value = WorksheetFunction.RoundUp((target.Value * Range("V" & target.Row).Value), -2)
End If
If target.Column = 41 Then
target.Offset(, 1).Value = WorksheetFunction.RoundUp((target.Value * Range("V" & target.Row).Value), -2)
ElseIf target.Column = 42 Then
target.Offset(, -1).Value = target.Value / Range("V" & target.Row).Value
End If
If target.Column = 43 Then
target.Offset(, 1).Value = target.Value / Range("V" & target.Row).Value
ElseIf target.Column = 44 Then
target.Offset(, -1).Value = WorksheetFunction.RoundUp((target.Value * Range("V" & target.Row).Value), -2)
End If
End If
Application.EnableEvents = True
End Sub
Second code needed:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim controlRng, nRng As Range
Set controlRng = Range("AF9:AF1000")
Set nRng = Intersect(controlRng, Target)
If nRng Is Nothing Then Exit Sub
If Target.Value = "No Promotion" Then
Target.Offset(0, 1) = Range("M" & Target.Row).Value
ElseIf Target.Value = "Promotion" Then
Target.Offset(0, 1) = ""
ElseIf Target.Value = "Demotion" Then
Target.Offset(0, 1) = ""
ElseIf Target.Value = "Partner" Then
Target.Offset(0, 1) = ""
ElseIf Target.Value = "" Then
Target.Offset(0, 1) = ""
End If
End Sub
Select Case will certainly tidy up your code. You might also want to build in a check that Target is not more than a single cell.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Dim controlRng As Range, nRng As Range
Set cell = Range("AK9:AR50")
Set controlRng = Range("AF9:AF1000")
Set nRng = Intersect(controlRng, Target)
Application.EnableEvents = False
If Not Application.Intersect(cell, Target) Is Nothing Then
Select Case Target.Column
Case 37, 39, 41, 43
Target.Offset(, 1).Value = Target.Value / Range("V" & Target.Row).Value
Case 38, 40, 42, 44
Target.Offset(, -1).Value = WorksheetFunction.RoundUp((Target.Value * Range("V" & Target.Row).Value), -2)
End Select
End If
If Not nRng Is Nothing Then
Select Case Target.Value
Case "No Promotion"
Target.Offset(0, 1) = Range("M" & Target.Row).Value
Case "Promotion", "Demotion", "Partner", ""
Target.Offset(0, 1).ClearContents
End Select
End If
Application.EnableEvents = True
End Sub
i keep getting a compile message 'end with without with'
im new to this and i cant see a problem i have a 'With' followed by and 'End with'. if i remove the end with i get a if with out end if.
Private Sub Submitnewcustomer_Click()
Dim RowCount As Long
Dim ctl As Control
If Me.companynameinput.Value = "" Then
MsgBox "Please enter a Company Name.", vbExclamation, "Britannia Monitoring Systems"
Me.companynameinput.SetFocus
Exit Sub
End If
If Me.contactnameinput.Value = "" Then
MsgBox "Please enter a Contact Name.", vbExclamation, "Britannia Monitoring Systems"
Me.companynameinput.SetFocus
Exit Sub
End If
If Me.addressinput.Value = "" Then
MsgBox "Please enter an Address.", vbExclamation, "Britannia Monitoring Systems"
Me.companynameinput.SetFocus
Exit Sub
End If
If Me.Telphone1input.Value = "" Then
MsgBox "Please enter at least 1 phone Number.", vbExclamation, "Britannia Monitoring Systems"
Me.Telphone1input.SetFocus
Exit Sub
End If
If Me.email1input.Value = "" Then
MsgBox "Please enter at least 1 Email Address.", vbExclamation, "Britannia Monitoring Systems"
Me.email1input.SetFocus
End If
RowCount = Worksheets("Database").Range("A1").CurrentRegion.Rows.Count
With Worksheets("Database").Range("A1")
.Offset(RowCount, 0).Value = Me.companynameinput.Value
.Offset(RowCount, 1).Value = Me.contactnameinput.Value
.Offset(RowCount, 2).Value = Me.Telphone1input.Value
.Offset(RowCount, 3).Value = Me.telephone2input.Value
.Offset(RowCount, 4).Value = Me.email1input.Value
.Offset(RowCount, 5).Value = Me.email2input.Value
.Offset(RowCount, 6).Value = Me.email3input.Value
.Offset(RowCount, 7).Value = Me.email4input.Value
.Offset(RowCount, 8).Value = Me.addressinput.Value
.Offset(RowCount, 15).Value = Format(Now, "dd/mm/yyyy hh:nn:ss")
If Me.CheckBox1.Value = True Then
.Offset(RowCount, 12).Value = "Yes"
Else
.Offset(RowCount, 12).Value = "No"
If Me.CheckBox2.Value = True Then
.Offset(RowCount, 13).Value = "Yes"
Else
.Offset(RowCount, 13).Value = "No"
If Me.CheckBox3.Value = True Then
.Offset(RowCount, 14).Value = "Yes"
Else
.Offset(RowCount, 14).Value = "No"
End If
End With
For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then
ctl.Value = ""
ElseIf TypeName(ctl) = "CheckBox" Then
ctl.Value = False
End If
Next ctl
End Sub
I have just struggled for 30 minutes just to post this on here
any help would be great thanks
You are missing a couple of End Ifs in this section. You'll need to sort that out before your code will compile correctly.
If Me.CheckBox1.Value = True Then
.Offset(RowCount, 12).Value = "Yes"
Else
.Offset(RowCount, 12).Value = "No"
If Me.CheckBox2.Value = True Then
.Offset(RowCount, 13).Value = "Yes"
Else
.Offset(RowCount, 13).Value = "No"
If Me.CheckBox3.Value = True Then
.Offset(RowCount, 14).Value = "Yes"
Else
.Offset(RowCount, 14).Value = "No"
End If