Macro won't run when button is clicked

Palma47

New Member
Joined
Sep 30, 2022
Messages
1
Platform
  1. Windows
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
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
if I assign it to a egular button
Based on the name of the sub, that code does not look like it belongs to a button click event at all. You should be able to find whatever procedure is linked to the button by right clicking on it and choose 'view code' (or whatever the option is - I forget at the moment). I think you'll find it's not what you posted.

Please post code within code tags to maintain readability and indentation. TBH I'm not going to even try to follow that to see if there's anything else that stands out.
 
Upvote 0

Forum statistics

Threads
1,215,782
Messages
6,126,872
Members
449,345
Latest member
CharlieDP

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