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")
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
I apologise for what might be a messy code and/or simple question.
I have searched this site and web and tried various code pieces but my understanding and patience is too limited for the current task. I appreciate your more knowledgeable experience. Now the question..:
I would like to loop a piece of code so that it can be implemented for different ranges. I start with two sheets of data, the second of which contains the refined data with around 66 columns, the first two columns of which will be used for each new sheet. The code first filters the third column and copies the first two and third column, creates a new sheet and pastes the values. Then it returns to Sheet2 to remove the filter and do the same actions for the fourth column.
Since each iteration has repetition e.g. 3, 4, 5... I would like to create a variable that can be used to loop code and make it a lot neater as well as simple to limit the number of loops to the number of columns - 2 (the first two columns). So instead of me writing this code 64 times and changing it for another workbook with 100 columns, I would like to change just a few variables and ranges, if that is a possibility.
Sub CopyPaste()
Dim rg As Range
Set rg = ActiveSheet.Range("$A$1:$BN$5279")
rg.AutoFilter Field:=3, Criteria1:="<>"
Union(Columns(1), Columns(2), Columns(3)).Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
rg.AutoFilter Field:=3
rg.AutoFilter Field:=4, Criteria1:="<>"
Union(Columns(1), Columns(2), Columns(4)).Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
rg.AutoFilter Field:=4
rg.AutoFilter Field:=5, Criteria1:="<>"
Union(Columns(1), Columns(2), Columns(5)).Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
rg.AutoFilter Field:=5
End Sub
Thank you,
Ricky
Something like this should work for you. You can adjust the for loop as needed to loop through all the columns needed. This avoids the selection pieces, resets the filters when done with each iteration, and is easy to adjust for any changes in numbers of columns. It will also programmatically find the last row instead of the sheet range instead of hard coding it in.
Sub CopyPaste()
Dim rg As Range
Dim LastRow As Long
LastRow = Sheets("Sheet2").Range("A1").SpecialCells(xlCellTypeLastCell).Row
Set rg = Sheets("Sheet2").Range("A1:BN" & LastRow)
For i = 3 To 5
rg.AutoFilter Field:=i, Criteria1:="<>"
Union(Columns(1), Columns(2), Columns(i)).Copy
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Cells(1, 1).PasteSpecial Paste:=xlPasteValues
rg.AutoFilter
Next i
End Sub
There might be a better way to approach this but just to put it in a loop, try the below code:
Sub CopyPaste()
Dim rg As Range: Set rg = ActiveSheet.Range("$A$1:$BN$5279")
For iC = 3 To 64
rg.AutoFilter Field:=iC, Criteria1:="<>"
Union(Columns(1), Columns(2), Columns(3)).Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
rg.AutoFilter Field:=iC
Next
End Sub
I am new to Excel VBA and I am having trouble finding out how to create a macro that copies data from each sheet in a workbook and pastes the values into a summary sheet in the same workbook, appending the data below for each successive sheet.
I think my main problem is that the data to be copied does not start in A1. There are loads of answers where data does start in the first column but I can’t adapt it for data that doesn’t.
The data is in the same location and is the same size in each sheet, so I guess I can Dim a range for each and that I can probably manage.
I need to roll it out to multiple workbooks that have different numbers of sheets in each. Each sheet in each workbook is named in a generic sheet1, sheet2 etc way throughout.
I do have other sheets in the data that I won’t want copied but I have a piece of code that works by exception so as long as it loops through all sheets named generically that shouldn’t cause too much of a problem.
I’m really sorry if this has already been asked. I’ve been trying to search for a solution for weeks and have luckily learnt quite a few other useful bits along the way but I still can’t find a solution.
At the moment I am using this as a basis but obviously it’s very manual and I just can’t work out how to make it adaptable and not so clunky.
I will ultimately put in a loop but it’s just the basics of how to address the data that I am having the biggest problem with.
Thanks for reading!
Sheets("Sheet1").Select
Range("AD9").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("MASTER_QI_SUMMARY").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AD9").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("MASTER_QI_SUMMARY").Select
Range("A288").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet3").Select
Range("AD9").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("MASTER_QI_SUMMARY").Select
Range("A574").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet4").Select
Range("AD9").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("MASTER_QI_SUMMARY").Select
Range("A860").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
I think this is what you're after.
Dim wsX As Worksheet, wsS As Worksheet
Dim strSheetsToExclude As String, strArr() As String
Set wsS = Worksheets("MASTER_QI_SUMMARY")
strSheetsToExclude = "Sheet4,Sheet5"
strArr = Split(strSheetsToExclude, ",")
For Each wsX In ActiveWorkbook.Worksheets
If Not wsX Is wsS _
And UBound(Filter(strArr, wsX.Name)) = -1 Then
wsX.Range("AD9").CurrentRegion.Copy
If IsEmpty(wsS.Range("A2")) Then
wsS.Range("A2").PasteSpecial xlPasteValues
Else
wsS.Range("A" & wsS.Range("A2").End(xlDown).Row + 1).PasteSpecial xlPasteValues
End If
End If
Next
Just add all sheets to be excluded to the comma separated string and may be change the range for pasting.
Here's something that might help you : you can concatenate strings.
Meaning Range("A" & 2+i*286)is a valid range for VBA. As well, Sheets("Sheet" & i)is a well-defined sheet.
If you loop on i, it should do what you want.
I would also advise you to search for posts adressing the use of Select and Copy if you feel the execution of the macro is too slow.
I'm trying to rewrite someone's program, but I'm not familiar with VBA. I've tried many different ranges with .End(x1Up) and (x1Down). I understand the Up is the better option, but it just keeps pasting it in the same row every time and doesn't drop down to the next one. Here's my code (many tries have been commented out):
Sub Save_History()
Sheets("Simple Calculation").Select
Range("A10:J10").Select
Selection.Copy
'Sheets("Media Data History").Select
'Range("A65536").End(xlup).Offset(1,0)
'If Range("A1") <> "" Then
'Range("A1").End(xlUp).Offset(1, 0).Select
'End If
' Range("A1").End(xlUp).Select
'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
This should work but I encourage you to take a look at THIS Post
Sub Save_History()
Sheets("Simple Calculation").Select
Range("A10:J10").Select
Selection.Copy
Sheets("Media Data History").Select
Range("A65536").End(xlup).Offset(1,0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
Got it!!
Sub Save_History()
Sheets("Simple Calculation").Select
Range("A10:J10").Select
Selection.Copy
Sheets("Media Data History").Select
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub