FILTER OLAP CUBE WITH VBA - filter

I am trying to update an OLAP pivotfilter with the value of another cell (not in a pivot table).
The code I have is as follows:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'This line stops the worksheet updating on every change, it only updates when
'cell C1 or C2 is touched
If Intersect(Target, Range("C1:C2")) Is Nothing Then Exit Sub
'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim NewStock As String
'Here you amend to suit your data
Set pt = ActiveSheet.PivotTables("PivotFilter")
'The activesheet is named "Top1_Value"
Set Field = pt.PivotFields("[StockCode].[Stock Code_SKU].[Stock Code_SKU]")
NewStock = ActiveSheet.Range("C1").Value
'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.VisibleItemsList = NewStock
pt.RefreshTable
End With
End Sub
Any assistance to resolve would be appreciated.

Related

VBA to hide SlicerItems

I have a slicer that contains items from a pivot table. My users wants the ability to hide certain slicer items even though these items contains data. Is there a way to hide sliceritems via VBA code?
Why not just filter them out? If this is an OLAP PivotTable, see Pivot Table filter out only 1 option
If this is a "Traditional" PivotTable, then you can use a variation of the code I posted at Pivotfields multiple filter
Here's some amended code:
Sub FilterSlicer_Inverse()
Dim slr As Slicer
Dim sc As SlicerCache
Dim si As SlicerItem
Dim i As Long
Dim vItem As Variant
Dim vSelection As Variant
Dim pt As PivotTable
Set sc = ActiveWorkbook.SlicerCaches("Slicer_test")
vSelection = Array("1", "2") <= List the items you want to hide in here
For Each pt In sc.PivotTables
pt.ManualUpdate = True 'Stops PivotTable from refreshing after each PivotItem is changed
Next pt
With sc
.ClearAllFilters
On Error Resume Next 'In case one of the items isn't found
For Each vItem In vSelection
.SlicerItems(vItem).Selected = False
Next vItem
On Error GoTo 0
End With
For Each pt In sc.PivotTables
pt.ManualUpdate = False
Next pt
End Sub
Note that it doesn't matter if you change the PivotItems or the SlicerItems...you get the same result.

MS Access underline text in a Table

I have long Text field in a table in MS ACCESS. I need to underline it for specific text in a field. I tried to change Text format to Rich Text in the design view , but I am getting:
Error : Operation is not supported for this type of Object
In the table I have 320 rows. I need to Underline for N.J.S.A. only in the long text.
Please help me regarding this. Thanks in Advance
Well... you have to get that field property changed from plain text to rich text for this to work and design view should handle this. If not try the below code.
Public Sub TestUnderline()
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As Field
Dim rst As DAO.Recordset
Dim strSQL As String
Dim strString As String
Set db = CurrentDb
Set tbl = db.TableDefs("Table1") 'Change to your table name
Set fld = tbl.Fields("TestField") 'Change to your field name
With fld.Properties("TextFormat")
If .Value = acTextFormatPlain Then
.Value = acTextFormatHTMLRichText
End If
End With
strSQL = "SELECT TestField " & _ 'Change to your Field name
"FROM Table1;" 'Change to your table name
Set rst = db.OpenRecordset(strSQL)
Do While Not rst.EOF
If InStr(1, rst![TestField], "N.J.S.A") Then 'Change to your field name
strString = Replace(rst![TestField], "N.J.S.A", "<u>N.J.S.A</u>") 'Change to your field name
rst.Edit
rst![TestField] = strString 'Change to your field name
rst.Update
End If
rst.MoveNext
Loop
EndCode:
If Not rst Is Nothing Then
rst.Close
Set rst = Nothing
End If
If Not tbl Is Nothing Then
Set tbl = Nothing
End If
If Not db Is Nothing Then
Set db = Nothing
End If
End Sub
Credit given to:
How to convert a text field in an Access table to a rich text memo using VBA
and:
http://www.tek-tips.com/viewthread.cfm?qid=1538917

Select end row of table, copy and paste in another workbook

