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
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'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 modified a piece of code that opens a .csv file, copies a selected range and pastes it in a selected cell in an excel worksheet.
I am having trouble selecting the next range in the .csv file to paste in the excel file. It only works for the first range (E2:E25).
I want it to select the next range (B2:B25) from the .csv file, copy/paste, but it only selects from the excel file. How do I fix that? Thanks.
Option Explicit
Sub copy2()
Dim FilesToOpen
Dim wkbAll As Workbook
Dim wkbTemp As Workbook
Dim newSheet As Worksheet
FilesToOpen = Application.GetOpenFilename(Title:="Text Files to Open")
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen, Format:=4)
wkbTemp.Sheets(1).Cells.copy
Range("E2:E25").Select
Selection.copy
Windows("Petty Cash Form (test).xls").Activate
Range("H10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B2:B25").Select
Selection.copy
Windows("Petty Cash Form (test).xls").Activate
Range("B10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
wkbTemp.Close
End Sub
In your VBA macro add the line: wkbTemp.Sheets(1).Activate (see 'Activate Source Worksheet):
Option Explicit
Sub copy2()
Dim FilesToOpen
Dim wkbAll As Workbook
Dim wkbTemp As Workbook
Dim newSheet As Worksheet
FilesToOpen = Application.GetOpenFilename(Title:="Text Files to Open")
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen, Format:=4)
wkbTemp.Sheets(1).Cells.copy
Range("E2:E25").Select
Selection.copy
Windows("Petty Cash Form (test).xls").Activate
Range("H10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Activate Source Worksheet
wkbTemp.Sheets(1).Activate
Range("B2:B25").Select
Selection.copy
Windows("Petty Cash Form (test).xls").Activate
Range("B10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
wkbTemp.Close
End Sub
Hope this may help.
This is somewhat of a rudimentary answer but I would suggest following the VBA hierarchy in order to select specific ranges.
workbook.worksheet.range
I think if you defined the worksheets then used
wrktemp.Sheets("sheet name").Range("E2:E25")
I believe this will help in pursuit of your problem
I have a input workbook, from which I will copy first row and paste it in another excel workbook (wbET). This I have to do for the number of rows in my input workbook.
I have code for first row. I have to do it for all the rows. can any one help me out
code:
Option Explicit
Dim wbIP As Workbook
Dim wbJT As Workbook
Dim wbET As Workbook
Dim mypathET As String
Dim mypathJT As String
Dim mypathIP As String
Dim vals As Variant
Sub tool()
mypathET = "C:\Documents and Settings\madinenih\Desktop\PremiumCalcutionTool"
mypathJT = "C:\Documents and Settings\madinenih\Desktop\Japancalculationtool"
mypathIP = "C:\Documents and Settings\madinenih\Desktop\A01"
'
'Set wbJT = Workbooks.Open(Filename:=mypathJT)
Set wbIP = Workbooks.Open(Filename:=mypathIP)
wbIP.Activate
'Rows("1:1").Select
'Selection.Copy
wbIP.Sheets("A01").Range("A1:IU1").Copy
Set wbET = Workbooks.Open(Filename:=mypathET)
wbET.Activate
wbET.Sheets("Input file data").Range("A3:IU3").PasteSpecial
'wbET.Activate
Application.Run (wbET.Name & "!run1")
Call Createexcels
wbIP.Activate
'Rows("1:1").Select
'Selection.Copy
wbIP.Sheets("A01").Range("A1:IU1").Copy
Set wbJT = Workbooks.Open(Filename:=mypathJT)
wbJT.Activate
wbJT.Sheets(2).Range("A5:IU5").PasteSpecial
'Application.Run (wbJT.Name & "!run1")
Call openexcel
Call compare
End Sub
Sub Createexcels()
Dim NewBook As Workbook
vals = "test"
Set NewBook = Workbooks.Add
NewBook.SaveAs Filename:=vals
'Workbooks("Whatever.xlsx").Worksheets("output").Range("A1:K10").Copy
'NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial (xlPasteValues)
' NewBook.Worksheets("Sheet1").Activate
wbET.Activate
wbET.Sheets("Calculation").Range("L2:L41").Copy
NewBook.Worksheets("Sheet1").Activate
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
wbET.Activate
wbET.Sheets("Calculation").Range("L44:L61").Select
Application.CutCopyMode = False
Selection.Copy
Windows(vals).Activate
Range("A44").Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
wbET.Activate
wbET.Sheets("Calculation").Range("L64:L69").Select
Application.CutCopyMode = False
Selection.Copy
Windows(vals).Activate
Range("A63").Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
wbET.Activate
wbET.Sheets("Calculation").Range("L72:L81").Select
Application.CutCopyMode = False
Selection.Copy
Windows(vals).Activate
Range("A70").Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("A:A").EntireColumn.AutoFit
NewBook.Save
End Sub
You need to loop through all of the rows in your Input worksheet. To start you need to get the last used row in our input sheet.
' use this in your loop. It looks like you are starting on row 3 of your input sheet.
Dim LastRow as Long
LastRow = Activesheet.Cells(Activesheet.Rows.Count, 2).End(xlUp).Row
Dim i as Long
For i = 3 to LastRow
' Code to copy each row goes here
' You will need to change how you are referencing your range
wbET.Sheets("Input file data").Range("A" & i & ":IU" & i).PasteSpecial
Next i
you need to find the last row and last column
using last row and column as reference, you can make the copy paste method easily
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.