baldi

Board Regular
Joined
Sep 15, 2009
Messages
156
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:

Some videos you may like

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,058
Office Version
  1. 365
Platform
  1. Windows
How about
Code:
   If answer = vbYes Then
      With Sheets("Sheet3")
         If .Range("N44").Value + .Range("Q21").Value = 0 Then Exit Sub
         .Unprotect "alexandeR"
         .Range("N25:N32").Copy .Range("N4:N11")
      End With
   End If
 

baldi

Board Regular
Joined
Sep 15, 2009
Messages
156
How about
Code:
   If answer = vbYes Then
      With Sheets("Sheet3")
         If .Range("N44").Value + .Range("Q21").Value = 0 Then Exit Sub
         .Unprotect "alexandeR"
         .Range("N25:N32").Copy .Range("N4:N11")
      End With
   End If

this is the full macro. I cant get it to work. always get an error for "end with or end if"

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
    ActiveSheet.Unprotect "alexandeR"
    Range("N25:N32").Select
    Selection.Copy
    Range("N4:N11").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _
        False, Transpose:=False
    Range("N36:N43").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("N4:N11").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlSubtract, _
        SkipBlanks:=False, Transpose:=False
    Range("Q34:Q40").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("N15:N21").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlSubtract, _
        SkipBlanks:=False, Transpose:=False
  
    Sheets("Sheet2").Select
    ActiveSheet.Unprotect "alexandeR"
    Sheets("sheet3").Select
    Range("N25:N32").Select
    Selection.Copy
    Sheets("Sheet2").Select
    Range("I6:I13").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlSubtract, _
        SkipBlanks:=False, Transpose:=False
    Sheets("sheet3").Select
    Range("N36:N43").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Sheet2").Select
    Range("F6:F13").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlSubtract, _
        SkipBlanks:=False, Transpose:=False
    Sheets("sheet3").Select
    Range("Q34:Q40").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Sheet2").Select
    Range("C6:C12").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlSubtract, _
        SkipBlanks:=False, Transpose:=False
    ActiveSheet.Protect "alexandeR"
    Sheets("sheet3").Select
    Range("N25:N32").Select
    Selection.ClearContents
    Range("N36:N43").Select
    Selection.ClearContents
    Range("Q34:Q40").Select
    Selection.ClearContents
    ActiveSheet.Protect "alexandeR"
    Sheets("LOG").Select
    ActiveSheet.Unprotect "alexandeR"
    Range("A6:I6").Select
    Selection.Delete Shift:=xlUp
    ActiveSheet.Protect "alexandeR"
    Sheets("MAIN").Select
    Range("B8:B14").Select
    Selection.ClearContents
    Range("E7:E14").Select
    Selection.ClearContents
    Range("H7:H14").Select
    Selection.ClearContents
    Range("B8").Select
    
     End If
    
End Sub
 
Last edited by a moderator:

baldi

Board Regular
Joined
Sep 15, 2009
Messages
156
I must be putting the end with or end if in the wrong spot. just don't know where to put it
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,058
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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
 

rlv01

Well-known Member
Joined
May 16, 2017
Messages
771
With / End With have to be nested in the same way as If /End if

For example:

Code:
Sub BaldiMacro()
    Dim answer As Integer
    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
        With Sheets("sheet3")
            .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
            .Range("N25:N32").Copy
        End With
        With Sheets("Sheet2")
            .Unprotect "alexandeR"
            .Range("I6:I13").PasteSpecial Paste:=xlPasteAll, Operation:=xlSubtract, SkipBlanks:=False, Transpose:=False
        End With
        With Sheets("sheet3")
            .Range("N36:N43").Copy
        End With
        With Sheets("Sheet2")
            .Range("F6:F13").PasteSpecial Paste:=xlPasteAll, Operation:=xlSubtract, SkipBlanks:=False, Transpose:=False
        End With
        With Sheets("sheet3")
            .Range("Q34:Q40").Copy
        End With
        With Sheets("Sheet2")
            .Range("C6:C12").PasteSpecial Paste:=xlPasteAll, Operation:=xlSubtract, SkipBlanks:=False, Transpose:=False
            .Protect "alexandeR"
        End With
        With Sheets("sheet3")
            .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 If
End Sub
 

baldi

Board Regular
Joined
Sep 15, 2009
Messages
156

ADVERTISEMENT

you rewrote the whole macro. thank you.
I will try it and let you know how it works.
 

rlv01

Well-known Member
Joined
May 16, 2017
Messages
771
you rewrote the whole macro..

Sort of, but not really. I just cleaned it up a bit. Your code uses the 'select-do' method, which is where we all start out, mainly because that's how the macro-recorder works. This methodology selects something, then performs an action on it.

For example:
Code:
    Range("N25:N32").Select  '<- you "select" a source range
    Selection.Copy '<- you perform a copy on the selection
    Range("N4:N11").Select '<- you "select" a destination range
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _  '<- you paste to the selection
    False, Transpose:=False

But after a while you learn that it is more efficient to work on the ranges directly and skip the 'select-do' method:
Code:
     .Range("N25:N32").Copy  '<- I copy the range directly, no "selection" step
     .Range("N4:N11").PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:=False, Transpose:=False '<- I paste to the destination range directly
 

baldi

Board Regular
Joined
Sep 15, 2009
Messages
156
can you mix the 2 methods together and still have the macro work?

so now how do I add this to the macro.

if the SUM(N44,Q21)=0 on sheet3. the macro stops.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,058
Office Version
  1. 365
Platform
  1. Windows
Did you try my suggestion from post#5?
 

Watch MrExcel Video

Forum statistics

Threads
1,108,585
Messages
5,523,728
Members
409,532
Latest member
Lmfacc

This Week's Hot Topics

Top