Excel 2010 Clear contents from a table - vba

I have an Excel sheet that has a table in range A4:BD4 (this is the first row of the data. I want to clear the data but leave the table intact. I do not know how many rows are of data are in the table, it will vary.
The table itself has a few more columns but I do not want to remove the data from them just from A thru BD. BE for instance needs to hold the data.
Here is code that removes all the data from all the columns:
For Each mySheet In wb.Sheets
For Each myTable In mySheet.ListObjects
myTable.DataBodyRange.ClearContents
Next myTable
Next mySheet
Thanks for any help with this.

This should work; you can use offset and resize together to skip over whichever areas you want to leave intact:
Sub ResetTables()
Dim wb As Workbook
Dim mySheet As Worksheet
Dim tbl As ListObject
Set wb = ThisWorkbook
For Each mySheet In wb.Sheets
For Each tbl In mySheet.ListObjects
With tbl.DataBodyRange.Offset(0, 0)
.Resize(.Rows.Count, .Columns.Count - 1).Columns.ClearContents
End With
Next tbl
Next mySheet
End Sub
I think you may find this resource helpful if you want additional help:
http://www.thespreadsheetguru.com/blog/2014/6/20/the-vba-guide-to-listobject-excel-tables

Related

Remove rows from selectrange VBA

Im trying to write a VBA to copy and paste alot of different data, there is a lot of non-important data in each sheet so i want to copy only the important part.
The VBA i have now looks like this:
Sub DynamicRange()
'Best used when your data does not have any entirely blank rows or columns
Dim sht As Worksheet
Dim StartCell As Range
Set sht = Worksheets("Kvalitetskriterier 2015")
Set StartCell = Range("A8")
'Select Range
StartCell.CurrentRegion.Select
End Sub
What I need is a way to, from the seleced range, remove the first row and the last 4 rows as i dont need that part. Is there a way to do that from my VBA?
There's no need to Select the range if you want to copy it, you can directly Copy it.
if you want to get rid of the first row of the Range, you can use Range.Ofset(1, 0), this will remove the first row (Header).
If you also want to get rid of rows at the end of your Range, you can use Range.Resize(- number of rows you want to remove).
Code
Sub DynamicRange()
'Best used when your data does not have any entirely blank rows or columns
Dim sht As Worksheet
Dim StartCell As Range
Set sht = Worksheets("Kvalitetskriterier 2015")
Set StartCell = sht.Range("A8")
'Select current region
Set StartCell = StartCell.CurrentRegion
' set offeset 1 row (remove header), and use Resize to remove the last row
Set StartCell = StartCell.Offset(1, 0).Resize(StartCell.rows.Count - 2)
StartCell.Copy ' <-- No need to Select it if you want to copy
End Sub

Copying dynamic range of cells from inactive workbook to pasting to active workbook at pointed places

I am fairly new to vba and with the help of stackoverflow guidance, i am learning.
I am trying to copy four different ranges of cells (dynamic in nature, but all have same row numbers, but different columns) (the source book shall be open and the user to select the range in particular order) so that the same can be pasted to target workbook in the same order.
I could not attach the sample source workbook (the name in future will change) and target workbooks as i am newbie here.
source book contains various col like, slno, acct no, name, amt, ifsc, remarks, arrears etc and the no of rows may be changing.
target book contains, only four colums, like, amt,acct no, name and ifsc in the same order.
From the source book, i need, acct, ifsc, name and amt col data to be pasted in target workbook, viz, amt, acct no, name and ifsc.
i tried the following code using this forum...
but, it copies only col a to g and not dynamic. I wish, it could ask some user input for all four ranges.
thanks..
Private Sub CommandButton1_Click()
Dim excel As excel.Application
Dim wb As excel.Workbook
Dim sht As excel.Worksheet
Dim f As Object
Set f = Application.FileDialog(3)
f.AllowMultiSelect = False
f.Show
Set excel = CreateObject("excel.Application")
Set wb = excel.Workbooks.Open(f.SelectedItems(1))
Set sht = wb.Worksheets("Sheet1")
sht.Activate
sht.Columns("A:G").Copy
Range("A1").PasteSpecial Paste:=xlPasteValues
wb.Close
End Sub
If your code is working you can delete the unwanted columns by adding this code between Range("A1").PasteSpecial Paste:=xlPasteValues and wb.close.
Dim cel As Range
With ThisWorkbook.Sheets(1)
For Each cel In Intersect(.UsedRange, .[1:1])
If cel.Value2 = "" Or InStr(1, "amtacct nonameifsc", cel.Value2) = 0 Then
cel.EntireColumn.Delete
End If
Next
End With

Copy tables excel vba

I am using the code below to copy tables from another workbook into my "extract" workbook.
This code works, but I need to specify which table I am looking for. I was hoping to use a program that could find the table in the specified sheet, or all the cells which contain data and copy those cells.
Sub SelectingTable()
Set Extract = Workbooks("Test1")
Set Pastdue = Workbooks("Past Due Data")
'Look for Past Due table
Pastdue.Activate
Pastdue.Worksheets("Sheet1").ListObjects("Table4").DataBodyRange.Copy
'Paste table in extract
Extract.Activate
Extract.Paste Destination:=Worksheets("Sheet1").Range("B10")
End Sub
In this code I am basically looking for table4 in the Past Due workbook and pasting it in my Extract. I am new to excel vba so I hope you could help me. Thanks.
Hmm do you want to extract a particular table or copy all the tables? For the latter you can just loop through all the tables
Sub SelectingTable()
Dim tbl as listObject
Set Extract = Workbooks("Test1")
Set Pastdue = Workbooks("Past Due Data")
'Loop through table
pasteRow = 1 'set which row to paste
For each tbl in Pastdue.Worksheets("Sheet1").ListObjects
rowsCount = tbl.range.rows.count - 1 'minus header
tbl.DataBodyRange.Copy extract.worksheets("Sheet1").cells(pasteRow,1)
pasteRow = rowsCount +3
next tbl
End Sub

Getting data validation list to populate a table

I have a Data Validation list in cell T3 in Sheet3 of my workbook. The list contains location names. In sheet1 of my workbook I have all the data for all locations in tables next to each other, e.g
location 1|date|score|percentage|target| |location 2|date|score|percentage|target| etc....
I am looking to select a location from the drop down list and that will copy in the relevant table to sheet3. So you just select a location and can see the data. I'm wondering if the best way to go about this is formulas or to use VBA (my experience with using drop down lists in VBA is limited). Here is something that I am currently working on but it is incomplete at the moment and still leads to the question of 'is there a faster way to do this in VBA'. Any help or advice is greatly appreciated! (my validation list is called List1)
=IF(ISNUMBER(A2),IF(ISERROR(VLOOKUP(A2,Sheet1!$A:$EG,MATCH(List1,Sheet1!$1:$1,0),FALSE)),0),"")
The idea is that i could have a table of formulas so depending on the list value, different data would appear.
This isn't complete but it should get you started / give you some ideas
Sub CopyTable()
Dim colNo, lastRow As Integer
Dim ws1, ws3 As Worksheet
Set ws1 = Sheets("Sheet1") ' assign the worksheet variables
Set ws3 = Sheets("Sheet3")
colNo = Application.Match(ws1.Range("T1"), ws1.Range("A1:R1")) ' work out which column the required table starts in
lastRow = ws1.Cells(10000, colNo).End(xlUp).Row ' work out the last row of the table
ws1.Range(ws1.Cells(1, colNo), ws1.Cells(lastRow, colNo + 4)).Copy ws3.Range("A1") ' copy the table
End Sub

Create an Excel worksheet based on specific column values

I am enclosing a link with a sample spreadsheet.
What I would like to do is create multiple worksheets using the key column of "Facility", perhaps using a macro. For example, I would like to create a new worksheet called Houston and fill the worksheet with the data specific to that row. Some of the cells may end up in different locations in the new worksheet. I need to do a separate worksheet for every value in "Facility". The original that I am working on has 270 rows (270 facilities).
Does anyone have any idea how to do something like this? I am new to running and creating macros. I did create a macro that didn't work right.
Try this:
Dim wks As Worksheet
Dim lstRow As ListRow
For Each lstRow In ThisWorkbook.Worksheets("Sheet1").ListObjects("Table1").ListRows
Set wks = ThisWorkbook.Worksheets.Add
wks.Name = lstRow.Range.Cells(, 1).Value
With wks
.Range(.Cells(1, 1), .Cells(1, lstRow.Range.Columns.Count)) = lstRow.Range.Value
End With
Next
Set wks = Nothing
Assumptions:
Worksheet name where data resides is called Sheet1
Table containing data is a ListObject (e.g., convert your data range into an Excel table)
ListObject name is Table1