Multiple copy values + call macro not working - excel

I recently started to work with macros and I am trying to copy a selection from a sheet, paste it in the same sheet and to repeat this in the whole workbook.
The problem is that only in the first sheet the values are copied.
I also linked the macros to a button and all macros are in the same module; you can see my sketch below:
Sub CopyValues_sheet1()
'
' CopyValues_sheet1 Macro
'
'
Range("C6:AD47").Select
Selection.Copy
ActiveWindow.SmallScroll Down:=18
Range("C53:AD94").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Sub CopyValues_sheet2()
'
' CopyValues_sheet2 Macro
'
'
Range("C6:AD47").Select
Selection.Copy
ActiveWindow.SmallScroll Down:=50
Range("C53").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("C60").Select
End Sub
Sub Button1_Click()
Call CopyValues_sheet1
Call CopyValues_sheet2
End Sub

The way you're doing this, you need to activate the sheet where you wish to complete the copy/paste operation. Something like this:
Sub CopyValues_sheet2()
'
' CopyValues_sheet2 Macro
'
Sheet2.Activate ' <--- Do this!
Range("C6:AD47").Select
Selection.Copy
ActiveWindow.SmallScroll Down:=50
Range("C53").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("C60").Select
End Sub
If you're doing the exact same thing on every sheet, you might want to parameterize your sub/function with a sheet name, and use something like this:
Worksheets("Your_worksheet_name").Activate

Related

Looking to get this to work on a specific tab within Excel

I need help getting this to get this to run on only one sheet within a workbook.
Sub BOXED_FMO_RawMilk()
'
' BOXED_FMO_RawMilk Macro
'
'
Worksheets("BOXED").Range("I17:I26").Select
Selection. Copy
Range("J17").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub

VBA to create running list of values based on same cells

I have two cells (R5 and S5) that are aggregates based on a RAND() function. I want to create a running list of those values, which change every time the sheet recalculates. The list should have about 100K iterations of this. I wrote a "dumb" bit of VBA that copy/pastes the values into a list.
This does sort of work, but is too slow to scale to 100k iterations and I get a gap every 50 records for some reason.
There must be a better / faster way to do this with a loop or something like that? TIA.
Sub Macro2()
'
' Macro2 Macro
'
'
Range("R5:S5").Select
Selection.Copy
Range("U5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("U6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("U7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
A Worksheet Calculate
Sheet Module e.g. Sheet1
Option Explicit
Private Sub Worksheet_Calculate()
Macro2 Me
End Sub
Standard Module e.g. Module1
Option Explicit
Sub Macro2(ByVal ws As Worksheet)
Application.EnableEvents = False
Dim lCell As Range
Set lCell = ws.Cells(ws.Rows.Count, "U").End(xlUp).Offset(1)
lCell.Resize(, 2).Value = ws.Range("R5:S5").Value
Application.EnableEvents = True
End Sub

Adding If/End If to macro

I'm trying to add a condition to a copy and paste macro where it copies a row from table1 and pastes it onto table2 if the row in table1 is red.
I've tried:
Sub ColdLake1()
If Range("B55").Interior.ColorIndex = 3 Then
Range("B55:H55").Select
Selection.Copy
Range("C140").Select
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
End Sub
It returns nothing. Any ideas?
Thanks (I'm quite new to all this).
This worked for me:
Sub ColdLake1()
With ActiveSheet
Debug.Print "ColorIndex", .Range("B55").Interior.ColorIndex
If .Range("B55").Interior.ColorIndex = 3 Then
.Range("B55:H55").Copy
With .Range("C140")
.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End With
End If
End With
End Sub
If your code isn't doing anything it may be because the color isn't what you think it is, or for some reason your code is not entering the If block. Put some breakpoints and see what is going on. https://www.excel-easy.com/vba/examples/debugging.html

Information regarding the Run time error 9

I am trying to copy the range of value from one sheet in excel to another. I have copied this formula from another part of my sheet that works however i cam coming up with the run time error 9.
Sub SaveJambStudEC()
'
' SaveCalcsJambEC Macro
'
Dim page As Integer
page = Cells(4, "T").Value
Range("A70:AN70").Select
Selection.Copy
Range("A71").Select
ActiveCell.Offset(page, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1:O63").Select
Selection.Copy
Sheets("10.3 JambCalcs EC").Select
Range("A1").Select
ActiveCell.Offset((page - 1) * 63, 0).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats
Sheets("9.3 Jamb Design EC").Select
Range("T5").Select
Selection.Copy
Range("N9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("T31").Value = 0
Call JambECsetDesignOptions
Call CopyJambOptiValues
Range("J9").Activate
End Sub
using the below code you will check if there is a sheet with that name. if you dont receive any message box means that there is no sheet with such name
Option Explicit
Sub test()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "10.3 JambCalcs EC" Then
MsgBox "Sheet Appears"
Exit Sub
End If
Next ws
End Sub
Note
'ThisWorkbook' refer to the workbook that the code included. If you want to clearly declare the workbook you could declare a variable 'Dim wb as Workbook' and then set the workbook 'Set wb=Workbooks("workbook name")'

VB to Post to New Line per Button Click

I recorded a macro, linked to a button, on say sheet1. The object of what I need done:
When the button is pressed, certain cells are selected and copied to a "summary" page on another sheet.
Sheet1 has a drop down that shows certain information. So after data is selected from the drop down, the user will push the button and post that data to the summary sheet.
The macro works fine (please note I am a VBA noob), but I need assistance in adding functionality that after every button press, it copies the data on the next line - in other words, if data is in row 1 already, it must place the data in row 2, and so on.
The VB code I have is as follows:
Sub Test()
'
' Test Macro
'
'
Range("C32:N32").Select
Selection.Copy
Sheets("Summary").Select
Range("D3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks
_
:=False, Transpose:=False
Sheets("Comm Payable").Select
Range("C3:D3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Summary").Select
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks
_
:=False, Transpose:=False
Sheets("Comm Payable").Select
Range("N1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Summary").Select
Range("C3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks
_
:=False, Transpose:=False
Range("B4").Select
Sheets("Comm Payable").Select
Range("O1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
Range("O1").Select
End Sub
Please could someone assist with the addition described above?
Much appreciated!
It's imperative you read the link posted by PEH as you can considerably shorten and speed up your code. I think this does what you want.
Sub Test()
Dim r As Long
r = WorksheetFunction.Max(Sheets("Summary").Range("D" & Rows.Count).End(xlUp).Row + 1, 3)
Sheets("Comm Payable").Range("C32:N32").Copy
Sheets("Summary").Range("D" & r).PasteSpecial Paste:=xlPasteValues
Sheets("Comm Payable").Range("C3:D3").Copy
Sheets("Summary").Range("B" & r).PasteSpecial Paste:=xlPasteValues
Sheets("Comm Payable").Range("N1").Copy
Sheets("Summary").Range("C" & r).PasteSpecial Paste:=xlPasteValues
Sheets("Comm Payable").Range("O1").ClearContents
End Sub
As an aside, transferring values directly is more efficient than copying and pasting and here is an example of that.
With Sheets("Comm Payable").Range("C32:N32")
Sheets("Summary").Range("D" & r).Resize(.Rows.Count, .Columns.Count).Value = Value
End With

Resources