Whenever I create a new excel worksheet I always go through the View menu and disable Gridlines, and so I thought I'd make it so that they are disabled as a default.
I've tried using templates for this but that will only work for the first worksheet which is why I now want to run the following code whenever a worksheet is created on any workbook.
ActiveWindow.DisplayGridlines = False
I included this line in the following method on a workbook which I saved as a .xlam file and later installed, but that didn't disable the gridlines
Private Sub Workbook_NewSheet(ByVal Sh As Object)
ActiveWindow.DisplayGridlines = False
End Sub
Can somebody tell me what code might work? I just need this code to run every time I create a new worksheet regardless of the book. It might be that What I need is not an Add-In, really what I'm looking for is for a way to automatically disable gridlines on all worksheets.
I'm a beginner at vba but have created a custom Add In in Excel with several User Defined Functions (UDF)...they are all working for multiple users but I can't figure out how to update the reference data in ThisWorkbook. Background: I have several clients within the same industry that each have a custom spreadsheets, however, the general excel functions and reference data (statistical rates etc) are the same so I have built UDF to automatically pull the rates/calculate results using index/match. What I can't figure out is how to update the reference data that I have saved in the Add In - "Rates" Sheet - I need to input a new rate on an annual basis (and adjust range) but I can't open/view the excel sheets in my Add In. Here is my code:
Function RATE1(Year As Double)
Dim WBT As Workbook
Dim WSD As Worksheet
Set WBT = ThisWorkbook
Set WSD = WBT.Worksheets("Rates")
RATE1 = Application.WorksheetFunction.Index(WSD.Range("R3:r25"), Application.WorksheetFunction.Match(Year, WSD.Range("N3:n25"), 0), 1)
End Function
Any help/suggestions are greatly appreciated. Thanks.
Open a spreadsheet that has a link to the add in. Open the VBA Editor. In project explorer you will see the add in shown. Click on this and then open up to ThisWorkbook. Click on this and then look in Properties explorer for the property "IsAddin". Set this to false and your add in will appear. Make your changes, then set the IsAddin Property back to True. Then save your changes by clicking on the add in in project explorer and then on the Save icon in the toolbar in the VB Editor.
I have a pre-defined Excel template which i would be filling it with data using .Net C# code.
I know the size of some columns but some of them i don't know, so when i export data into the excel, the presentation doesn't look good.
This xls sheet is sent to customer. 1000's of documents generated every day like this. It's a tiresome work to every time open a newly generated excel document and change the width of the column to autofit by double clicking the column.
Is it possible in Microsoft Excel to AutoFit the size some column and not for few other columns?
range("a2:d2").Columns.AutoFit
You could also use something like
range("a2").CurrentRegion.Columns.AutoFit
or
range("a:d, f:g").Columns.AutoFit
If you want the VBA for it is:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Application.ScreenUpdating = False
For Each Value In Target.Columns
Worksheets(Sh.Name).Columns(Value.Column).AutoFit
Next Value
Application.ScreenUpdating = True
End Sub
Go to the Developer Tab (have to turn it on via File>Options>Customise Ribbon.
Open Visual Basic
Select Thisworkbook
Copy the Above VBA Code
Paste into the VBA Window
Close the VBA Window and the VBA editor
So basically, I have a Visio file that has lots of shapes and data. I'm trying to create a shape report into a new excel file, and then have the excel file turn the exported data into a pivot table programmatically.
I have a macro running in Visio that generates the excel file with data in normal table form already. I want to be able to run a macro in Visio that activates the excel window of exported data and runs a macro to make it into a pivot table. However, any excel macro code I put into my visio macro modules is unrecognized (e.g. "Range"), presumably because they're not words recognized by Visio.
My question is this: How do I run a macro that alters an Excel file FROM a Visio module?
I know how to call a macro IN an Excel file from Visio (Excel.run "ModuleNAME"), but that would require the macro to already be in Excel. Since I'm creating new Excel files when I get shape reports, these files don't have any macros in them.
An alternative solution would be if I was able to export shape data report from Visio as a new sheet to an EXISTING Excel file that already contained the macro, but I'm not sure how to do this either... (export shape reports from Visio to existing Excel file)
My Code to Generate Excel Report:
Sub Excel2()
Visio.Application.Addons("VisRpt").Run
("/rptDefName=ReportDefinition_2.vrd/rptOutput=EXCEL")
End Sub
I would like to run this macro after running Excel2()
Sub NewMacro()
AppActivate "Microsoft Excel"
Dim AppExcel As Excel.Application
Set AppExcel = CreateObject("Excel.Application")
'Do all sorts of fancy stuff with making pivot tables
'Do all sorts of fancy stuff with making pivot tables
End Sub
You can just use the Get/CreateObject function to use an Excel application instance from Visio.
Have a look at this article:
http://msdn.microsoft.com/en-us/library/gg251785.aspx
You might also find this useful:
http://support.microsoft.com/kb/309603
...it describes the opposite direction (ie controling Visio from another app), but in your case you'd do something like:
Dim AppExcel As Excel.Application
On Error Resume Next
Set AppExcel = GetObject(, "excel.application")
If AppExcel Is Nothing Then
Set AppExcel = CreateObject("excel.application")
End If
Hope that helps.
I've got an Access 2007 database on which I have created around 15 SQL queries to process specific data, I have created a main frame navigation menu using menus in Access, I now need to extract all th queries to Excel using VBA code, I have managed to do this with the code below by creating a button and specifying this code to it.
Private Sub query1_Click()
DoCmd.TransferSpreadsheet acExport, _
acSpreadsheetTypeExcel9, "Total Users and Sessions", _
"C:\UsersandSessions.xls", , "Total Users & Sessions"
End Sub
Now my problem at the moment is that fine the query is exported to Excel, but it is done so without any formatting applied at all, I would like to add some formatting at least to the headers and maybe a title inside the spreadsheet, and one thing I dont really like is that all records are being started from the first cell. Also I would prefer that if I hit that button again in Access and the Excel spreadsheet has already exists with that query output then when clicked again it will write again to a the next available sheet.
Any suggestions or ideas a very welcome.
The short story, is you can't. You might be able to do some scripting on the Excel side to format the resulting file. If you want something pretty, you probably want to create a report.
You could, instead mount the excel sheet as a table, and then on a separated sheet in the excel file, reference the first sheet, and format the second sheet for viewing.
if you use DoCmd.TransferSpreadsheet and create an original and then edit it so that the formatting is correct, you can then run DoCmd.TransferSpreadsheet again and it will update the file with the values but keep the formatting.
However, if a human then changes the file by adding new tabs, or adding calculations, etc, then the DoCmd.TransferSpreadsheet will no longer work and will fail with an ugly error message. So what we do in our enviroment is DoCmd.TransferSpreadsheet to an original file with formatting, and follow that up in the VBA by copying the file to the users desktop, and then opening that copy so the user doesn't mess up the original source excel file.
This approach is a minimum code, clean, and easy to maintain solution. But it does require a extra "source" or original file to be hanging around. Works in Access 2007.
You also would like the results to end up on a new tab. Unfortunately, I think it will take some excel automation to do that. The VBA inside Acccess can call a function inside the VBA in Excel. That VBA could then copy the tabs as needed.
My idea would be a hybrid of Excel automation from Access and creating a template in Excel as well that would have a data table linked to your query.
To start create your data table in Excel. You can start three rows down and two columns to the right if you want or wherever. Go to your data tab and click access, find your db, choose your query you want to link to, choose table as the radio button but click properties next instead of ok, uncheck the enable background refresh, this part is critical ... under the definition tab in the connection string you will see a part that says Mode=Share Deny Write change that to Mode=Read, this will make sure that the query refreshes without errors from an MS Access VBA while the db is open and will keep your users from writing back to the db in case your query is a writeable query. Once you set that up you can adjust the table formatting however you choose from the table design tab and it will keep that formatting.
For the purposes of this we are going to assume you started the table in cell B4 ,and your named the worksheet CurrentDay, for purpose of the following VBA example be sure to replace that reference with your actual placement.
Next go back to Access and write your VBA first ensure that in your VBA window you have the reference to Microsoft Excel 12.0 Object Library is selected by going to Tools > References and selecting it from the alphabetical listing.
Create your sub as follows:
Sub query1_click()
Dim xl as Excel.Application
Dim wbk as Excel.Workbook
Dim wks as Excel.Worksheet
Dim RC as Integer
Dim CC as Integer
Set xl = New Excel.Application
Set wbk = xl.wbk.Open "X:\Filelocation\FileName.xlsx" 'name and path you saved the file you previously created
xl.Visible = True
'The above is not necessary but you may want to see your process work the first few times and it will be easier than going to task manager to end Excel if something fails.
RC = xl.Application.CountA(xl.wbk.Worksheets("CurrentDay").Range("B:B")) + 3 'This will count the rows of data in your table including your header so you can copy the data to another tab dynamically as the size of your table expands and shrinks we add 3 to it because we started at row 4 and we need the location of the last row of the record set.
CC = xl.Application.CountA(xl.wbk.Worksheets("CurrentDay").Range("4:4")) + 1 'This counts the header row and adds one space because we will use this as a location holder for our copy / paste function
Set wks = xl.wbk.Worksheets.Add
wks.Name = format(date(),"MM_dd_yy") 'this will name the tab with today's date... you can eliminate this step if you just want the sheets to be the generic Sheet1, Sheet2, etc.
With xl.wbk
.Worksheets("CurrentDay").Range(Cells(4,2),Cells(RC,CC)).Copy
.wks.PasteSpecial xlPasteValues 'This pastes the values so that the table links do not paste otherwise every tab would just refresh everyday.
.wks.PasteSpecial xlPasteFormats 'This gets your formatting.
.RefreshAll 'This will refresh your table
Wend
With xl
.Save
.Close False
.Quit
Wend
Set xl = Nothing
Set wbk = Nothing
Set wks = Nothing
End Sub
That should get you to have your data to not start on A1 of your sheets, save your old data each time, and automate the steps from access.