I have a workbook with 84 worksheets all with different amounts of rows of data.
I need to convert all of the worksheets data to tables.
I found this macro online which I thought would work if I had all of the sheets selected but it doesn't.
Sub A_SelectAllMakeTable2()
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes)
tbl.TableStyle = "TableStyleMedium15"
End Sub
Is there a way to modify this so it will affect the entire workbook?
You could use something like this:
Sub A_SelectAllMakeTable2()
Dim ws as worksheet
for each ws in activeworkbook.worksheets
ws.ListObjects.Add(xlSrcRange, ws.range("A1").Currentregion, , xlYes).TableStyle = "TableStyleMedium15"
next ws
End Sub
Related
I have a team that works remotely on a shared spreadsheet. They can apply filters for search purposes. I would like the spreadsheet to be able to have the filters previously applied cleared automatically upon closing OR opening the spreadsheet without deleting the ability to set future filters. I cannot figure out the code to make this work.
I've searched these threads and tried many codes. Some have come close and have removed the filters when opening the spreadsheet, but it also removes the ABILITY to filter. This means that I have to turn on filtering each time I re-open the spreadsheet and that is not ideal. Code used is:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In Worksheets
If ws.AutoFilterMode Then
ws.AutoFilterMode = False
End If
Next ws
End Sub
This will clear, but keep the filter:
Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In Worksheets
If ws.AutoFilterMode Then
ws.AutoFilter.ShowAllData
End If
Next ws
End Sub
Another approach could be the following:
Sub clearFilter()
Dim sht As Worksheet 'Declare a worksheet variable
Dim rng As Range 'Declare a Range variable
Dim j As Long
Set sht = ThisWorkbook.Worksheets("Name of your Worksheet") 'The worksheet where the data is
Set rng = sht.Range("A:E") 'The range that is being filtered. In this case columns A,B,C,D,E are being filtered.
For j = 1 To rng.Columns.Count Step 1 'loop through all the columns that are being filtered...
rng.AutoFilter Field:=j '...and clear the filter while maintaining the filtering capabilities
Next j
End Sub
I wanna set visibility and invisibility for two shapes at the same time by VBA.
I wrote this code:
Sub Set_Visible_Invisible()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.ActiveSheet.Shapes("btn_S2_Pasive").Visible = False
ws.ActiveSheet.Shapes("btn_S2_Active").Visible = True
Next ws
End Sub
but it's work for only active sheet, not all of the sheets that the workbook has.
Any idea is welcome.
Perhaps:
Sub Set_Visible_Invisible()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Shapes("btn_S2_Pasive").Visible = False
ws.Shapes("btn_S2_Active").Visible = True
Next ws
End Sub
The key issue is not to double specify the worksheet.
I have a problem which is probably very easy for you to help me solve.
I have two tables: Table1 and Table2. Both tables are in the same worksheet called "Budget".
I want to add a command button / a plus button, that enables a user to add a new row at the bottom of each table.
However, after trying this via the macro record function, I recognized that at some point, the new rows of Table2 are added somewhere in the middle, after having added several new rows to Table1.
Can someone please provide me with a code, that solves this issue and sort of auto-adjusts?
I have never in my life coded something.
Thank you in advance for your help!
Code from recorder:
Sub NEWROW()
'
' NEWROW Makro
'
'
Range("B12:C12").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
Try this code
Option Explicit
Sub AddRows()
Dim wks As Worksheet
Dim tbl1 As ListObject
Dim tbl2 As ListObject
Set wks = ActiveSheet
Set tbl1 = wks.ListObjects("Table1")
Set tbl2 = wks.ListObjects("Table2")
tbl1.ListRows.Add
tbl2.ListRows.Add
End Sub
Table object is explained here
Update Ok, for an absolute beginner this might be the easiest way to do it.
Sub AddRowTbl1()
Dim wks As Worksheet
Dim tbl As ListObject
Set wks = ActiveSheet
Set tbl = wks.ListObjects("Table1")
tbl.ListRows.Add
End Sub
Sub AddRowTbl2()
Dim wks As Worksheet
Dim tbl As ListObject
Set wks = ActiveSheet
Set tbl = wks.ListObjects("Table2")
tbl.ListRows.Add
End Sub
PS A more advanced user would use a function
Function tblAddRow(tblname As String, wks As Worksheet)
Dim tbl As ListObject
On Error GoTo EH
Set tbl = wks.ListObjects(tblname)
tbl.ListRows.Add
EH:
End Function
Sub Test_tblAdd()
tblAddRow "Table1", ActiveSheet
End Sub
I am trying to write a code that will copy data from one workbook and save it to a closed book. The problem is that I need the data transposed from columns to rows. I have tried paste specials but they did not work. Below is the code I used that will at least transfer the data from the active sheet to the closed one.
Sub LessonLearned()
Dim destSht As Worksheet
Workbooks.Open Filename:="C:\Users\ceasat28\Documents\LessonsLearnedTest\LessonsLearnedLog.xlsx"
Set destSht = ActiveWorkbook.Worksheets("DiscoveredLessons")
ActiveSheet.Unprotect Password:="Secret"
With ThisWorkbook.Worksheets("Sheet1")
With .Range(.Range("LL_Data"), .Range("LL_Data").End(xlDown))
destSht.Cells(destSht.Rows.Count, 1).End(xlUp).Offset(1).Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
destSht.Parent.Close True
End With
End Sub
Can someone please help me transpose the values?
Thanks,
Tara
Couple of issues:
You need to actually transpose the data
You need to correctly size the destination range based on the transposed source range
Sub LessonLearned()
Dim destWb As Workbook
Dim destSht As Worksheet
Dim Data As Variant
Set destWb = Workbooks.Open(Filename:="C:\Users\ceasat28\Documents\LessonsLearnedTest\LessonsLearnedLog.xlsx")
Set destSht = destWb.Worksheets("DiscoveredLessons")
destSht.Unprotect Password:="Secret"
With ThisWorkbook.Worksheets("Sheet1")
With .Range(.Range("LL_Data"), .Range("LL_Data").End(xlDown))
Data = Application.Transpose(.Value)
destSht.Cells(destSht.Rows.Count, 1).End(xlUp).Offset(1).Resize(.Columns.Count, .Rows.Count).Value = Data
End With
destSht.Parent.Close True
End With
End Sub
I'm trying to sort through each table within each worksheet in an active workbook and reset all the filters.
Note that each table is a pivot table.
Sub ResetFilters()
Dim ws As Worksheet
Dim wb As Workbook
Dim listObj As ListObjects
For Each ws In ActiveWorkbook.Worksheets
For Each listObj In ws
With ActiveSheet.listObj.Sort.SortFields.Clear
End With
Next listObj
Next ws
End Sub
Error received: "Object doesn't suppor this property or method" on Line 7.
Assuming you mean clear filters and remove sorting, you can use:
Sub ResetFilters()
Dim ws As Worksheet
Dim wb As Workbook
Dim listObj As ListObject
For Each ws In ActiveWorkbook.Worksheets
For Each listObj In ws.ListObjects
If listObj.ShowHeaders Then
listObj.AutoFilter.ShowAllData
listObj.Sort.SortFields.Clear
End If
Next listObj
Next ws
End Sub
This worked for with a small change to the previous answer. Just use the ShowAutoFilter property instead of ShowHeaders. ShowAutoFilter is the property that references the these things: Screencap of AutoFilter Dropdown Box in Excel Table
(I would have just commented on the other answer, but I need 50 "reputation" -_- )
7-line solution
This will show all data without removing the filter, and won't fail on unfiltered sheets.
Sub RemoveFilters()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
On Error Resume Next
ws.ShowAllData
Next ws
End Sub
The poster's VBA might have failed on encountering an unfiltered sheet.
To remove each filter entirely, just replace lines 4+5 with Cells.AutoFilter.