May someone please help me with the code bellow? I didn't write that. It is an old workbook that my boss asked me to update... So, when I run the macro through the VBA it works perfectly. However, if I assign it to a egular button and try to run it, it doesn't work. It just jump to one of the tabs in the workbook. Thanks.
Sub CalculateWeightBreakMargin()
'Calculate Weight Break Margin
Dim iWeightBreakCol As Integer
Dim iPriceCheck As Integer
Dim bPriceCheck As Boolean
wksPricingForm.Activate
'If bNoEnteredFields Then
' MsgBox "Please populate at least column " & Chr(wksPricingForm.Range("InputPalletStart").Column + 64) & " with Pallet and/or Loose metrics", vbCritical, WorkbookName
' Exit Sub
' End If
'Check for blank Rates
'For iPriceCheck = 9 To 3 Step -1
' If Cells(54, iPriceCheck).Value = vbNullString Then
' bPriceCheck = True
' Exit For
' End If
' Next iPriceCheck
'If bPriceCheck And Not gbMultiDest Then '2/27/10 BG added flag to bypass check if MultiDest calculations
' iPriceCheck = MsgBox("Not all Per Kg rates have been input, do you want to continue?", vbQuestion + vbYesNo, WorkbookName)
' If iPriceCheck = vbNo Then Exit Sub
'End If
'Check Exchange Rates
'If ExchangeRateNotValid Then
' Exit Sub
' End If
'Populate Op Margins Sheets with Input Rates
With wksOpMarginWB1
.Unprotect
.Range("PerAddrMatrix").ClearContents
.Range("f18").Value = "=WB1_Kg_Rate*ExchangeRate"
.Range("f19").Value = "=WB1_Address_Rate*ExchangeRate"
.Protect
End With
With wksOpMarginWB2
.Unprotect
.Range("PerAddrMatrix").ClearContents
.Range("f18").Value = "=WB2_Kg_Rate*ExchangeRate"
.Range("f19").Value = "=WB2_Address_Rate*ExchangeRate"
.Protect
End With
With wksOpMarginWB3
.Unprotect
.Range("PerAddrMatrix").ClearContents
.Range("f18").Value = "=WB3_Kg_Rate*ExchangeRate"
.Range("f19").Value = "=WB3_Address_Rate*ExchangeRate"
.Protect
End With
With wksOpMarginWB4
.Unprotect
.Range("PerAddrMatrix").ClearContents
.Range("f18").Value = "=WB4_Kg_Rate*ExchangeRate"
.Range("f19").Value = "=WB4_Address_Rate*ExchangeRate"
.Protect
End With
With wksOpMarginWB5
.Unprotect
.Range("PerAddrMatrix").ClearContents
.Range("f18").Value = "=WB5_Kg_Rate*ExchangeRate"
.Range("f19").Value = "=WB5_Address_Rate*ExchangeRate"
.Protect
End With
With wksOpMarginWB6
.Unprotect
.Range("PerAddrMatrix").ClearContents
.Range("f18").Value = "=WB6_Kg_Rate*ExchangeRate"
.Range("f19").Value = "=WB6_Address_Rate*ExchangeRate"
.Protect
End With
With wksOpMarginWB7
.Unprotect
.Range("PerAddrMatrix").ClearContents
.Range("f18").Value = "=WB7_Kg_Rate*ExchangeRate"
.Range("f19").Value = "=WB7_Address_Rate*ExchangeRate"
.Protect
End With
'Loops Through the Weight Breaks (based on Shipment Type)
'Fills in Input Sheet with data
'Stores Op Margin on appropriate Op Margin Tab
For iWeightBreakCol = 9 To 2 Step -1
If wksPricingForm.Cells(54, iWeightBreakCol).Value <> vbNullString Then
RefreshPricingInputs (iWeightBreakCol)
wksInputs.Calculate
Sheet4.Calculate
'Populate appropriate Op Margin based on the Weight Break being calculated
Select Case iWeightBreakCol
Case 2 'Base
With wksPricingForm
.Unprotect
.Calculate
Sheet4.Calculate
.Range("OverallMargin").Value = Sheet4.Range("s66").Value
.Protect
End With
Case 3 '1-44
With wksOpMarginWB1
.Unprotect
.Calculate
Sheet4.Calculate
.Range("wb1_margin").Value = Sheet4.Range("s66").Value
.Protect
End With
Case 4 '45-70
With wksOpMarginWB2
.Unprotect
.Calculate
Sheet4.Calculate
.Range("wb2_margin").Value = Sheet4.Range("s66").Value
.Protect
End With
Case 5 '71-99
With wksOpMarginWB3
.Unprotect
.Calculate
Sheet4.Calculate
.Range("wb3_margin").Value = Sheet4.Range("s66").Value
.Protect
End With
Case 6 '100-299
With wksOpMarginWB4
.Unprotect
.Calculate
Sheet4.Calculate
.Range("wb4_margin").Value = Sheet4.Range("s66").Value
.Protect
End With
Case 7 '300-499
With wksOpMarginWB5
.Unprotect
.Calculate
Sheet4.Calculate
.Range("wb5_margin").Value = Sheet4.Range("s66").Value
.Protect
End With
Case 8 '
With wksOpMarginWB6
.Unprotect
.Calculate
Sheet4.Calculate
.Range("wb6_margin").Value = Sheet4.Range("s66").Value
.Protect
End With
Case 9
With wksOpMarginWB7
.Unprotect
.Calculate
Sheet4.Calculate
.Range("wb7_margin").Value = Sheet4.Range("s66").Value
.Protect
End With
End Select
End If
Next iWeightBreakCol
'wksPricingForm.Unprotect
'If InStr(1, wksPricingForm.Range("InputCurrency").Value, "USD") <> 0 Then
' Sheet4.Visible = xlSheetVisible
' Sheet18.Visible = xlSheetHidden
' Else
' Sheet4.Visible = xlSheetHidden
' Sheet18.Visible = xlSheetVisible
'End If
'If Range("InputOrigin").Value <> "United States" Then
' Rows("49:53").Hidden = True
'Else
' Rows("49:53").Hidden = False
'End If
wksPricingForm.Protect
End Sub
Sub CalculateWeightBreakMargin()
'Calculate Weight Break Margin
Dim iWeightBreakCol As Integer
Dim iPriceCheck As Integer
Dim bPriceCheck As Boolean
wksPricingForm.Activate
'If bNoEnteredFields Then
' MsgBox "Please populate at least column " & Chr(wksPricingForm.Range("InputPalletStart").Column + 64) & " with Pallet and/or Loose metrics", vbCritical, WorkbookName
' Exit Sub
' End If
'Check for blank Rates
'For iPriceCheck = 9 To 3 Step -1
' If Cells(54, iPriceCheck).Value = vbNullString Then
' bPriceCheck = True
' Exit For
' End If
' Next iPriceCheck
'If bPriceCheck And Not gbMultiDest Then '2/27/10 BG added flag to bypass check if MultiDest calculations
' iPriceCheck = MsgBox("Not all Per Kg rates have been input, do you want to continue?", vbQuestion + vbYesNo, WorkbookName)
' If iPriceCheck = vbNo Then Exit Sub
'End If
'Check Exchange Rates
'If ExchangeRateNotValid Then
' Exit Sub
' End If
'Populate Op Margins Sheets with Input Rates
With wksOpMarginWB1
.Unprotect
.Range("PerAddrMatrix").ClearContents
.Range("f18").Value = "=WB1_Kg_Rate*ExchangeRate"
.Range("f19").Value = "=WB1_Address_Rate*ExchangeRate"
.Protect
End With
With wksOpMarginWB2
.Unprotect
.Range("PerAddrMatrix").ClearContents
.Range("f18").Value = "=WB2_Kg_Rate*ExchangeRate"
.Range("f19").Value = "=WB2_Address_Rate*ExchangeRate"
.Protect
End With
With wksOpMarginWB3
.Unprotect
.Range("PerAddrMatrix").ClearContents
.Range("f18").Value = "=WB3_Kg_Rate*ExchangeRate"
.Range("f19").Value = "=WB3_Address_Rate*ExchangeRate"
.Protect
End With
With wksOpMarginWB4
.Unprotect
.Range("PerAddrMatrix").ClearContents
.Range("f18").Value = "=WB4_Kg_Rate*ExchangeRate"
.Range("f19").Value = "=WB4_Address_Rate*ExchangeRate"
.Protect
End With
With wksOpMarginWB5
.Unprotect
.Range("PerAddrMatrix").ClearContents
.Range("f18").Value = "=WB5_Kg_Rate*ExchangeRate"
.Range("f19").Value = "=WB5_Address_Rate*ExchangeRate"
.Protect
End With
With wksOpMarginWB6
.Unprotect
.Range("PerAddrMatrix").ClearContents
.Range("f18").Value = "=WB6_Kg_Rate*ExchangeRate"
.Range("f19").Value = "=WB6_Address_Rate*ExchangeRate"
.Protect
End With
With wksOpMarginWB7
.Unprotect
.Range("PerAddrMatrix").ClearContents
.Range("f18").Value = "=WB7_Kg_Rate*ExchangeRate"
.Range("f19").Value = "=WB7_Address_Rate*ExchangeRate"
.Protect
End With
'Loops Through the Weight Breaks (based on Shipment Type)
'Fills in Input Sheet with data
'Stores Op Margin on appropriate Op Margin Tab
For iWeightBreakCol = 9 To 2 Step -1
If wksPricingForm.Cells(54, iWeightBreakCol).Value <> vbNullString Then
RefreshPricingInputs (iWeightBreakCol)
wksInputs.Calculate
Sheet4.Calculate
'Populate appropriate Op Margin based on the Weight Break being calculated
Select Case iWeightBreakCol
Case 2 'Base
With wksPricingForm
.Unprotect
.Calculate
Sheet4.Calculate
.Range("OverallMargin").Value = Sheet4.Range("s66").Value
.Protect
End With
Case 3 '1-44
With wksOpMarginWB1
.Unprotect
.Calculate
Sheet4.Calculate
.Range("wb1_margin").Value = Sheet4.Range("s66").Value
.Protect
End With
Case 4 '45-70
With wksOpMarginWB2
.Unprotect
.Calculate
Sheet4.Calculate
.Range("wb2_margin").Value = Sheet4.Range("s66").Value
.Protect
End With
Case 5 '71-99
With wksOpMarginWB3
.Unprotect
.Calculate
Sheet4.Calculate
.Range("wb3_margin").Value = Sheet4.Range("s66").Value
.Protect
End With
Case 6 '100-299
With wksOpMarginWB4
.Unprotect
.Calculate
Sheet4.Calculate
.Range("wb4_margin").Value = Sheet4.Range("s66").Value
.Protect
End With
Case 7 '300-499
With wksOpMarginWB5
.Unprotect
.Calculate
Sheet4.Calculate
.Range("wb5_margin").Value = Sheet4.Range("s66").Value
.Protect
End With
Case 8 '
With wksOpMarginWB6
.Unprotect
.Calculate
Sheet4.Calculate
.Range("wb6_margin").Value = Sheet4.Range("s66").Value
.Protect
End With
Case 9
With wksOpMarginWB7
.Unprotect
.Calculate
Sheet4.Calculate
.Range("wb7_margin").Value = Sheet4.Range("s66").Value
.Protect
End With
End Select
End If
Next iWeightBreakCol
'wksPricingForm.Unprotect
'If InStr(1, wksPricingForm.Range("InputCurrency").Value, "USD") <> 0 Then
' Sheet4.Visible = xlSheetVisible
' Sheet18.Visible = xlSheetHidden
' Else
' Sheet4.Visible = xlSheetHidden
' Sheet18.Visible = xlSheetVisible
'End If
'If Range("InputOrigin").Value <> "United States" Then
' Rows("49:53").Hidden = True
'Else
' Rows("49:53").Hidden = False
'End If
wksPricingForm.Protect
End Sub