I have data in Excel which is in table format, half of which (columns) contains data and the rest contains some formulas. I am writing some results on the data part, so I want to delete it before each update.
I tried to delete data using a VBA code, which contains similar piece of codes as below:
Range("A2:K2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
However, it deletes the whole table. I tried various version of this code, but did not work. Also, I realized that, when the data is not in table format the macro works correctly. However, I need it to be a table to update the formulas in a dynamic range.
Does this error is related to table format & any ideas how can I fix it?
You need to use the .DataBodyRange property of the ListObjects object (this is what tables are called in VBA). The code below will clear the non formula elements of a table that is on the active sheet.
Sub ClearTableButLeaveFormulas()
With ActiveSheet.ListObjects(1)
.DataBodyRange.SpecialCells(xlCellTypeConstants).ClearContents
End With
End Sub
Related
I am copying data from two spreadsheets and condensing the data into one table. Following this procedure, I want to sort the resulting data based on two criteria (Sort Column A ascending and then numerical values in descending order in column E).
When i use Call Sort I get a runtime error 1004 stating that "for this procedure, all cells need to have the same size"
However, when I assign the macro to a separate button and run it from there, it works. Below is the code i use for the sorting feature.
Sub Sort()
Range("A8:E100").Sort Key1:=Range("A8"), Order1:=xlAscending, Header:=xlNo, Key2:=Range("E8"), Order2:=xlDescending, Header:=xlNo
End Sub
When using several Sheets in Excel VBA, please make sure to refer to each one with the following:
Sheets("nameofyoursheet").Range("...") instead of just Range("...")
This is necessary because Excel doesn't know which sheet to look in.
I am attempting to efficiently copy columns of data from one worksheet to a second worksheet in Excel using VBA, starting with a defined name for the column.
I am unsure why my code doesn't work. I cannot emphasis enough, how little I know about coding. I am attempting to teach myself VBA in order to manipulate vast quantities of data in Excel.
Function SortDataC()
'cuts and pastes columns from the unsorted worksheet to the sorted worksheet
Worksheets("UnsortedData").Range("DeltaModScore").Copy Destination:=Worksheets("SortedData").Columns(1)
End Function
DeltaModScore is the column header. If I look in defined named it is present on the sheet UnsortedData with workbook scope. Thus, I assume I have screwed up the syntax somewhere?
I have used the term Sheets("UnsortedData").Range..... as well as Worksheets.... as you see above. I've been basically searching the web for code examples and trying to get them to work with my data. Inevitably, I end up with errors I have much difficulty fixing. I hope this is something simple someone can point out.
You only require the single top left cell of a destination to complete a Copy & Paste.
SUB SortDataC()
'COPIES and pastes columns from the unsorted worksheet to the sorted worksheet
Worksheets("UnsortedData").Range("DeltaModScore").Copy _
Destination:=Worksheets("SortedData").Cells(1, "A")
End SUB
Functions are intended to return a value. If you simply want to complete an operation, a sub procedure is more appropriate.
First off I want to apologize if my question has already been answered elsewhere as I did spend some time searching.
I'm using two macros in a sheet (very simple macros... they filter a table) and this sheet is a template that will be used every week. When I copy the sheet and make a new one, the macros no longer work. The error I receive is Runtime Error 9; Subscript out of range. I looked at the VBA code (which I've never really learned VBA) and see it is referencing a table title. Is there a way to fix this so it doesn't reference that specific table but rather the cells contained in that table? Example of the title name:
ActiveSheet.ListObjects("Table1619").Range.AutoFilter Field:=1
I want to access the data from a cell range of A103:A113. I tried this:
ActiveSheet.ListObjects.Range("$A$103:$A$113").AutoFilter Field:=1
This didn't work either but the error I received was different. "Run-time error '438': Object doesn't support this property or method"
What I know would work is if there was a way to have the same table name across all sheets but my limited research has seemed to point me in the direction of this not being possible.
It's impossible to reference ActiveSheet.ListObjects.Range("$A$103:$A$113") like this - either as above in your first sample, OR convert table to normal range and then use ActiveSheet.Range("$A$103:$A$113").
The problem you're facing is that you access the Range through a Table/ListObject - but the ListObject changes the name during the copy, as each Table must have a unique name. The solution is simple - instead of accessing the ListObject with its name, simply use the index in the worksheet - that will not change. Therefore, replace
ActiveSheet.ListObjects("Table1619").Range.AutoFilter Field:=1
with
ActiveSheet.ListObjects(1).Range.AutoFilter Field:=1
(assuming it is the only/first table in the worksheet.)
I'm working on a VBA script that pulls a range of dates from Access, then filters the data and creates a chart based on the filtered data. The filtered data will be going to a separate sheet where the chart will be pulling its data from . I can get data out of Access with a SQL statement, but my AutoFilter in Excel is erroring out. Here is what I have...
Sheet3.Range("F4:F500").AutoFilter(, "Riveter 01").Copy Destination:=Sheet2.Range("A5")
It gives an app-defined or object-defined error and I can't figure out why. Is this the proper way or is there an easier way?
PS: This filter will happen with 22 unique machines so I was planning on running a loop for each machine. If that is not the fastest or proper way please let me know.
You need to split this into two parts. Filter and then copy/ paste. See below:
With Sheet3
.AutoFilterMode = False
With .Range("F4:F500")
.AutoFilter Field:=1, Criteria1:="Riveter 01"
.SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("A5")
End With
End With
to remove the filter:
On Error Resume Next
Sheet3.ShowAllData
On Error GoTo 0
On Error Resume Next is for when there is no filter present to skip the error. Please note the use of Sheet3 and Sheet2 for those looking for a generic solution.
I think that you have to do this in 2 separate steps:
filter the data
copy the filtered data to another sheet
The answer here has an excellent example of how to do this: Autofilter Macro, then copy visible data ONLY and paste to next available row
I'm having a bit of a trouble (once again...) with Excel VBA.
I just want to copy Shapes (which are in fact "templates" shapes, with pre-defined layout) from a Worksheet to another Worksheets.
When I record a macro, this is the generated VBA code :
Sheets("Layout").Select
ActiveSheet.ChartObjects("CHART_TEMPLATE").Activate
ActiveChart.ChartArea.Copy
Sheets("Example").Select
Range("A1").Select
ActiveSheet.Paste
Of course, I want to get rid of all ".Select" methods, for performance issue. For this reason, I can't Select a range, neither use ActiveSheet.Paste
Unfortunately, the Paste method only exists for Worksheet objects (like ActiveSheet.Paste), so I had to use the PasteSpecial method of Range objects.
So here's my code :
ThisWorkbook.Sheets("Layout").ChartObjects("CHART_TEMPLATE").Copy
ThisWorkbook.Sheets("Example").Range("A1").PasteSpecial Paste:=xlPasteAll
But, the PasteSpecial method copies Shapes as pictures...
Of course I don't want pictures, because I have to populate those Chartswith data.
Does someone have a clue here ?
Thanx,
The Paste method can take a destination as an argument. Have you tried something like this?
ThisWorkbook.Sheets("Example").Paste Destination:=ThisWorkbook.Sheets("Example").Range("A1")