Macro not working if assigned to a button or even on its own

subash86

New Member
Joined
May 18, 2018
Messages
2
Hi

I have written a VB macro to transfer contents from one excel to another. However, if I run the macro (either from VB window or assign it to a button) it returns only one value (NO ERROR) but if I debug and have both the related excel files and go to each of the excel files step by step, I achieve the desired result.
I am a new user of macro, but I can see my logic in the code is right and since it does not return any error, I feel the error is with respect to opening of the corresponding excel files for that purpose.
I can share the code, if anyone can look into it and give me a suggestion.

Thanks
Suba
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi

I have written a VB macro to transfer contents from one excel to another. However, if I run the macro (either from VB window or assign it to a button) it returns only one value (NO ERROR) but if I debug and have both the related excel files and go to each of the excel files step by step, I achieve the desired result.
I am a new user of macro, but I can see my logic in the code is right and since it does not return any error, I feel the error is with respect to opening of the corresponding excel files for that purpose.
I can share the code, if anyone can look into it and give me a suggestion.

Thanks
Suba
If you post your code and an explanation of what it is intended to do, you will greatly improve your chances of getting some help.
 
Upvote 0
you need to post the complete macro for us to review.

Hi Jim.
Please find my code below. I am trying to check three conditions and copy the corresponding names from one excel to another excel.

==========================================================
Code:
Sub cond_copy()
'These are location indicators for destination workbook
y1 = 4
 y2 = 4
 y3 = 4
 
For x = 4 To 18
  Workbooks("Sample_Doc.xlsm").Activate
  Sheets("Sheet1").Select
 
  If IsEmpty(Cells(x, 5).Value) = True Then
  y1 = y1
  y2 = y2
  y3 = y3
  ElseIf Cells(x, 5) = "E" And (Cells(x, 2) = "HIGH" Or Cells(x, 2) = "GR7") Then
  If Cells(x, 4) = "Y" Then
  temp = Split(Cells(x, 1).Value, " ")(0)
  FirstName = temp & "*"
  Else
  FirstName = Split(Cells(x, 1).Value, " ")(0)
  End If
 
Workbooks("Sample_Allocation.xlsm").Sheets("AS1").Activate
  Cells(y2, 7) = FirstName
  y2 = y2 + 1
 ElseIf Cells(x, 5) = "M" And (Cells(x, 2) = "HIGH" Or Cells(x, 2) = "GR7") Then
  If Cells(x, 4) = "Y" Then
  temp = Split(Cells(x, 1).Value, " ")(0)
  FirstName = temp & "*"
  Else
  FirstName = Split(Cells(x, 1).Value, " ")(0)
  End If
 
Workbooks("Sample_Allocation.xlsm").Sheets("AS1").Activate
  Cells(y1, 2) = FirstName
  y1 = y1 + 1
  ElseIf Cells(x, 5) = "N" And (Cells(x, 2) = "HIGH" Or Cells(x, 2) = "GR7") Then
  If Cells(x, 4) = "Y" Then
  temp = Split(Cells(x, 1).Value, " ")(0)
  FirstName = temp & "*"
  Else
  FirstName = Split(Cells(x, 1).Value, " ")(0)
  End If
  
  Workbooks("Sample_Allocation.xlsm").Sheets("AS1").Activate
  Cells(y3, 12) = FirstName
  y3 = y3 + 1
  ElseIf Cells(x, 5) = "O" Then
  y1 = y1
  y2 = y2
  y3 = y3
  End If
  Next x
End Sub
======================================

Thanks
Suba
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,393
Members
449,446
Latest member
CodeCybear

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