I have a VBA object in Excel 2003 that triggers three simple macros when certain values are attained via streaming data. It runs nicely. I would like to open a duplicate worksheet, but with different streaming data, and have the macros trigger on their respective sheets. It works now, but only on the worksheet I currently have selected. Each worksheet has the object and macros in it.
The object monitors three cells, and when the first cell is triggered, goes on to monitor the next cell, and when triggered monitors the next cell, then repeats.
I would some help having them both run on their respective spreadsheets at the same time.
Here is the VBA object:
Private Sub Worksheet_Calculate()
Static oldval1
Static oldval2
Static oldval3
Static LastAction As Integer
' Initial state will be 0, neither Fast nor Slow
Const Fast As Integer = 1
Const Fast2 As Integer = 2
Const Slow As Integer = 3
Application.EnableEvents = False
If Range("I1").Value = "1" And oldval1 <> "1" And LastAction <> Fast Then
PasteFast
LastAction = Fast
ElseIf Range("Q1").Value = "1" And oldval2 <> "1" And LastAction <> Slow Then
PasteFast2
LastAction = Fast2
ElseIf Range("Y1").Value = "1" And oldval3 <> "1" And LastAction <> Slow Then
PasteSlow
LastAction = Slow
End If
oldval1 = Range("I1").Value
oldval2 = Range("Q1").Value
oldval3 = Range("Y1").Value
Application.EnableEvents = True
End Sub
And, here are the three macros – they are essentially the same – they copy from the same location, but paste into different locations. PasteSlow and PasteFast are on one module, and PasteFast2 is on a second module (for no reason).
Sub PasteSlow()
'
' PasteSlow Macro
'
'
Application.ScreenUpdating = False
Range("G5:G57").Select
Selection.Copy
Range("H5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("K5:K57").Select
Application.CutCopyMode = False
Selection.Copy
Range("L5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
End Sub
Sub PasteFast()
'
' PasteFast Macro
'
'
Application.ScreenUpdating = False
Range("g5:g57").Select
Selection.Copy
Range("P5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("k5:k57").Select
Application.CutCopyMode = False
Selection.Copy
Range("T5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
End Sub
Sub PasteFast2()
'
' PasteFast2 Macro
'
'
Application.ScreenUpdating = False
Range("g5:g57").Select
Selection.Copy
Range("x5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("k5:k57").Select
Application.CutCopyMode = False
Selection.Copy
Range("ab5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
End Sub
I super-apologize for any code formatting errors.
The reason that your code isn't working lies in the way that you use the Range function. Using Range alone is short for Application.Range.
The Excel help files say this about it:
When used without an object qualifier, this property is a shortcut for ActiveSheet.Range (it returns a range from the active sheet; if the active sheet isn’t a worksheet, the property fails).
Keeping this in the Worksheet_Calculate event locks it to the given worksheet.
I would suggest moving the code that's in your Worksheet_Calculate into a module in a separate function with the cells of interest as parameters. This would allow you DRY up your code, and allow you to place a call to the function in the Worksheet_Activate event.
If you don't want to rewrite the code, just throw a call to Worksheet_Calculate into the Worksheet_Activate event:
Private Sub Worksheet_Activate()
Worksheet_Calculate
End Sub
This will start your code upon entering the worksheet.
Related
A few times a day I receive a file. I'm trying to automate it as much as possible and one part would include having the macro that lets you select a file to vlookup into (the file name is different every time). My macro runs, but for some reason it prompts you to select your file 3 times. I've tried a few variations on the code, but nothing worked. Does anyone have any insight as to why? It is prompting once when first opening the file, once when filling in the first cell with the formula, and again when the macro fills down column with the vlookup formula. I've pasted the relevant part below:
Dim MyFile As String
MyFile = Application.GetOpenFilename
Set firstWB = ActiveWorkbook
Set mySheet = ActiveSheet
Set wbLookup = Workbooks.Open(MyFile)
firstWB.Activate
mySheet.Range("T2").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-18],'[wbLookup]tempemail'!R2C2:R123C20,19,0)"
Range("S1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Columns("t:t").EntireColumn.AutoFit
Columns("T:T").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
wbLookup.Close False
Range("U1").Select
ActiveCell.FormulaR1C1 = "=NOW()"
Range("U1").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.Paste
Application.CutCopyMode = False
Columns("u:u").EntireColumn.AutoFit
End Sub
Thanks!
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-18],'[wbLookup]tempemail'!R2C2:R123C20,19,0)"
This will not work unless wbLookup is literally the name of your file. Excel sees this and prompts you for the actual name.
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-18],'[" & wbLookup.Name & "]tempemail'!R2C2:R123C20,19,0)"
might work better
This:
Columns("T:T").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
...could be replaced by this:
Columns("T:T").Value = Columns("T:T").Value
A lot of selecting/activating is unneeded and is better avoided: How to avoid using Select in Excel VBA
I make loop code based only on macros, is there any other way that can be used? or at least a simple form of code I created.
Sub xx()
Dim nom As Long
Dim bck As Workbook
Dim I As Long
Windows("LP13.xlsm").Activate
Application.CutCopyMode = False
Sheets("Validasi").Range("T2:T10").Copy
Windows("backup.xlsx").Activate
Sheets("backup").Range("F1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("LP13.xlsm").Activate
Application.CutCopyMode = False
Sheets("Validasi").Range("V2:X11").Copy
Windows("backup.xlsx").Activate
Sheets("backup").Range("G1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("LP13.xlsm").Activate
Application.CutCopyMode = False
For I = 1 To nom
Sheets("Data").Range("A2:W" & I).Select
Next
Selection.Copy
Windows("backup.xlsx").Activate
Sheets("backup").Range("J1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
bck.Save
Application.Visible = False
bck.Close True
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
it does look difficult because there are too many repetitions. I want a simpler code in the backup to another workbook.
As for strategy part of your question. you can create file versions backup on save as in http://analystcave.com/excel-quick-versioning-excel-files/
As for macro part
1. I would try to simplyfie the code with variables. You don't need to activate any thing to copy data, this is just way macro recorder works.
The backup part is asking for extracting function to paste range.
I would name the ranges in "from" workbook so you don't hardcode rng.address in code.
Your macro is not complex so naming range will solve the issue and will give you "documentation" what you coping and where.
If needed you can create sheet with list of source / destination ranges to copy and than pass it to "backup manager"
My take to refactor you code
Option Explicit
Private Type LP13Backup
FileName As String ' ?path
Sht As String
Rng As String
End Type
Public Sub LP13_BuckupManager() 'yes I know ..Manager ;)
Dim From As LP13Backup
Dim Backup As LP13Backup
From.FileName = "LP13.xlsm"
From.Sht = "Validasi"
From.Rng = "A1:B1"
With From
Workbooks(.FileName).Worksheets(.Sht).Range(.Rng).Copy
End With
Backup.FileName = "backup.xlsx"
Backup.Sht = "Backup"
Backup.Rng = "F1"
CopyToBackup Backup
End Sub
Sub CopyToBackup(ByRef Backup As LP13Backup)
With Backup
Workbooks(.FileName).Worksheets(.Sht).Range(.Rng).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End With
End Sub
Word of comment on your solution
For I = 1 To nom
Sheets("Data").Range("A2:W" & I).Select
Next
This is not efecive way to select all values in column. You can go down from first cell
Range(Range("F1"), Range("F1").End(xlDown)).Address
or go up as in https://stackoverflow.com/a/27066381/7385538
With ws
lastRowIndex = .Cells(.Rows.Count, "A").End(xlUp).row
End With
Im trying to run macro which should clear contents from sheets AA, BB and CC and then move into sheet MENU but its highliting me an error on line 4. Please see the code below.
Sub clear_sheets()
Snames = Split(AA, BB, CC)
For Count = 0 To UBound(Snames)
Sheets(Snames(Count)).Range("A3:C3").End(xlDown).ClearContents
Next
Sheets("MENU").Select
Optimise (False)
End Sub
I got a little bit different approach posted on a different forum. Which would run more efficiently, I mean which one will will be putting less stress on a processing?
Sub clear_sheets()
Optimise (True)
Snames = Split("AA, BB, CC", ", ")
For count = 0 To UBound(Snames)
MyRange = Range("A3:C3", Range("A3:C3").End(xlDown)).Address
ThisWorkbook.Sheets(Snames(count)).Range(MyRange).ClearContents
Next
Sheets("MENU").Select
Optimise (False)
End Sub
Update
I understand this is not a code writing website but I would like to ask you if you could have a look at my code below.
Sub distribute_dsp_data_9()
Sheets("raw_data_1_9").Visible = True
Sheets("raw_data_1_9").Select
ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2, Criteria1 _
:="DTTD"
Range("C2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.Copy
Sheets("DTTD").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("raw_data_1_9").Select
ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2, Criteria1 _
:="FDTL"
Range("C2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.Copy
Sheets("FDTL").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("raw_data_1_9").Select
ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2, Criteria1 _
:="FULL"
Range("C2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.Copy
Sheets("FUL ON").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("raw_data_1_9").Select
ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2
Sheets("raw_data_1_9").Visible = False
End Sub
Try this. I've declared your variables properly and also assigned values to your array using Array() rather than Split() both are fine, but Array() is more flexible
Sub clear_sheets()
Dim sheetNames As Variant
Dim count As Integer
sheetNames = Array("AA", "BB", "CC")
For Count = 0 To UBound(Snames)
With Sheets(sheetNames(Count))
Range(.Range("A3"), .Range("C3").End(xlDown)).ClearContents
End With
Next
Sheets("MENU").Select
Optimise (False)
End Sub
Also, it's better to use ThisWorkbook.Sheets()rather than just Sheets() if your code refers to the same workbook you are writing your VBA in. If you don't do this then VBA will assume you are referring to the sheets in whichever workbook is active when you run the code - that's not usually a good thing.
Update
I've changed the code to delete what I think you might want deleted?
Use:
Snames = Split("AA", "BB", "CC")
I'm attempting to write a macro that will copy a range of cells from a sheet, paste them into a sheet ("Bulksheet") that will contain all pasted data, then move on to the next tab after the first sheet. This needs to be done for 40+ tabs. Luckily, the data is in the same place in each tab, including the Bulksheet tab.
I can easily get this to apply to one tab, but returning to the first active tab and then moving on to the next is giving me no end of trouble.
Ex. code (shortened to the crucial bit). At the bottom where Next is would be where I need to move to the next sheet and do the same function, returning to "Bulksheet" and pasting in the next empty cell in column C.:
Sub
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Activate
Range("C100:F103").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Bulksheet").Select
Range("D1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next
End Sub
Try looping through the sheets using an index value instead.
Sub
Dim i as integer
For i = 1 to worksheets.count
sheets(i).Activate
if activesheet.name <> "Bulksheet" then
Range("C100:F103").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Bulksheet").Select
Range("D1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
end if
Next
End Sub
Try this:
Sub CopyToBulksheet()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Bulksheet" Then
ws.Activate
Range("C1:F10").Copy
Sheets("Bulksheet").Select
Range("D" & Cells.Rows.Count).End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Next
End Sub
I want to copy a defined number (lets say 10) of rows from one sheet ("Data") and paste it in another sheet ("Input). This will cause a bunch of stuff to calculate. Then I want to copy said calculated data (6 rows) from ("Input") to ("Data") and paste in a results table. THen I would repeat this a defined number of times for a certain number of columns (lets say 10).
I tried writing the code but it has literally been years since I have written code.
I used the Record Marco thing and got this:
Sub Macro2()
'
' Macro2 Macro
'
'
Range("C5:C14").Select
Selection.Copy
Sheets("Input").Select
Range("C5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("P12:P19").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("C22").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("D5:D14").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Input").Select
Range("C5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("P12:P19").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("D22").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("G16").Select
End Sub
I hope this makes sense
Sub Macro2()
Const NUM_TIMES As Long = 10
Dim shtInput As Worksheet, shtData As Worksheet
Dim rngCopy As Range, i As Long
Set shtInput = Sheets("Input")
Set shtData = Sheets("Data")
Set rngCopy = shtData.Range("C5:C15")
For i = 1 To NUM_TIMES
With shtInput
.Range("C5").Resize(rngCopy.Rows.Count, 1).Value = rngCopy.Value
.Calculate
rngCopy(1).Offset(17, 0).Resize(8, 1).Value = .Range("P12:P19").Value
End With
Set rngCopy = rngCopy.Offset(0, 1)
Next i
End Sub