Help with another Macro

baldi

Board Regular
Joined
Sep 15, 2009
Messages
159
I start this macro with this.
Code:
answer = MsgBox(prompt:="You can only RESET ONE TIME! Do you really want to do this? ", Buttons:=vbYesNo + vbCritical, Title:="Transaction Error")
    If answer = vbYes Then
    Sheets("Sheet3").Select
    If Range("N44") + Range("Q41") = "" Then
    Exit Sub
    End If
Sheets("Sheet3").Select
    ActiveSheet.Unprotect "alexandeR"
    Range("N25:N32").Select
    Selection.Copy
    Range("N4:N11").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _
        False, Transpose:=False

I want the macro to stop if cell N44 plus Q21 equal 0 but I cant get it right.
any and all help would be great thanks
 
Last edited by a moderator:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
It's setting a variable called Ws2 to sheets("Sheet2")
So rather than constantly referring to sheets("Sheet2") you can simply refer to Ws2, as I did in that code.
 
Upvote 0
it works great.
I had tried it before with no luck but once I realized I had the wrong cell number. it worked.
 
Upvote 0
Try
Code:
Sub baldi()
   Dim Ws2 As Worksheet
   
   Set Ws2 = Sheets("Sheet2")
   
   answer = MsgBox(prompt:="You can only RESET ONE TIME! Do you really want to do this? ", Buttons:=vbYesNo + vbCritical, title:="Transaction Error")
   If answer = vbNo Then Exit Sub
   With Sheets("Sheet3")
      If .Range("N44").Value + .Range("Q21").Value = 0 Then Exit Sub
      .Unprotect "alexandeR"
      .Range("N25:N32").Copy
      .Range("N4:N11").PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _
         False, Transpose:=False
      .Range("N36:N43").Copy
      .Range("N4:N11").PasteSpecial Paste:=xlPasteAll, Operation:=xlSubtract, _
         SkipBlanks:=False, Transpose:=False
      .Range("Q34:Q40").Copy
      .Range("N15:N21").PasteSpecial Paste:=xlPasteAll, Operation:=xlSubtract, _
         SkipBlanks:=False, Transpose:=False

      Ws2.Unprotect "alexandeR"
      .Range("N25:N32").Copy
      Ws2.Range("I6:I13").PasteSpecial Paste:=xlPasteAll, Operation:=xlSubtract, _
         SkipBlanks:=False, Transpose:=False
      .Range("N36:N43").Copy
      Ws2.Range("F6:F13").PasteSpecial Paste:=xlPasteAll, Operation:=xlSubtract, _
         SkipBlanks:=False, Transpose:=False
      .Range("Q34:Q40").Copy
      Ws2.Range("C6:C12").PasteSpecial Paste:=xlPasteAll, Operation:=xlSubtract, _
         SkipBlanks:=False, Transpose:=False
      Ws2.Protect "alexandeR"
      .Range("N25:N32").ClearContents
      .Range("N36:N43").ClearContents
      .Range("Q34:Q40").ClearContents
      .Protect "alexandeR"
   End With
   With Sheets("LOG")
      .Unprotect "alexandeR"
      .Range("A6:I6").Delete shift:=xlUp
      .Protect "alexandeR"
   End With
   With Sheets("MAIN")
      .Range("B8:B14").ClearContents
      .Range("E7:E14").ClearContents
      .Range("H7:H14").ClearContents
      .Range("B8").Select
   End With
End Sub

thank you for your help. this works great
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
Glad we could help & thanks for the feedback

I just rewrote one of the macros in the workbook like how you guys just showed me and it works great. thanks for the help.

Code:
[FONT=Calibri][COLOR=#000000]answer =MsgBox(prompt:="You can only RESET ONE TIME! Do you really want to dothis? ", Buttons:=vbYesNo + vbCritical, Title:="Fill/CreditError")[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    If answer = vbNo Then Exit Sub[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    With Sheets("sheet3")[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    If .Range("K41").Value +.Range("K51").Value = 0 Then Exit Sub[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Unprotect "alexandeR"[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Range("K33:K40").Copy[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Range("Q7:Q14").PasteSpecialPaste:=xlPasteAll, Operation:=xlSubtract, _[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]        SkipBlanks:=False, Transpose:=False[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Range("K43:K50").Copy[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Range("Q18:Q25").PasteSpecialPaste:=xlPasteAll, Operation:=xlSubtract, SkipBlanks:= _[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]        False, Transpose:=False[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Range("K33:K40").Copy[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Range("N4:N11").PasteSpecialPaste:=xlPasteAll, Operation:=xlSubtract, _[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]        SkipBlanks:=False, Transpose:=False[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Range("K43:K50").Copy[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Range("N4:N11").PasteSpecialPaste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]        False, Transpose:=False[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Range("K33:K40").ClearContents[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Range("K43:K50").ClearContents[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Protect "alexandeR"[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    End With[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    With Sheets("LOG")[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Unprotect "alexandeR"[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Range("A6:I6").Deleteshift:=xlUp[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Protect "alexandeR"[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    End With[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    With Sheets("MAIN")[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Range("B22:B29").ClearContents[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Range("E22:E29").ClearContents[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    .Range("B8").Select[/COLOR][/FONT]
[FONT=Calibri][COLOR=#000000]    End With[/COLOR][/FONT]
 
Upvote 0
That is a lot better:
You could shorten you code more by doing something like this:
Code:
Sub My_Sub()
With Sheets("sheet3")
.Range("K43:K50").Copy Range("Q18")
End With
End Sub
 
Upvote 0
That's not going to include the Add/Subtract operations that the OP is using.
 
Upvote 0
this works great

Your new code looks a lot better. I notice the variable "answer" is undeclared. If you put this line at the top of your code module

Code:
Option Explicit

It will force you to declare all variables, or else get a compiler error. It's a bit of a pain at first, but declaring all variables will make it easier for you to debug your code.
 
Upvote 0
Looks like you got alot of good help,

if you don't mind me suggesting next steps.

Practice some error handling, before your next project,there is alot of good videos about it on youtube.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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