Excel VBA Topic 4.1.1 - Example, Calling a Sub Procedure

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

calling subprocedure from button malfunctions but running it in vba editor works fine. what might be the reason?

beenay
Автор

running sub-procedure from button malfunctions but running it from vba editor works fine?

this problem started when i protected all worksheet.
I wrote the following code to unprotect all the sheets at the beginning of sub-procedure
and again protect all worksheet in the end of sub-procedure.

Please help!

sub name()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Unprotect

code

Dim ws As Worksheet
For Each ws In Worksheets
ws.Unprotect

end sub

beenay
Автор

for the moment i am not being able to solve it by taking it as double but it works with integer

Sub math()


Dim num1 As Integer

Dim num2 As Integer


Dim diff As Integer

Dim prod As Integer



Sheets("sheet3").Select

Range("B2").Select

num1 = ActiveCell.Value

ActiveCell.Offset(1, 0).Select

num2 = ActiveCell.Value


MsgBox num1 & ", " & num2 & ", " & diff & " , " & prod




diff = num1 - num2

prod = num1 * num2

MsgBox num1 & ", " & num2 & ", " & diff & ", " & prod



ActiveCell.Offset(2, 0).Select

ActiveCell.Value = diff

ActiveCell.Offset(1, 0).Select

ActiveCell.Value = prod






End Sub

deepakshrestha
Автор

Hello,

I thought that I could pass arguments in a call where the Caller had different names for arguments than in the Called sub. But I am getting and error, at least in how I am rying to do this. The error is "Compile error: Duplicate declaration in current scope" (OK, Help).

My code is a little different than the example here, but I think functionally the the same. I commented out the originally called Sub and a couple of line of the original sub just to keep the comparison visible. This is what I have that causes the error. I thought the arguments only had to be in the same order, but did not need the same name, again.

Option Explicit
Sub Math()
  Dim num1 As Double, num2 As Double, diff As Double, prod As Double
  Range("B2").Select
  num1 = Range("B2").Value
  num2 = Range("B3").Value
  MsgBox "Num1=" & num1 & " Num2=" & num2
  'diff = num1 - num2
  'prod = num1 * num2
  Call calcdp(num1, num2, diff, prod)
  MsgBox "num1=" & num1 & "  " & "num2=" & num2 & "  " & "diff=" & diff & "  " & "prod=" & prod
  ActiveCell.Offset(3, 0).Value = diff
  ActiveCell.Offset(4, 0).Value = prod
End Sub
'Sub calcdp(num1, num2, diff, prod)
'  diff = num1 - num2
'  prod = num1 * num2
'End Sub
Sub calcdp(w, x, y, z)
  Dim w As Double, x As Double, y As Double, z As Double
  diff = w - x
  prod = y * z
End Sub

The above fails with the error indicated.
Thanks,
David

davidlanman
Автор

I am able to create the button but when I click it does not display the macro can someone suggest what i am doing doing, I know i might have to record the macro but where do i do that

libanabu
visit shbcf.ru