I have the following code that simply delete the range in excel.. but I don't know why that if you click the button to delete the selected range it happens the entire range are deleting and I don't know why. Can you help me thanks in advance
Private Sub cmdDel_Click()
cmdEdit.Enabled = False: cmdAdd.Enabled = False
cmdClose.Caption = "CANCEL"
If MsgBox("Delete this record?", vbYesNo + vbQuestion, "Message") = vbYes Then
Range("A" & r & ":" & "V" & r).Delete
r = 0
MsgBox "Record deleted!", vbExclamation, "Message"
End If
Call UserForm_Activate
End Sub
It's simple because the value of r is 0 so your string actually becomes
Range("A:V").Delete
Set a value of r before deleting and the problem will go away
Also if you are manually selecting the range and deleting the range then use this
Selection.Delete
Related
I have an ActiveX TextBox named txtNewPeymankar and a command button named cmdAdd. I want when user type a string in txtNewPeymankar in my form and click Add button, that value compare with column one of Table1 (Table1[column1]) in DATA sheet and if match with any of cells, show error massage "duplicated value!. please try again." and if not match, save it in the end of column1.
I write this code but not work.
Private Sub cmdAdd_Click()
Dim iRow1 As Long
iRow1 = Sheets("DATA").Range("A1048576").End(xlUp).Row + 1
If Application.Match(txtNewPeymankar.Text, Range(Table1[column1]), 0) Then
MsgBox "duplicated value!. please try again.", vbOKOnly + vbInformation
Else
With ThisWorkbook.Sheets("DATA")
.Range("A" & iRow1).Value = txtNewPeymankar.Value
End With
End If
End Sub
When deleting a product from the "Prod_List"(sheet2) I need to copy the row and paste it into a "DeletedList"(sheet3).
This sub "Delete_product" should, from the display on the "Summary"(sheet1) ask the user to confirm the action, then copy the row from the "Prod_List"(sheet2), pasting it into the "DeletedList"(sheet3) and clean the view from the display on the "Summary"(sheet1)
'Delete Product
Option Explicit
Dim PRodRow As Long, ProdCol As Long
Sub Delete_Product()
With Summary
If .Range("B8").Value = Empty Then
MsgBox "Please Select a correct product to delete"
Exit Sub
End If
If MsgBox("Are you sure you want to delete this Product?", vbYesNo, "Delete Product") = vbNo Then Exit Sub
PRodRow = DeletedList.Range("A999999").End(xlUp).Row + 1
For ProdCol = 1 To 22
'DeletedList.Cells(PRodRow, ProdCol).Value = .Range(DeletedList.Cells(1, ProdCol).Value).Value 'add data to product to deleted tab
Next
With Prod_List
PRodRow = Summary.Range("B8").Value 'product row
Prod_List.Range(PRodRow & ":" & PRodRow).EntireRow.Delete
With Summary
.Range("J4,J5,J6,J7,J9,J10,n5,J11,N7,N8,N9,N12,N13,N14,J13,J14,J15").ClearContents
End With
End With
End With
End Sub
When I run it, it deletes the row correctly, however, it doesn't copy the row into the "DeletedList"(sheet3), however, when the line below is not as an observation the data is pasted on the "DeletedList"(sheet3), but it doesn't delete it from the "ProductList"(sheet2)
'DeletedList.Cells(PRodRow, ProdCol).Value =
.Range(DeletedList.Cells(1, ProdCol).Value).Value 'add data to
product to deleted tab "
Any ideas or suggestions on how to fix it?
Thank you!
ive been learning VBA and Excel in the past 2 weeks by my own during my free time, but sometimes we need some help, and currently ive no one to help besides the internet. So ive developed a Form(Sheet1) in a sheet using shapes and excel cells so the user could perform operations like insert,update, new register to the other sheet(Data) which is my Data Sheet or DataTable more specifically. But im struggling to get the update button to work. i could definitely use some help.
Heres my code:
Public Upda As String
Sub Search()
'
' Search Macro
'
Dim Sheet As String, ans
On Error GoTo Erro:
Data.Activate
Sheet = Data.Name
ans = InputBox("Write down the ID", "Search")
If ans = "" Then
Sheet1.Activate
Exit Sub
End If
Dim C
With Worksheets(Data).Range("A:A")
Set C = .Find(ans, LookIn:=xlValues, lookat:=xlWhole)
If Not C Is Nothing Then
C.Activate
Sheet1.Cells(17, 9).Value = C.Value ' Id
Sheet1.Cells(9, 4).Value = C.Offset(0, 1).Value ' Name
' here goes the other fields to be inserted
Sheet1.Activate
Upda = Sheet1.Cells(17, 9).Text
Else
Sheet1.Activate
MsgBox "Insert a valid ID", vbCritical, "Search"
End If
End With
Exit Sub
Erro:
MsgBox "Something went wrong, contact the Adm!", vbCritical, "Erro"
End Sub
'Update macro need to get a fix
Sub Update()
'update macro
Dim Sheet As String
On Error GoTo Erro
If IsEmpty(Range("I17")) Or IsEmpty(Range("D9")) Then ' there are more fields to validate
MsgBox "All the fields must have a value", vbExclamation, "Upda"
If Upda = "" Then
MsgBox "Please retry the search", vbExclamation, "Update"
Exit Sub
End If
Dim C
'
Data.Activate
Sheet = Data.Name
With Worksheets(Sheet).Range("A:A")
Set C = .Find(Upda, LookIn:=xlValues, lookat:=xlWhole)
If Not C Is Nothing Then
C.Activate
ActiveCell.Value = Sheet1.Cells(17, 9).Text ' ID
ActiveCell.Offset(0, 1).Value = Sheet1.Cells(9, 4).Text ' Name
'Update the table with the contents of the form1
Sheet1.Activate
Range("I6:J6").ClearContents
' remaining code to clear the contents of the form sheet1
Upda = ""
'Call clear
Else
MsgBox "ID number not found", vcCritical, "Update"
End If
End With
Exit Sub
Erro:
MsgBox "Something went wrong, contact the Adm!", vbCritical, "ERRO"
End Sub
Sub clear()
'
' clear Macro
'
Range("I17").ClearContents
' remaining code to cleear the contents of the form sheet1
Upda = ""
End Sub
Each one of those macros are associated with a Button(Shape), evrything is working besides the Update one.
Im getting the follow error which makes no sense to me
PS:if u need more information please let me know
You are missing the End if statement for the first If in the below block of code:
If IsEmpty(Range("I17")) Or IsEmpty(Range("D9")) Then ' there are more fields to validate
MsgBox "All the fields must have a value", vbExclamation, "Upda"
End if 'Missing If in the original code
If Upda = "" Then
MsgBox "Please retry the search", vbExclamation, "Update"
Exit Sub
End If
I have a number of things I want to achieve using VBA on a particular sheet.
1) Have a 3 button message box pop up when a certain condition is met.
2) Display the active cell address in a specific cell.
3) When hitting Enter after inputting data only to empty cells in a particular column, make the cursor jump to another column on the same row.
(Codes for each of these are at the end of the post)
I have the code to do all three of these things and they all work fine on their own, indeed the codes for items 1 & 2 also both work fine together, but when I add the code for item 3, a message box appears with
"Compile Error:
Ambiguous name detected: Worksheet_SelectionChange"
and the offending article in the code window is also highlighted.
I've noticed that the code for items 2 & 3 have the heading "Private Sub Worksheet_SelectionChange(ByVal Target As Range)"
If I remove the code for item 2, im then faced with a different message box
"Compile Error: Only comments may appear after End Sub, End Function, or End Property"
and highlights the words "Option Explicit" in the code window.
This also seems to break the code for item 1 as well.
I suspect the fact that I have two sets of code in the same window with the same "heading" might be the issue here. Is there a way for me to "blend" them so that they'll all play nicely with each other?
**---CODE FOR ITEM 1---**
Private Sub Worksheet_Calculate()
Dim r As Range
For Each r In Range("L:L")
If r.Value < 0 Then
result = MsgBox("You do not have enough stock to fulfil this request" & vbNewLine & vbNewLine & vbNewLine & _
"Please click: -" & vbNewLine & vbNewLine & _
" -Abort to order more stock" & vbNewLine & _
" -Retry to enter a different value" & vbNewLine & _
" -Ignore to receive stock", _
_
vbAbortRetryIgnore + vbDefaultButton2 + vbExclamation, "Negative Stock Level Warning")
End If
Next r
If result = vbAbort Then
MsgBox "Opening web browser", vbOKOnly + vbInformation, "New program warning!"
ActiveWorkbook.FollowHyperlink _
Address:="https://uk.rs-online.com/login", _
NewWindow:=True
End If
If result = vbRetry Then
MsgBox "Please enter a smaller parts count value", vbOKOnly + vbInformation, "Parts Count Input"
ActiveCell.Offset(-1, 0).Select
End If
If result = vbIgnore Then
MsgBox "You will now be directed to the Goods In window", vbOKOnly + vbInformation, "Receive Stock"
Sheets("Goods In").Activate
End If
End Sub
---CODE FOR ITEM 2---
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Range("A1048575") = ActiveCell.Address
End Sub
---CODE FOR ITEM 3---
Option Explicit
Dim emptyCell As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub ' don't bother with multicell selections
If Cells(1, Target.Column).Value <> "S.I.#" Then Exit Sub ' don't bother with selections outside "S.I.#" column
If emptyCell And Not IsEmpty(Target.Value) Then Cells(Target.Row, Range("A1", Cells(1, Columns.Count).End(xlToLeft)).Find(what:="Count", _
LookIn:=xlValues, lookat:=xlWhole).Column).Select ' if current cell was empty and now it is not then skip to "Count" column same row
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
emptyCell = IsEmpty(Target.Value) ' store the info about current selection being empty
End Sub
I am facing a complication with excel macro syntax.
My workbook contains several sheets, with the first one titled "Reference Sheet".
There are some entries in the other sheets that I do not want the user to edit through the sheet they are currently working on but only through the Reference Sheet.
I locked the cells and used protect sheet, however I want the user to receive a prompt message whenever they double click on one of the cells in the concerned range, asking them if they want to change the selected cell but in the Reference Sheet.
My aim is to have the same cell selected in the current sheet, selected in the Reference Sheet to be able to edit it.
I posted the following code in the corresponding sheet VB but apparently there is an error in the Cells property at the end -> Cells(val1, val2).Select
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("C2:C5,E2")) Is Nothing Then
Else
Dim Msg As String, Title As String
Dim Config As Integer, Ans As Integer
Msg = "Do not modify this entry from the current sheet."
Msg = Msg & vbNewLine
Msg = Msg & "This modification is only enabled in the Reference Sheet."
Msg = Msg & vbNewLine
Msg = Msg & "Would you like to go to the corresponding cell and modify it?"
Title = "Attention"
Config = vbYesNo + vbExclamation
Ans = MsgBox(Msg, Config, Title)
If Ans = vbYes Then
Dim val1 As Integer, val2 As Integer
val1 = ActiveCell.Row
val2 = ActiveCell.Column
Worksheets("Reference Sheet").Activate
Cells(val1, val2).Select
End If
End If
End Sub
Your thoughts are much appreciated.
The reason your code fails is that in a Worksheet module an unqualified range reference (Cells(val1, val2) in your case) refers to the worksheet Module, not the Active worksheet. Since you have just activated another sheet, you are trying to select a cell on an inactive sheet, which causes the error.
A better way to do it is
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("C2:C5,E2")) Is Nothing Then
Else
Dim Msg As String, Title As String
Dim Config As Long, Ans As VbMsgBoxResult
Msg = "Do not modify this entry from the current sheet."
Msg = Msg & vbNewLine
Msg = Msg & "This modification is only enabled in the Reference Sheet."
Msg = Msg & vbNewLine
Msg = Msg & "Would you like to go to the corresponding cell and modify it?"
Title = "Attention"
Config = vbYesNo + vbExclamation
Ans = MsgBox(Msg, Config, Title)
If Ans = vbYes Then
With Worksheets("Reference Sheet")
.Activate
.Range(Target.Address).Select
End With
End If
End If
End Sub