Avatar of Justin
Justin
 asked on

Do Wend Macro not working

Hi Guys, I have a simple If Statement within a Do Wend Loop which is not working on Sheet "Data". Anyone any idea why?
Behavioural-Mismatch-Report---Copy.xlsm
ProgrammingMicrosoft OfficeMicrosoft ExcelVBA

Avatar of undefined
Last Comment
[ fanpages ]

8/22/2022 - Mon
Martin Liss

Your code is password protected so please either tell us the password or post a version that isn't protected.
Justin

ASKER
Hi, the password is "accounts"
Martin Liss

Please tell me what you mean when you say it's not working.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Justin

ASKER
It should change the value in Column A on the same line the 2 IF statements are satisfied and it does not.
Shums Faruk

Try changing this line:
If Cells(i, 4).Value = "4131581" & Cells(i, 13).Value = "Deposit" Then
to
If Cells(i, 4).Value = "4131581" And Cells(i, 13).Value = "Deposit" Then
[ fanpages ]

Hi,

I think we are discussing this routine:

Sub Activate()

Worksheets("Data").Select

Dim i As Integer

i = 1

While Cells(i, 1).Value <> ""

If Cells(i, 4).Value = "4131581" & Cells(i, 13).Value = "Deposit" Then
Cells(i, 1).Value = "L14c"

i = i + 1
Else
i = i + 1


End If

Wend



End Sub

Open in new window



Did you intend this statement:

If Cells(i, 4).Value = "4131581" & Cells(i, 13).Value = "Deposit" Then...

To read:

If Cells(i, 4).Value = "4131581" And Cells(i, 13).Value = "Deposit" Then...

?



PS. I would also advise changing:

Dim i As Integer

to read:

Dim i As Long
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Justin

ASKER
Hi, it works good. Can you also tell me how if an IF statement in column 4 is satisfied, I can multiply the value in column 11 by -1? This is the code I think

Sub Activate()

Worksheets("Data").Select

Dim i As Long

i = 1

While Cells(i, 1).Value <> ""
ElseIf Cells(i, 4).Value = "4513973" Then
Cells(i, 11).Value = Cells(i, 11) * -1
i = i + 1
Else
i = i + 1


End If

Wend



End Sub
[ fanpages ]

I think what you are trying to do is this:

  While Cells(i, 1).Value <> ""

      If Cells(i, 4).Value = "4513973" Then
         Cells(i, 11).Value = Cells(i, 11) * -1
         i = i + 1
      Else
         i = i + 1
      End If

  Wend

Open in new window


Are you confident that every value in column [K] is going to be numeric?
Justin

ASKER
Hi, it works now. However, I am trying to use a Macro button to kick it off, but when I assign the Macro button, It cannot see
the Macro in Module 2. Any ideas? Here's the code that works.

Sub Activate()

Worksheets("Data").Select

Dim i As Long

i = 1

While Cells(i, 1).Value <> ""

If Cells(i, 4).Value = "4131581" And Cells(i, 13).Value = "Deposit" Then
Cells(i, 1).Value = "L14c"
i = i + 1

ElseIf Cells(i, 4).Value = "4378014" And Cells(i, 13).Value = "Deposit" Then
Cells(i, 1).Value = "L14c"

i = i + 1

ElseIf Cells(i, 4).Value = "4193174" And Cells(i, 13).Value = "Deposit" Then
Cells(i, 1).Value = "L14c"
i = i + 1

ElseIf Cells(i, 4).Value = "4193014" And Cells(i, 13).Value = "Deposit" Then
Cells(i, 1).Value = "L14c"
i = i + 1

ElseIf Cells(i, 4).Value = "4513973" Then
Cells(i, 11).Value = Cells(i, 11) * -1
i = i + 1
ElseIf Cells(i, 4).Value = "4494550" Then
Cells(i, 11).Value = Cells(i, 11) * -1
i = i + 1
ElseIf Cells(i, 4).Value = "4510417" Then
Cells(i, 11).Value = Cells(i, 11) * -1
i = i + 1
Else
i = i + 1


End If

Wend



End Sub
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Shums Faruk

Are you using Shape to call the macro or Command Button?
Shums Faruk

If you are using Shape then you would be able to assign macro on right click, like below image:
Call MacroIf you are using Command Button then right click and select view code, it will take you to blank module, in between Private Sub CommandButton_Click and End Sub, paste your code without Sub & End Sub like below;
Private Sub CommandButton1_Click()
Worksheets("Data").Select

Dim i As Long

i = 1

While Cells(i, 1).Value <> ""

If Cells(i, 4).Value = "4131581" And Cells(i, 13).Value = "Deposit" Then
Cells(i, 1).Value = "L14c"
i = i + 1

ElseIf Cells(i, 4).Value = "4378014" And Cells(i, 13).Value = "Deposit" Then
Cells(i, 1).Value = "L14c"

i = i + 1

ElseIf Cells(i, 4).Value = "4193174" And Cells(i, 13).Value = "Deposit" Then
Cells(i, 1).Value = "L14c"
i = i + 1

ElseIf Cells(i, 4).Value = "4193014" And Cells(i, 13).Value = "Deposit" Then
Cells(i, 1).Value = "L14c"
i = i + 1

ElseIf Cells(i, 4).Value = "4513973" Then
Cells(i, 11).Value = Cells(i, 11) * -1
i = i + 1
ElseIf Cells(i, 4).Value = "4494550" Then
Cells(i, 11).Value = Cells(i, 11) * -1
i = i + 1
ElseIf Cells(i, 4).Value = "4510417" Then
Cells(i, 11).Value = Cells(i, 11) * -1
i = i + 1
Else
i = i + 1


End If

Wend
End Sub

Open in new window

Justin

ASKER
From the developer tab, I am using Insert -> Form Controls -> Button (Form Control), but I cannot see it when I try to assign the Macro.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Shums Faruk

Form Control will not have option of assigning the macro, try using ActiveX Command Button
Justin

ASKER
Here's the file. Why can I not assign the Macro called "Activate" in Module 2?
Behavioural-Mismatch-Report---21-03.xlsm
Shums Faruk

Its password protected
Your help has saved me hundreds of hours of internet surfing.
fblack61
Justin

ASKER
the password is "accounts". How come 1 macro is showing but not the other?
ASKER CERTIFIED SOLUTION
Shums Faruk

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Justin

ASKER
superb as ever
Justin

ASKER
Hi, can you double check your command button? It does not seem to be working.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
[ fanpages ]

You're welcome.
Shums Faruk

JCutcliffe,

It is working here, but you should re-open the question, as Fanpages also deserve points.
[ fanpages ]

Thanks Shums.

Your acknowledgement of my contribution was enough.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck