VBA: Refresh an Excel page linked with Nielsen - vba

I just discovered VBA on Excel and I was wondering if (and how) I could refresh automatically an Excel page linked with a Nielsen base thanks to a VBA code.
I have already succesfully created such a VBA code for an Excel document linked with a VBA base, but i ran out of ideas for that. Do you have any ideas?
Thanks for helping!
Your sincerely,
Laurent

You should ask your companies Nielsen contact for the NITRO Components Reference Guide document. It has examples and code to help you on this. The code is in vb which you will have to translate to vba (minor differences).
Dim acnNitro As New ACNNITRO
Dim acnNitroUpdate As ACNielsenNitro.ACNNitroUpdate
Dim WS As Worksheet
Dim bret as Boolean
acnNitro.ParentApp = Application
acnNitroUpdate = acnNitro.ACNNitroUpdate
WS = ActiveSheet 'or Set WS = WorkSheets("My Sheet")
bret = acnNitroUpdate.UpdateAllNRanges(WS, ntrSelectGet)
acnNitro = Nothing
acnNitroUpdate = Nothing
WB = Nothing

Related

How To Develop My Own Excel Add-in in VB.Net

I am trying to create an Excel Add-in using Vb.Net. I've started an Excel 2007 Add-in Project in VS2010. Sadly, I am not good with vb.net; I am more a VB6 developer in this regard, and my ThisAddin.vb code is:
Public Class ThisAddin
Private Sub ThisAddIn_Startup() Handles Me.Startup
End Sub
Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
End Sub
' test function; simple
Public Function getRowCount() As Long
Dim thisWB As Workbook = Me.Application.ThisWorkbook
Dim activWS As Worksheet
activWS = thisWB.ActiveSheet
Return activWS.UsedRange.Rows.Count
End Function
End Class
I've also added a Ribbon item (via Add New Item... menu option) in designer mode (not xml) - and then add a button. Then I go to code and try to call the function and I get this error when using:
MsgBox(Globals.ThisAddIn.getRowCount())
Which I got from this link: Calling a procedure within another class
To be honest, I've been trying a myriad things and I've been getting so many errors. I've been looking online as well for a tutorial on creating my own Excel Addin from scratch with no real luck. I would like not to use Add-In-Express since that's a third party app and I have to create an Excel add-in for my company from scratch.
Does anyone have an idea on how I can create a vb.net coded Excel Addin (2007) that I can use as a template or guide? I've tried several and many rely on Add-In-express and I really cannot go that way. I have a lot of VBA code (natural VBA so it's in a module in an my excel files' VBA/Developer section) and I think I can translate those from VBA/VB6 to VB.Net format so that's not my concern. It is really about getting to code my own Excel Addin in VB.Net. Any help would really be great. Thank you.
*note: I would also like not to have to ask coworkers (or do myself) to just add to the quick access toolbar the functions and subs I've created since that's really not a solution, considering that those buttons will be there when they create or open another workbook. Essentially, I've got to create my own excel addin in vb.net. Thank you once again.
The issue has to do with the definitions in Microsoft.Office.Tools.Excel and Microsoft.Office.Interop.Excel. To code an "Interop" version you could use this:
Public Function getRowCount() As Long
Dim thisWB As Excel.Workbook = Application.ActiveWorkbook
Dim activWS As Excel.Worksheet = CType(thisWB.ActiveSheet, Excel.Worksheet)
Return activWS.UsedRange.Rows.Count
End Function
To extend the functionality of the Native objects and use VSTO, you could do it like this:
Public Function getRowCount() As Long
Dim NativeWorkbook As Excel.Workbook = Application.ActiveWorkbook
Dim NativeWorksheet As Excel.Worksheet = CType(NativeWorkbook.ActiveSheet, Excel.Worksheet)
Dim thisWB As Workbook = Nothing
Dim activWS As Worksheet = Nothing
If NativeWorkbook IsNot Nothing Then
thisWB = Globals.Factory.GetVstoObject(NativeWorkbook)
End If
If NativeWorksheet IsNot Nothing Then
activWS = Globals.Factory.GetVstoObject(NativeWorksheet)
End If
Return activWS.UsedRange.Rows.Count
End Function
This is a function you can put in ThisAddin.vb that will create a new Worksheet. Note that this function names the Worksheet and adds it to the end.
Public Function AddWorkSheet(sheetName As String) As Worksheet
Dim wk = Application.ActiveWorkbook
Dim ws As Worksheet = Nothing
Try
ws = CType(wk.Sheets.Add(, wk.Sheets(wk.Sheets.Count)), Worksheet)
ws.Name = sheetName
Catch ex As Exception
Throw
Finally
AddWorkSheet = ws
End Try
End Function
To use this outside of ThisAddin.vb you could do something like this:
Dim ws As Excel.Worksheet
Dim newSheetName As String
.
'
ws = Globals.ThisAddIn.AddWorkSheet(newSheetName)

Edit links to a file in VBA (in Publisher)

I am trying to edit the links to a lot of Excel file using Publisher. Given that Microsoft seems to not allow to use relative links, I am trying to create something similar in VBA. I did not have found a lot of documentation online, just some reference to LinkSources.
Is there anyway possible to change those links with VBA?
I assume you know how to add the reference to the Excel object library. Then to read the links:
dim xl as New Excel.Application
xl.Visible = True
Dim wb As Workbook
Set wb = xl.Workbooks.Open("C:\path to \Myworkbook.xlsx")
Dim v As Variant
v = wb.LinkSources(xlExcelLinks)
Dim first_link as String
first_link = v(1)
MsgBox "First link is to " & first_link
To change the links:
Dim new_link as String
new_link = "C:\path to\Alternative link.xlsx"
wb.ChangeLink first_link, new_link
Hopefully you get the idea.

Copy VBA code from one Worksheet to another using VBA code

Ok here is what I want to accomplish: I am trying to copy all the VBA code from "Sheet2" to "Sheet 3" code pane. I'm NOT referring to copying a Module from one to another but the excel sheet object code.
I already added a Reference to MS VB for Applications Extensibility 5.3
I'm not sure where to start but this is what I have started with and its not going anywhere and probably all wrong. Please Help - Simply want to programmatically copy sheet vba code to another sheet vba pane.
Dim CodeCopy As VBIDE.CodePane
Set CodeCopy = ActiveWorkbook.VBProject.VBComponents("Sheet2").VBE
ActiveWorkbook.VBProject.VBComponenets("Sheet3").CodeModule = CodeCopy
Use the CodeModule object instead of the CodePane, then you can create a second variable to represent the destination module (where you will "paste" the code).
Sub test()
Dim CodeCopy As VBIDE.CodeModule
Dim CodePaste As VBIDE.CodeModule
Dim numLines As Integer
Set CodeCopy = ActiveWorkbook.VBProject.VBComponents("Sheet2").CodeModule
Set CodePaste = ActiveWorkbook.VBProject.VBComponents("Sheet3").CodeModule
numLines = CodeCopy.CountOfLines
'Use this line to erase all code that might already be in sheet3:
'If CodePaste.CountOfLines > 1 Then CodePaste.DeleteLines 1, CodePaste.CountOfLines
CodePaste.AddFromString CodeCopy.Lines(1, numLines)
End Sub
In addition to adding a reference to "Reference to MS VB for Applications Extensibility 5.3"
You'll also need to enable programmatic access to the VBA Project.
In Excel 2007+, click the Developer item on the main Ribbon and then
click the Macro Security item in the Code panel. In that dialog,
choose Macro Settings and check the Trust access to the VBA project
object model.
Thank you all! After testing multiple suggestions above, where "b" is the Worksheet name, you must use .CodeName, NOT .Name
Set CodePaste = ActiveWorkbook.VBProject.VBComponents(WorkShe‌ets(b).CodeName).Cod‌​eModule
If you have set your target worksheet as an object:
Dim T As Worksheet
Set T = Worksheets("Test")
Then you simply need:
Set CodePaste = ActiveWorkbook.VBProject.VBComponents(Worksheets(T.Name).CodeName).Cod‌​eModule

'DBNull' Error when attempting to set a range in VB.net. Database is Excel

I've written a pretty useful macro within Excel in VBA and am attempting to transfer it to a stand-alone windows application written in VB.net. I've found all the referencing pretty confusing and am now facing trouble in converting the general syntax of my code.
I'm pasting below the relevant code:
Dim ElevenSheets As Excel.Worksheet
Dim TwelveSheets As Excel.Worksheet
Dim ThirteenSheets As Excel.Worksheet
Dim WorkingSheet As Excel.Worksheet
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open("FILENAME.xls") 'Removed file name for company confidentiality purposes.
ElevenSheets = xlWorkBook.Worksheets("2011")
TwelveSheets = xlWorkBook.Worksheets("2012")
ThirteenSheets = xlWorkBook.Worksheets("2013")
WorkingSheet = xlWorkBook.Worksheets("WorkingSheet")
...
Cell = WorkingSheet.Range("B3") '<--- This line causes the error.
CurrentCell = (Cell.Row & "," & Cell.Column)
CurrentRow = Cell.Row
MyColumn = (Cell.Column)
CurrentCell = (CurrentRow & "," & MyColumn)
So, as you can see I've pointed out the line that gives me an error. I'm trying to set a range names "Cell" and the error "MissingMemberException unhandled No default member found for type 'DBNull'" presents itself.
Does anyone know what I've done wrong? I'm sure it's something very simple with my syntax but am finding this whole process difficult and also finding it difficult to understand other reasonably similar topics on the internet.
Thanks for bothering to ready this and let me know if you need more context,
Josh
I dont see any constructor for Cell. Also you dont have to explicitly declare all those worksheets. You can simply call them using something like xlWorkBook("WorkingSheet").Range("B3")
I have fixed the problem through trial and error. The way I did it was simply change these declarations:
ElevenSheets = xlWorkBook.Worksheets("2011")
TwelveSheets = xlWorkBook.Worksheets("2012")
ThirteenSheets = xlWorkBook.Worksheets("2013")
WorkingSheet = xlWorkBook.Worksheets("WorkingSheet")
To this:
ElevenSheets = CType(xlWorkBook.Worksheets(1), Excel.Worksheet)
TwelveSheets = CType(xlWorkBook.Worksheets(2), Excel.Worksheet)
ThirteenSheets = CType(xlWorkBook.Worksheets(3), Excel.Worksheet)
WorkingSheet = CType(xlWorkBook.Worksheets(4), Excel.Worksheet)
I don't know whether changing the sheet names to the sheet number fixed it or the CType fixed it. But one of them did.
Thank-you for your contributions!

How to enable Grouping on a protected Excel 2007 sheet - without macros

I have an Excel 2007 XLSX workbook I am developing. It has multiple sheets and most of those use Excel's Grouping feature. I want to be able to protect the sheets, to protect some formulas and such from user intervention, but it seems there's no way to do that and still allow the user to expand or collapse the Grouped columns whenever they want.
It appears the standard answer on this is to insert a macro with commands like:
ActiveSheet.EnableGrouping
ActiveSheet.Proect UserInterfaceonly = true
But this workbook has no macros now and cannot have any ever. Is there a way to do this in Excel without a macro?
Thanks!
If there is nothing preventing you from running the code externally, then just put this code into another macro enabled workbook OR run it from a seperate C# winform/console application exe.
Note I had quick search of the Excel 2007 object model and didn't find the EnableGrouping method, but maybe i wasn't looking hard enough.
External VBA
Sub UpdateWorkbook()
Constant workbookpath As String = "C:\somepath\someworkbookname.xlsx"
Dim wkbk As Workbook
Set wkbk = Application.Workbooks.Open(workbookpath)
Dim wksht As Worksheet
Set wksht = wkbk.Worksheets.Item("sheetname")
wksht.EnableGrouping
wksht.Protect UserInterfaceonly = true
Set wksht = Nothing
Set wkbk = Nothing
End Sub
External C#
public void UpdateWorkbook()
{
const string workbookpath = #"C:\somepath\someworkbookname.xlsx";
Excel.Application xlApp = New Excel.Application();
// To use the next line you need the Excel Extension library otherwise use Type.Missing.
Excel.Workbook wkbk = Application.Workbooks.Open(workbookpath);
Excel.Worksheet wksht wksht = (Excel.Worksheet)wkbk.Worksheets.get_Item("sheetname");
//Check the way this method works..
wksht.EnableGrouping();
// UserInterfaceOnly is the 6th parameter, so 5 before and 11 after.
wksht.Protect(missing,missing,missing,missing,missing,True,missing,missing,missing, missing,missing,missing,missing,missing,missing,missing,missing);
Set wksht = null;
Set wkbk = null;
Set xlApp = null;
}