Integer to string in MsgBox message

wasntme

New Member
Joined
Feb 1, 2019
Messages
37
Hello,

I have a macro, activated by button click, that moves the entire row to another sheet ("Archive") if cell value in column F is "Finalised". Macro works fine.

I want to add message after it finishes with information how many rows have been moved. The problem i am encountering is that it always show 0. Hoped CStr() will fix it, but no success.
Code:
 Sub Update() 'moving finalised projects to archive

Dim Check As Range, r As Long, lastrow2 As Long, lastrow As Long, status As Integer


Application.EnableEvents = False
Application.ScreenUpdating = False


status = Application.WorksheetFunction.CountIf(Range("F3:F100"), "Finalised") 'count finalised projects moved to archive
lastrow = Worksheets("VAT registrations").UsedRange.Rows.Count


For r = lastrow To 2 Step -1
    If Worksheets("VAT registrations").Range("F" & r).Value = "Finalised" Then
       Worksheets("VAT registrations").Rows(r).Cut Destination:=Worksheets("Archive").Range("A" & Rows.Count).End(xlUp)(2)
       Worksheets("VAT registrations").Rows(r).Delete
    End If
Next r


Application.EnableEvents = True
Application.ScreenUpdating = True


MsgBox "File updated, projects moved to archive: " & CStr(status)
End Sub

Thanks in advance.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
You don't need the CStr, but which sheet is active when you run the code?
 
Upvote 0
Why not just add a counter right inside your loop, so you are actually counting how many rows you are moving as you move them?
Code:
Sub Update() 'moving finalised projects to archive

Dim Check As Range, r As Long, lastrow2 As Long, lastrow As Long, status As Long

Application.EnableEvents = False
Application.ScreenUpdating = False

lastrow = Worksheets("VAT registrations").UsedRange.Rows.Count

For r = lastrow To 2 Step -1
    If Worksheets("VAT registrations").Range("F" & r).Value = "Finalised" Then
       Worksheets("VAT registrations").Rows(r).Cut Destination:=Worksheets("Archive").Range("A" & Rows.Count).End(xlUp)(2)
       Worksheets("VAT registrations").Rows(r).Delete
       status = status + 1
    End If
Next r

Application.EnableEvents = True
Application.ScreenUpdating = True

MsgBox "File updated, projects moved to archive: " & status

End Sub
 
Last edited:
Upvote 0
Maybe...(NOT TESTED)

Try changing this
Code:
status = Application.WorksheetFunction.CountIf(Range("F3:F100"), "Finalised") 'count finalised projects moved to archive
lastrow = Worksheets("VAT registrations").UsedRange.Rows.Count

to
Code:
With Worksheets("VAT registrations")
    lastrow = [B][COLOR=#ff0000].[/COLOR][/B]UsedRange.Rows.Count
    status = Application.WorksheetFunction.CountIf([SIZE=3][COLOR=#ff0000][B].[/B][/COLOR][/SIZE]Range("F[B][COLOR=#ff0000]2[/COLOR][/B]:F" [COLOR=#ff0000]& lastrow[/COLOR]), "Finalised") 'count finalised projects moved to archive
End With
Hope this helps

M.
 
Upvote 0
You are welcome.

As Marcelo pointed out, note that in your previous code, your formula was working on a hard-coded range ending in row 100, but your loop was calculating the last row.
So the ranges may not have been the same too.
 
Upvote 0
You are welcome.

As Marcelo pointed out, note that in your previous code, your formula was working on a hard-coded range ending in row 100, but your loop was calculating the last row.
So the ranges may not have been the same too.

I have used this range to avoid any slowing down (the sheet will never have so much entries, 50 tops), but i learn with every thread here :). Once again many thanks!
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,434
Members
448,961
Latest member
nzskater

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