Help with 'minimalizing' / 'trimming down' a working formula

StephanV

Board Regular
Joined
Jun 25, 2008
Messages
93
I've got 2 working formulas, but they are rather inefficient I guess. Because im new with vba, i just copy/pasted al the info i found on this board into one formula, and now it works!

I think its easy to follow what the formulas should do:

Code:
Private Sub CommandButton1_click()
Dim ans As Long
ans = MsgBox("Weet je zeker dat je het certificaat wilt verwijderen?? Is er een cel in de juiste rij geselecteerd?", vbYesNo)
If ans = vbYes Then
   Dim CopyRange As Range, NextCell As Range
   Dim lngRow As Long
   lngRow = ActiveCell.Row
   Set CopyRange = Worksheets("Certificate_Database").Range("A" & lngRow & ":W" & lngRow)
   Set NextCell = Worksheets("Prullenbak").Cells(Cells.Rows.Count, 1).End(xlUp).Offset(1, 0)
   CopyRange.Copy
   NextCell.PasteSpecial
   Application.CutCopyMode = False
   With Sheets("Certificate_Database")
      .Range("A" & lngRow & ":W" & lngRow).ClearContents
    End With
        With ActiveWorkbook.Worksheets("Certificate_Database").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Else
        Exit Sub
    End If
End Sub

Code:
Private Sub CommandButton2_click()
Dim ans As Long
ans = MsgBox("Weet je zeker dat je het certificaat wilt verlengen en kopieren naar NDO_Database? Is er een cel in de juiste rij geselecteerd?", vbYesNo)
If ans = vbYes Then
   Dim CopyRange As Range, NextCell As Range
   Dim lngRow As Long
   lngRow = ActiveCell.Row
   Set CopyRange = Worksheets("Certificate_Database").Range("A" & lngRow & ":D" & lngRow & ",T" & lngRow & ":W" & lngRow)
   Set NextCell = Worksheets("NDO_Database").Cells(Cells.Rows.Count, 1).End(xlUp).Offset(1, 0)
   CopyRange.Cut
   NextCell.PasteSpecial
   Application.CutCopyMode = False
   With Sheets("Certificate_Database")
      .Range("T" & lngRow & ":W" & lngRow).ClearContents
      .Range("O" & lngRow).Value = .Range("O" & lngRow).Value + 730
    End With
    Else
        Exit Sub
    End If
End Sub

How can i 'clean' these formulas (and write them more correctly the next time :P)
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Those are VBA procedures not formulas. What makes you think they are inefficient?

Mmh, well my lack on VBA knowledge i guess :P

When I read all the VBA procedures (examples on this board), there is a certain way of writing them down, and I did not follow it. Furthermore, people more familliar with VBA could perhaps make the code simpler (like writing 100 x 100 x 100 x 100 as 100^4).
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top