VBA Help

Chippa

New Member
Joined
Aug 11, 2012
Messages
6
Hey everyone,

I am super new to vba and am trying to get started to help speed things along with some study work. Firstly I am trying to create a macro that runs based on the value of a cell, and I need that macro to copy and paste data at the start of a loop. I have made the copy/paste macro by recording, now I just need the automatic start.

My code for the copy/paste is;

Sub Run()
'
' Run Macro
'
' Keyboard Shortcut: Ctrl+m


Range("s91:s103").Select
Selection.Copy
Range("G91:G103").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub

And I need it to run if cell U104 = 1.

Can anyone help? Also can someone point to a good book or notes to help get me started in doing this myself.

Cheers

Dave
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Please explain how a 1 gets into cell U104. Is it manually entered? Does a 1 pop up from a formula in U104? Is it from a web query or from copying and pasting from elsewhere?
 
Last edited:
Upvote 0
First off, since you are only copy values, there is no need to use 4 code lines to do it... you can assign the values of one range directly to another. So, the 4 code lines you posted can all be replaced by this...

Code:
Range("G91:G103").Value = Range("S91:S103").Value
Now, to run that only when U104 equals 1 takes a simple If..Then statement...

Code:
If Range("U104").Value = 1 Then
  Range("G91:G103").Value = Range("S91:S103").Value
End If
As for the "automatic start", I have the same question as Tom posted.
 
Upvote 0
I am thinking by his reference of "automatic start" it will be an event code such as Change or Calculate at the worksheet level, per my questions, but we'll see.
 
Upvote 0
Please explain how a 1 gets into cell U104. Is it manually entered? Does a 1 pop up from a formula in U104? Is it from a web query or from copying and pasting from elsewhere?

Hi,

Cheers for the reply. The 1 is obtained from an 'if' statement, the data the answer at the end of my calculations is the re-calculated until the 1 becomes a 0, then it will stop. Does that make some sense?

Cheers
 
Upvote 0
Sounds like you have a formula in cell U104 that sometimes can produce a 1.

Try this, right-click your worksheet tab and left-click to select View Code. Paste the below procedure into the large white area that is the worksheet module. Press Alt+Q to return to the worksheet.

Code:
Private Sub Worksheet_Calculate()
If Range("U104").Value = 1 Then Range("G91:G103").Value = Range("S91:S103").Value
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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