Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

how add formula to the code

0

greeting  guys 

 I   try  adding   this  formula "=ROW()-1"  to  the  code  in column A  in sheet BRANDS , but  it  clears   the  column A  with  header . the  idea is    when  delete specific  row  should  not  affect  for  autonumber  . it  should  re arrange  again 1,2,3   . actually  i  don't  want  writing  this  formula  manually in column  A   becuase  the  autonumbering  depends  on the  textboxes  on  userform  when  fill the  data  in textboxes & comboboxes. 

I add  this  to  the  code  but  doesn't  work

 With Columns("A:A")
           .Formula = "=ROW()-1"
           .Value = Value
           End With

this  is  the  whole code 

Private Sub CommandButton3_Click()

    Dim lReply      As VbMsgBoxResult
    Dim ws          As Worksheet
    Dim strFind     As String
    Dim FoundCell   As Range

    strFind = Me.TextBox2.Value

    Set ws = ThisWorkbook.Worksheets("BRANDS")

    If Len(strFind) = 0 Then
        MsgBox "PLEASE WRITE  THE CODE", vbExclamation, "Entry Required"
        Me.TextBox2.SetFocus
        Exit Sub
    Else
        Set FoundCell = ws.Columns(2).Find(What:=strFind, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True)
        If Not FoundCell Is Nothing Then
            lReply = MsgBox("Are you sure you want to delete variation (Code " & FoundCell.Value & ")?", 276, "Confirm")
            If lReply = vbNo Then Exit Sub
            FoundCell.Value = ""
            FoundCell.EntireRow.Delete
           With Columns("A:A")
           .Formula = "=ROW()-1"
           .Value = Value
           End With
        Else
         MsgBox "Could Not find " & strFind & " On " & ws.Name, 48, "Not Found"
        End If
    End If




    strFind = "VO" & strFind
    On Error Resume Next
    Application.DisplayAlerts = False
    Sheets(strFind).Delete
    Application.DisplayAlerts = True
    On Error GoTo 0
    'Close Form
    Unload Me
End Sub

to  know  button's name  is DELETE  on userform 

any  help  to  do  that,please?

Answer
Discuss

Answers

0
Selected Answer

Mussa

You code had two problems- firstly you applied the formula to the entire column then you set .Value for each cell of the column to something called "Value" (which VBA saw as an undeclared variable with no value so the column was made blank, overwriting the formula).

The correction needed is in bold below (notice it's dot value in the right hand side of the formula in the second bold line) and in the revised file attached. You'll see I've limited the action to the used range in column A (after the item has been deleted) rather then the whole column:

           With Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
           .Formula = "=ROW()-1"
           .Value = .Value
           End With
Hope this works for you.
Discuss

Discussion

thanks  for  you  fixing  my   mistake .
Mussa (rep: 48) Oct 17, '21 at 7:20 am
You're welcome Mussa.

If you declare Option Explicit at the top of your module, VBA won't allow you to run codes with undeclared variables (so would alert you about "Value")- the Microsoft note Option Explicit statement. That isn't necessary for simple macros though- see Don's tutorial Variables in Macros VBA for Excel - Practical Guide
John_Ru (rep: 6142) Oct 17, '21 at 7:39 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login