I have 2 workbooks.
Source Workbook
- Select a cell in column B, last row of a table. e.g. B29 (But this would change as the table grows)
2nd Workbook
- Paste that cell into G14 of the 2nd workbook (This doesn't change)
Source Workbook
- Select a cell in column D, same row - last row of table. e.g.. D29
2nd Workbook
- Paste that cell into D8 (This doesn't change)
This same process repeats 4 more times (Columns E-H) and all the pasting is done into C3, F14, I14 and E14 respectively.
The following code will do what you need. Just make the changes mentioned in the comments.
Sub Copy2Workbook() 'You need to place this in your source workbook!
Dim wbT As Workbook ' target workbook
Set wbT = Workbooks("United")
Dim shtT As Worksheet 'target worksheet
Set shtT = wbT.Worksheets("Name of target sheet") ' change name to fit your case
Dim shtS As Worksheet 'source worksheet
Set shtS = ThisWorkbook.Worksheets("Name of source sheet") ' change name to fit your case
Dim lastRow As Long
'*****************************************
lastRow = shtS.Cells(Rows.Count, "B").End(xlUp).Row
shtT.Range("G14").Value = shtS.Range("B" & lastRow).Value
'repeat the two lines of above while changing the references to your needs
End Sub

How to copy rows of from one sheet to another sheet using vbscript

Suppose I have Sheet(1) in an excel. Now i do also have 2500 rows which has data for the columns from A to BO.Now I want the data to copy from these sheet to another sheet of the same Excel file for 2500 rows but not the whole the columns,rather i need only columns from A to AA data to copy to the new sheet.
So how to frame it using VBscript?
Please help me.
How to copy rows of from one sheet to another sheet using vbscript
To copy data from one sheet to another you can use the Copy en PasteSpecial commands. To do this with a .vbs script do the following:
' Create Excel object
Set objExcel = CreateObject("Excel.Application")
' Open the workbook
Set objWorkbook = objExcel.Workbooks.Open _
("C:\myworkbook.xlsx")
' Set to True or False, whatever you like
objExcel.Visible = True
' Select the range on Sheet1 you want to copy
objWorkbook.Worksheets("Sheet1").Range("A1:AA25").Copy
' Paste it on Sheet2, starting at A1
objWorkbook.Worksheets("Sheet2").Range("A1").PasteSpecial
' Activate Sheet2 so you can see it actually pasted the data
objWorkbook.Worksheets("Sheet2").Activate
If you want to do this in Excel with a VBS macro you can also call the copy and paste methods. Only your workbook object will be something like ActiveWorkbook
This code is Working fine. Just Copy and paste it.
Dim CopyFrom As Object
Dim CopyTo As Object
Dim CopyThis As Object
Dim xl As Object
xl = CreateObject("Excel.Application")
xl.Visible = False
CopyFrom = xl.Workbooks.Open("E:\EXCEL\From.xls")
CopyTo = xl.Workbooks.Open("E:\EXCEL\To.xls")
For i = 0 To 1
''To use a password: Workbooks.Open Filename:="Filename", Password:="Password"
If i = 0 Then
CopyThis = CopyFrom.Sheets(1)
CopyThis.Copy(After:=CopyTo.Sheets(CopyTo.Sheets.Count))
CopyTo.Sheets(3).Name = "Sheet3"
Else
CopyThis = CopyFrom.Sheets(2)
CopyThis.Copy(After:=CopyTo.Sheets(CopyTo.Sheets.Count))
CopyTo.Sheets(4).Name = "Sheet4"
End If
Next
CopyTo.Sheets(1).Activate()
CopyTo.Save()
'CopyTo.SaveAs("E:\EXCEL\Check.xls")
xl.Quit()
Sub buildMissingSheet(strMissingSheet) 'Just passing the missing sheet name in
' Master Sheet code
' Working on creating the "Master Sheet" at this time...May need to seperate the the code a little.
Dim GetRows1 As Worksheet
Dim GetRows2 As Worksheet
Dim PutRows As Worksheet
Dim sglRowNum As Single, i%
If strMissingSheet = strMASTERSHEET Then ' Create the strMASTERSHEET
Set GetRows1 = Sheets(strRAWDATA) ' These two sheets could be missing but will code around that later.
Set GetRows2 = Sheets(strDATAWITH) ' The two sheets I am getting rows from
' Just creating a new worksheet here assuming it is missing
Worksheets.Add(After:=Worksheets(5)).Name = strMissingSheet
Set PutRows = Sheets(strMissingSheet) ' Missing sheet must be created before declaring.
PutRows.Select 'Select the sheet being built.
With Cells(1, 1)
.Value = strRAWDATA 'Not copying rows here but left it in this example anyway
.AddComment
.Comment.Visible = False
.Select
.Comment.Text Text:= _
Chr(10) & "Name of sheet including header and the last 32 entries at the time this sheet was updated."
End With
'Here is where we copy the whole row from one sheet to the other.
GetRows1.Rows(1).Copy PutRows.Rows(2) 'Copy header row from existing sheet to "Master Sheet" for instance.
GetRows1.Select
sglRowNum = ReturnLastRow(ActiveSheet.Cells) 'return last row with data on active sheet
' I wanted the last few rows of data "32 rows" so found the end of the sheet this code can be found on the internet in several places including this site.
'Now the code you may have been looking for move 32 row of data from one sheet to another.
For i = 1 To 32 'Start at row 3 on the Put sheet after sheet name and header.
GetRows1.Rows(sglRowNum - (32 - i)).Copy PutRows.Rows(i + 2)
Next i
end sub

How to add data to a specific column in an existing excel file using VBScript

I'm currently doing automation testing and need to write a dynamic value to an existing excel document in a specific column, this is what I have so far. Forgive I'm a novice
Sub WriteTRNtoExcelDoc
Dim fileName, sheetName
fname = "<Path_To_The_File>"
sheetName = "Sheet1"
Set app = Sys.OleObject("Excel.Application")
Set book = app.Workbooks.Open(fname)
Set sheet = book.Sheets(sheetName)
' What do I do next to add a value to a specific column or cell in this
' spreadsheet?
End Sub
Thanks in advance!
You create an Excel instance in a VBScript with
CreateObject("Excel.Application")
An already running Excel instance can be grabbed with
GetObject(, "Excel.Application")
In a worksheet you can access cells by using the Cells property:
Set app = CreateObject("Excel.Application")
app.Visible = True
Set book = app.Workbooks.Open(fname)
Set sheet = book.Sheets(sheetName)
sheet.Cells(2,3).Value = "foo"
Edit: If you need to find the first empty cell in a given column, you can use something like this:
row = 1
Do Until IsEmpty(sheets.Cells(row, 3).Value)
row = row + 1
Loop
sheet.Cells(row, 3).Value = RemPropValue

Resources