I am subtotaling data, copying that data to another worksheet, then filtering for a column and deleting those rows.
The problem is that the range of the data to delete will vary every time the macro is run.
Here's an example of what I have:
Range("A2").Select
Columns("A:C").Select
Selection.Subtotal GroupBy:=1, Function:=xlCount, TotalList:=Array(3), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
Range("A2:C2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.Range("$A$1:$C$396").AutoFilter Field:=2, Criteria1:="<>"
Rows("2:394").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
ActiveSheet.Range("$A$1:$C$42").AutoFilter Field:=2
Range("A22").Select
Selection.End(xlDown).Select
Rows("42:42").Select
Selection.Delete Shift:=xlUp
So, instead of this:
"$A$1:$C$396"
"$A$1:$C$42"
Find the last used row and last used column dynamically.
Dim sht As Worksheet
Dim LastRow As Long
Set sht = ActiveSheet
'Ctrl + Shift + End
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
And . . .
Dim sht As Worksheet
Dim LastColumn As Long
Set sht = ThisWorkbook.Worksheets("Sheet1")
'Using UsedRange
sht.UsedRange 'Refresh UsedRange
LastColumn = sht.UsedRange.Columns(sht.UsedRange.Columns.Count).Column
Related
I am getting run-time error for Vlook-up formula while using dynamically from activesheet to another sheet i.e Binarysheet, in Binary sheet, my lookup range is A to C column
I need to iterate the for loop and I have to use vlookup inside the for loop
Dim wfd As Worksheet
Set wfd = thisworkbook.Sheets("Binarysheet")
For i = 20 To 61
Cells(2, i).Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],wfd!$A:$B,2,False)"
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
ActiveCell.Offset(0, 1).Range("A1").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Cells(2, i+1).Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-3],wfd!$A:$C,3,0)"
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
ActiveCell.Offset(0, 1).Range("A1").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next
With the above comments and advice from the two linked questions, we can simplify the whole thing and load the whole without the need to loop:
Dim wfd As Worksheet
Set wfd = ThisWorkbook.Sheets("Binarysheet")
With ActiveSheet
Dim lastrow As Long
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
Dim rng As Range
Set rng = .Range(.Cells(2, 20), .Cells(lastrow, 61))
rng.FormulaR1C1 = "=VLOOKUP(RC1,'" & wfd.Name & "'!C1:C65,COLUMN(RC),False)"
rng.Value = rng.Value
End With
I have below code to copy and paste from different sheets in workbook. I don't want to show the user different sheets opening and show only the sheet in which the action triggered (Interface).
How can I do it, any guidance?
Sheets("List").Select
Range("N2").Select
lastrow = Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
Selection.AutoFill Destination:=Range("N2:N" & lastrow)
Range("O2").Select
Selection.AutoFill Destination:=Range("O2:O" & lastrow)
Range("O2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Team Sports Pricelist").Select
Range("N5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Interface").Select
This is what your code might look like if you don't select anything. (Code isn't tested).
Dim Ws As Worksheet
Dim Rng As Range
Set Ws = Worksheets("List")
Set Rng = Ws.Range(Ws.Cells(2, "N"), Ws.Cells(Ws.Rows.Count, "N").End(xlUp))
Rng.Cells(1).AutoFill Destination:=Rng
Set Rng = Rng.Offset(0, 1)
Rng.Cells(1).AutoFill Destination:=Rng
Rng.Copy
Worksheets("Team Sports Pricelist").Range("N5").PasteSpecial xlPasteValues
I've created a basic macro within a workbook to clear data from a set number of tabs then copy in refreshed data from external workbooks. There is a master data tab within the workbook that uses IF formulas to obtain various stock information for that tab which then feeds through to other sheets.
E.G.
=IF($A$2="","",SUMIF(Data_CoventryStock!$A:$A,Data!$A$2,Data_CoventryStock!$E:$E))
Currently when the macro runs it produces the desired result but the IF Formulas lose the reference to the range e.g. $A:$A becomes #N/A!
I've been looking online for a solution but am unable to see a suitable option. I am new to this area.
Sub Update()
'
' Update Macro
'
Application.DisplayAlerts = False
' Clears data from tabs
Sheets("Data_10Day").Select
Columns("A:B").Select
Selection.Delete Shift:=xlToLeft
Sheets("Data_CoventryStock").Select
Columns("A:E").Select
Selection.Delete Shift:=xlToLeft
Sheets("Data_CowleyStock").Select
Columns("A:E").Select
Selection.Delete Shift:=xlToLeft
Sheets("Data_RugbyStock").Select
Columns("A:B").Select
Selection.Delete Shift:=xlToLeft
Sheets("Data_10Day").Select
' Copies data from other workbooks then pastes
Workbooks.Open Filename:= _
"C:\Users\ceasdown\Documents\HDS\Data\Data_10Day.xlsx"
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows("Coventry Ordering Template2.xlsm").Activate
Sheets("Data_10Day").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks("Data_10Day.xlsx").Close
Workbooks.Open Filename:= _
"C:\Users\ceasdown\Documents\HDS\Data\Data_CoventryStock.xlsx"
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows("Coventry Ordering Template2.xlsm").Activate
Sheets("Data_CoventryStock").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks("Data_CoventryStock.xlsx").Close
Workbooks.Open Filename:= _
"C:\Users\ceasdown\Documents\HDS\Data\Data_CowleyStock.xlsx"
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows("Coventry Ordering Template2.xlsm").Activate
Sheets("Data_CowleyStock").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks("Data_CowleyStock.xlsx").Close
Workbooks.Open Filename:= _
"C:\Users\ceasdown\Documents\HDS\Data\Data_RugbyStock.xlsx"
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows("Coventry Ordering Template2.xlsm").Activate
Sheets("Data_RugbyStock").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks("Data_RugbyStock.xlsx").Close
Application.DisplayAlerts = True
End Sub
I need it to retain the cell range in the IF formula so no manual update after running the macro is required.
The reason your formulas get damaged is that you are Deleting the ranges they refer to. Instead of deleting, use ClearContents instead.
Also, your code can do with quite a bit of optimsation.
Consider this
Sub Update()
Dim wbMain As Workbook
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range
Dim FilePath As String
Application.DisplayAlerts = False
Set wbMain = ActiveWorkbook
With wbMain
FilePath = Environ$("UserProfile") & "\Documents\HDS\Data\"
' Copies data from other workbooks then pastes
UpdateFromWB .Worksheets("Data_10Day").Cells(1, 1), FilePath & "Data_10Day.xlsx", "WhatSheet?"
UpdateFromWB .Worksheets("Data_CoventryStock").Cells(1, 1), FilePath & "Data_CoventryStock.xlsx", "WhatSheet?"
UpdateFromWB .Worksheets("Data_CowleyStock").Cells(1, 1), FilePath & "Data_CowleyStock.xlsx", "WhatSheet?"
UpdateFromWB .Worksheets("Data_RugbyStock").Cells(1, 1), FilePath & "Data_RugbyStock.xlsx", "WhatSheet?"
End With
Application.DisplayAlerts = True
End Sub
Private Sub UpdateFromWB(rngDest As Range, wbName As String, wsName As String)
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range
Set wb = Workbooks.Open(Filename:=wbName)
Set ws = wb.Worksheets(wsName)
With ws
Set rng = .Range(.Cells(1, 1).End(xlDown), .Cells(1, 1).End(xlToRight))
'Alternative, in case there might be gaps in the data
'Set rng = .Range(.Cells(.Rows.Count, 1).End(xlUp), .Cells(1, .Columns.Count).End(xlToLeft))
End With
rngDest.Worksheet.Cells.ClearContents 'Delets ALL data from sheet. Adjust range if required
rngDest.Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
wb.Close
End Sub
I try to create a code avoiding .Select, .Activate and repetition. The code is untested but it will give you an idea about the concept. For any question please ask me.
Option Explicit
Sub Update()
Dim ws As Worksheet
'
' Update Macro
'
Application.DisplayAlerts = False
' Clears data from tabs
For Each ws In ThisWorkbook
With ws
If .Name = "Data_10Day" Or .Name = "Data_RugbyStock" Then
.Columns("A:B").Delete Shift:=xlToLeft
ElseIf .Name = "Data_CoventryStock" Or .Name = "Data_CowleyStock" Then
.Columns("A:E").Delete Shift:=xlToLeft
End If
End With
Next ws
' Copies data from other workbooks then pastes
Call Procedure("Data_10Day.xlsx", "Data_10Day")
Call Procedure("Data_CoventryStock.xlsx", "Data_CoventryStock")
Call Procedure("Data_CowleyStock.xlsx", "Data_CowleyStock")
Call Procedure("Data_RugbyStock.xlsx", "Data_RugbyStock.xlsx")
Application.DisplayAlerts = True
End Sub
Sub Procedure(ByVal FileName As String, ByVal SheetName As String)
Workbooks.Open FileName:="C:\Users\ceasdown\Documents\HDS\Data\" & FileName
Workbooks(FileName).Sheets("Sheet1").UsedRange.Copy
Workbooks("Coventry Ordering Template2.xlsm").Sheets(SheetName).Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Workbooks(FileName).Close
End Sub
I'm trying to create a Match macro that compares two lists and gives me the cells that present in only one of the lists. The cells are then copied to another sheet where the cells are counted. However, the blank cells are also being copied and I don't know why.
The below is what I have:
Sub Macro_do_Match()
Dim CopyrangeB As String
Dim lRowB As Integer
Dim fRowB As Integer
Dim CopyrangeD As String
Dim lRowD As Integer
Dim fRowD As Integer
Dim rng As Range
' Defines range for column B
lRowB = Cells(Rows.Count, 1).End(xlUp).Row
fRowB = 2
Let CopyrangeB = "B" & fRowB & ":" & "B" & lRowB
' "macro"
Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=IF(NOT(ISERROR(MATCH(C[-1],C[1],0)))=FALSE,C[-1], """")"
Range("B2").Select
Selection.AutoFill Destination:=Range(CopyrangeB)
' Defines range for column D
lRowD = Cells(Rows.Count, 3).End(xlUp).Row
fRowD = 2
Let CopyrangeD = "D" & fRowD & ":" & "D" & lRowD
' "macro"
Range("D2").Select
ActiveCell.FormulaR1C1 = _
"=IF(NOT(ISERROR(MATCH(C[-1],C[-3],0)))=FALSE,C[-1], """")"
Range("D2").Select
Selection.AutoFill Destination:=Range(CopyrangeD)
'Copy and paste B
Range("B2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Final Results").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Copy and paste D
Sheets("Insert Lists").Select
Range("D2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Final Results").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
I am having some issues with my VBA code that will select data from one sheet, copy it, paste it to a new sheet and insert a total at the bottom of the table. The first steps work, but I am struggling with the total , any help would be greatly appreciated. Here is what I have so far:
Range("A3").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "='Pricing Main'!RC[1]"
Range(xlToRight, xlDown).Offset(1, 0).Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=SUM(R[-36]C:R[-1]C)"
End Sub
If the problem is in making formula with dynamic range (while first row is always the same and the 2nd is different), you could use:
ActiveCell.FormulaR1C1 = "=SUM(R1C:R[-1]C)"
If you're using number without "[]" it will absolute address with $
In this example it will be ="SUM(A1:Ax)" where X is row before active cell
Another option is using Activecell.Formula and get this address by combining letters with Activecell.row
activecell.Formula = "=SUM(A1:A" & activecell.Row - 1 &")"
And after that you can autofill to other columns if needed by
activecell.AutoFill Destination:=range(activecell, activecell.Offset(0,5)), Type:=xlfilldefault
3rd option could be using range variables calculate address before formula:
Sub Makro1()
Dim rng1 As Range Dim rng2 As Range Dim rng3 As Range
Set rng1 = Range("A1") Set rng2 = ActiveCell.Offset(-1, 0) Set rng3 = Range(rng1, rng2)
ActiveCell.Formula = "=SUM(" & rng3.Address & ")"
End Sub
If you have another problem, please, try explain it better ;)