Macro Code

SAT-65

Board Regular
Joined
Feb 10, 2008
Messages
69
How can you wright into the code to skip over parts of the code depending on a cell value?
 
You can test for it:

<font face=Calibri>    <SPAN style="color:#00007F">If</SPAN> Range("A1") = 1 <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#007F00">'   Do something</SPAN><br>    <SPAN style="color:#00007F">Else</SPAN><br>        <SPAN style="color:#007F00">'   Do something else</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN></FONT>

That's just a very basic example, but hopefully it'll get you started.
 
Upvote 0
Thanks for the start. I'm doing somthing wrong?

If Range("A1") = 1 Then
' Range("D3").Select
Selection.Copy
Range("B3").Select
ActiveSheet.Paste
Else
' Range("D6").Select
Application.CutCopyMode = False
Selection.Copy
Range("B6").Select
ActiveSheet.Paste
End If

End Sub
 
Upvote 0
Thanks for the start. I'm doing somthing wrong?
What do you think you are doing wrong?

The If Else End If structure looks fine to me.

The only problem I can see is Application.CutCopyMode = False followed by the use of Selection.
 
Upvote 0
OK, you followed my example a bit too closely. The text you see in green that's preceded by an apostrophe isn't actually code. The apostrophe forces VBA to see it as a comment. So what's happening is that you're not actually copying anything, therefore not pasting anything as a result. Here's what it should be:

If Range("A1") = 1 Then
Range("D3").Copy Range("B3")
Else
Range("D6").Copy Range("B6")
End If


Note that if you see "Select" followed by "Selection" you can generally eliminate both statements, which will really help your code. That's what I did, and why the code looks so short.

Now, since you're only copying single cell ranges, you can eliminate copying altogether:

If Range("A1") = 1 Then
Range("B3") = Range("D3")
Else
Range("B6") = Range("D6")
End If
 
Upvote 0

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