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!
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
I have a form that I have created in Excel as an input area for Production Information. Once the data has been filled into the Form I have a save button with a macro behind it. This to copy and paste the data from the form into a line in the Info Log area below the form.
I have tried to use a "helper" column to place True if the information being saved has already been saved once. I am not sure I have all of that correct either.
Sub Record_Save()
'With Sheets("Roll Line Form")
'Application.ScreenUpdating = False
'Range("B43:B" & Cells(Rows.Count, 1).End(xlUp).Row) =
"=CountIf(D2:D4,D2,)>1"
'Range("B:B").AutoFilter 1, "True"
'If .Range("B:B").Value = "True" Then
'MsgBox "This has already been entered"
'Exit Sub
'End If
'End With
With Sheets("Roll Line Form")
If .Range("D2").Value = Empty Then
MsgBox "Please Enter a Date"
Exit Sub
End If
RecordRow = .Range("C999999").End(xlUp).Row + 1 'Find First Available Row
For RecordCol = 3 To 46
.Cells(RecordRow, RecordCol).Value = .Range(.Cells(41,
RecordCol).Value).Value
Next RecordCol
End With
End Sub
I want to have the Macro understand that the data is already saved and not to save the data in the Log area. I have commented out the code I tried to use but when it was still running I would get the error code of Run-time error '1004': Application-defined or object error
This is on the line that starts Range ("B43:B"...
Following on from a previous question I asked today - I have modified code written by Roy Cox (Thank you for saving me SO much time!) to create a userform to add, modify and delete users' details in an analysis tool I am creating.
It works perfectly when working with user data on a single worksheet.
Selecting a user and clicking 'delete' deletes their user data on the worksheet. I have modified the code so that when a user is added or deleted, it should check each worksheet and adds or deletes rows accordingly.
This is the code to delete the pupil data on a single sheet:
Private Sub cmbDelete_Click()
Dim msgResponse As String 'confirm delete
Application.ScreenUpdating = False
'get user confirmation
msgResponse = MsgBox("This will delete the selected record. Continue?", _
vbCritical + vbYesNo, "Delete Entry")
Select Case msgResponse 'action dependent on response
Case vbYes
'c has been selected by Find button on UserForm
Set c = ActiveCell
c.EntireRow.Delete 'remove entry by deleting row
'restore form settings
With Me
.cmbAmend.Enabled = False 'prevent accidental use
.cmbDelete.Enabled = False 'prevent accidental use
.cmbAdd.Enabled = True 'restore use
'clear form
Call ClearControls
End With
Case vbNo
Exit Sub 'cancelled
End Select
Application.ScreenUpdating = True
End Sub
I have tried modifying it to delete the user data on each worksheet, as follows:
Private Sub cmbDelete_Click()
Dim Sh As Worksheet
Dim msgResponse As String 'confirm delete
Application.ScreenUpdating = False
'get user confirmation
msgResponse = MsgBox("This will delete the selected record. Continue?", _
vbCritical + vbYesNo, "Delete Entry")
Select Case msgResponse 'action dependent on response
Case vbYes
For Each Sh In ThisWorkbook.Sheets
With Sh.UsedRange
'c has been selected by Find button
Set c = ActiveCell
c.EntireRow.Delete 'remove entry by deleting row
End With
Next
'restore form settings
With Me
.cmbAmend.Enabled = False 'prevent accidental use
.cmbDelete.Enabled = False 'prevent accidental use
.cmbAdd.Enabled = True 'restore use
'clear form
Call ClearControls
End With
Case vbNo
Exit Sub 'cancelled
End Select
Application.ScreenUpdating = True
End Sub
but this version deletes the user's data and the data on the 4 rows beneath them. It does not delete data from the next worksheet at all.
Can anyone offer any advice please?
change:
Case vbYes
For Each Sh In ThisWorkbook.Sheets
With Sh.UsedRange
'c has been selected by Find button
Set c = ActiveCell
c.EntireRow.Delete 'remove entry by deleting row
End With
Next
to:
Case vbYes
Dim l As Long
l = ActiveCell.Row 'store currently active cell row
For Each Sh In ThisWorkbook.Worksheets
Sh.Rows(l).Delete
Next
should you ever be interested in knowing why your previous code didn't work:
1)
Set c = ActiveCell
would set c to the currently active cell, i.e. the cell your "Find" button selected in the currently active sheet
2) while
c.EntireRow.Delete
would always, quite unsurprisingly, delete c entire row, i.e. the same row in the sheet where c has been found in, since nobody is setting c again and point to another sheet range.
and simply looping through Sheets collection doesn't change the Active sheet
see codes below.
I have the 'beforesave' code in the Workbook module and it works fine when I'm in the active sheet. However from the table I use on sheet 2 I also have a pivot table on sheet 1. To refresh my pivot I use an inserted button with an attached macro (this is in the module section)
Sub Refresh_Pivot()
'
' Refresh_Pivot Macro
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
ActiveWorkbook.Save
End Sub
On Activate.Workbook.Save its starts to act on my other code (which is in the workbook module), I want this to happen as a pivot table with missing data is not a good tool. However on using this it defaults with an error and highlights the cell.Offset(0, 1).Select - How can I prevent this?
Ideally I want the user to select OK on the msgbox and then the screen page changes to Sheet 2 and highlights the offending cell.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim esave As Range
Dim psave As Range
Dim jsave As Range
Dim RAll As Range
Dim cell As Range
Set esave = Sheet2.Range("Table1[Estimated Claim (USD)]")
Set psave = Sheet2.Range("Table1[Provisional Claim (USD)]")
Set jsave = Sheet2.Range("Table1[Agreed Claim (USD)]")
Set RAll = Union(esave, psave, jsave)
For Each cell In RAll
If cell.Value <> "" And cell.Offset(0, 1).Value = "" Then
Dim missdata
missdata = MsgBox("Missing Data - Enter the Date for WorkBook to Save", vbOKOnly, "Missing Data")
Cancel = True
cell.Offset(0, 1).Select
Exit For
End If
Next cell
End Sub
.Select should be avoided.
INTERESTING READ
I also want to know why are you trying to select that cell? What is the purpose. If you want to interact with it, then you can do that without selecting it. For example
If cell.Value <> "" And cell.Offset(0, 1).Value = "" Then
Dim missdata
missdata = MsgBox("Missing Data - Enter the Date for WorkBook to Save", vbOKOnly, "Missing Data")
Cancel = True
With cell.Offset(0, 1)
'
''~~> Do something
'
End With
Exit For
End If
Having said that if you still want to select that cell then you need to be on that sheet. There are two ways now. One is like I mentioned in the comment above.
Add Sheet2.Activate just before For Each cell In RAll in the Workbook_BeforeSave event or do that in the button's click event.
Sub Refresh_Pivot()
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
Sheet2.Activate
ActiveWorkbook.Save
End Sub
Another point. You might want to pass Cancel = True before the Exit For to disable the save?
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