''''''''Public Variables Module'''''''
Public oXL As Microsoft.Office.Interop.Excel.Application
Public oWB As Microsoft.Office.Interop.Excel.Workbook
Public oSheet As Microsoft.Office.Interop.Excel.Worksheet
''''''''End Public Variables Module''''''''
'''''''''Form1.vb'''''''''''
oXL = New Excel.Application
oXL.Visible = True
oWB = oXL.Workbooks.Add
oSheet = oWB.ActiveSheet
oWB.Worksheets(1).range("a1").select()
oWB.Worksheets(1).Paste()
oSheet.UsedRange.Copy()
oWB.Worksheets.Add()
oWB.Worksheets("Sheet2").range("a1").select()
oWB.Worksheets("Sheet2").PasteSpecial(Transpose:=True)
'''''''''End Form1.vb'''''''''''
It's crashing on the pastespecial line with error:
Additional information: Exception from HRESULT: 0x800A03EC
I've tried skipping the range("a1").select step on sheet2 to no avail.
I should add that the reason I am pasting data to the first worksheet before re-copying it and pasting to the second is because the data is already on the the clipboard from an external application, and I am assuming it isn't possible to transpose it on the initial paste when it's data from an external source.
If I change the pastespecial to a normal paste, it works fine. So I am assuming something is wrong with my pastespecial command.
I have tried copying multiple forms of data to my clipboard before running the app so it's not related to a specific set of data being untransposable.
I have also verified that it is in fact selecting the usedrange before copying (vs the entire sheet).
Figured it out, needed to specify a range as well:
oWB.Worksheets("Sheet2").range("a1").PasteSpecial(Transpose:=True)
Related
I am using Access 2007. In my Access file I have VBA code that opens an Excel
workbook when the Access program begins. The Excel workbook remains open
while the program is running. I use this code to open the workbook:
Public xl As Object
Public wb1 As Object
Set xl = New Excel.Application
Set wb1 = xl.Workbooks.Open("c:\Book1.xlsx")
Now while the program is running I may need to open a second Excel workbook,
get some data from it, then close it. I used this code:
Public wb2 As Object
Set wb2 = xl.Workbooks.Open("c:\Book2.xlsx")
But this doesn't work. It seems that the first workbook may have closed because
if I do debug.print Workbooks.Count the response is "0".
Any suggestions on how to open a second workbook and still have the first workbook open?
Should I create a second instance of Excel and use that to open the second workbook?
Thank you.
You may have more luck with this procedure instead:
Sub OpenExcel(fName As String)
Dim xlApp As Object
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
'Excel wasn't running, start it from code
Set xlApp = CreateObject("Excel.Application")
End If
On Error GoTo 0
xlApp.Workbooks.Open (fName)
End Sub
With your code it's possible that Excel was opening but the second workbook did not default to visible, which could be set with xl.Visible = True. Alternatively, a second xl object could have helped.
Regardless, this method is safer is a number of ways.
Make sure when you're done, make sure you clean up (and free up memory) with something like xlApp.Quit and Set xlApp = Nothing.
In fact, I'd suggest you reboot right now... before you do, take a look at the running processes in your Task Manager - I wouldn't be surprised if you have several "invisible" instances of Excel running. :)
Hi, I have to perform Import from Excel file into Access DB, without duplicates. Only way that this can be done is by creating table where you can Import Excel data, and then append It to destination table by buiilding a Query. Problem is that this Excel file has to be .xls format, none other format is working for me (don't know why). How can I do that from Access - desirable without opening Excel workbook, just change It's extension file name ? This is what I tried (I receive error : " object doesn't support this property or method")
Dim XFile As Excel.Application
Set XFile = CreateObject("Excel.Application")
XFile.Workbooks.Open "C:\Users\Mike\Desktop\Copy.xlsx", True
XcelFile.SaveAs Filename:="C:\Users\Mike\Desktop\Copy.xlsx", FileFormat:=56
XFile.Workbooks.Close
End Sub
As you see, I need to save from .xlsx, It's default format for Excel 2013.
Any help much appreciated !!
Thanks #Porcupine911, that worked, even without opening workbook :)
Dim XcelFile As Excel.Application
Dim wb As Excel.Workbook
Set XcelFile = New Excel.Application
Set wb = XcelFile.Workbooks.Open("Filepath here")
wb.SaveAs Filename:="Filename here, with different destination and desirable extension", FileFormat:=56
wb.Close
Set XcelFile = Nothing
End Sub
I have 2 Workbooks:
Workbook With Macros
Workbook to write to.
I have 2 subs. One is to insert a hyperlink in the 'workbook to write to' and the other Sub will execute when the hyperlink is clicked.
However, my sub to create the Hyperlink is causing me some confusion. I need to reference the worksheet in the Macro Enabled Workbook, but doing that requires a Workbook.Open command. Obviously the Macro Enabled Workbook will already be open so this closes and re-opens it. I've gotten very muddled with this, can someone point me in the right direction?
So Macro Sheet will have the below Sub, the link is to another Sub in the same sheet. "CreateHyperlinks" is called from another method which writes to an external spreadsheet.
Obviously "ActiveSheet" below is wrong. I want to write to a different spreadsheet, so I will need to open it also (I assume)?
Or, can I pass the worksheet that is being written to from it's write method which is calling "CreateHyperlinks" or am I coupling everything too much as it is?
' This is called elsewhere
Sub CreateHpyerlinks(rangeValue, textValue)
Dim fileName As String
Dim wb As Workbook
Dim TheSheet As Worksheet
fileName = "c:\blah\blah.xlsm"
' ** This is the part: How do i reference "TheSheet" without opening the XL?
Set wb = Workbooks.Open(fileName:=fileName)
Set TheSheet = wb.Sheets("MasterCard")
TheSheet.UsedRange.Select
ActiveSheet.Hyperlinks.Add Anchor:=rangeValue, Address:=TheSheet!THISISMYMACROHERE(textValue), SubAddress:="", ScreenTip:="Go to Word Documebnt", TextToDisplay:=textValue
End Sub
UPDATED:
I have updated the Sub, and am hitting Object does not support his method or property
Sub CreateHpyerlinks(rangeValue, textValue)
Dim fileName As String
Dim wb As Workbook
Dim wbWrite As Workbook
Dim TheSheetWithMacros As Worksheet
Dim TheSheetToWriteTo As Worksheet
fileName = "c:\WorkbookToWriteTo.xlsx"
Set wb = Application.Workbooks(ThisWorkbook.Name)
Set TheSheetWithMacros = wb.Worksheets("Sheet1")
Set wbWrite = Workbooks.Open(fileName:=fileName)
Set TheSheetToWriteTo = wbWrite.Worksheets("Sheet1")
' This Line Errors:
TheSheetToWriteTo.Hyperlinks.Add Anchor:=rangeValue, Address:="", SubAddress:=TheSheetWithMacros!Goto80BytePopulationGuide(textValue), ScreenTip:="Call Macro", TextToDisplay:=textValue
wbWrite.Save
wbWrite.Close
End Sub
The line TheSheetToWriteTo.Hyperlinks.Add Anchor:=rangeValue, Address:="", SubAddress:=TheSheetWithMacros!Goto80BytePopulationGuide(textValue), ScreenTip:="Call Macro", TextToDisplay:=textValue is at fault, clearly TheSheetWithMacors!CallMacro doesn't work like I had hoped.
You may be able to copy the Module's code from one workbook to the other workbook. I assume for now that the Worksheet module only contains the macro and nothing else (or that it is OK to copy everything in that module).
So the reason I propose this, is that if you can copy it, then you can point the hyperlink to a local (within the same file) macro -- because we're copying the module to the new file -- which I think you have already been able to do.
This requires you to check the trust center setting to allow access to the VBProject. A prompt will ask you to do that before it can proceed.
Sub writeit()
'copies a specified code module from one workbook, to
' a code module in another workbook
'
'ASSUMPTIONS:
'-the specified destination must already exist
'-the specified destination does not already contain
' any procedures that would conflict naming with the
' copied module procedures
MsgBox "Please make sure to enable access to ""Trust access to the VBA Project Object Model""", vbInformation
Application.CommandBars.ExecuteMso("MacroSecurity")
Dim macrobook As Workbook
Dim otherbook As Workbook
Dim lines As String
Dim destModule As Object 'VBComponent
Dim copyModule As Object 'VBComponent
Set macrobook = ThisWorkbook 'Modify as needed
Set copyModule = macrobook.VBProject.VBComponents("Sheet1") 'Name of your module to copy
Set otherbook = Workbooks(2) 'Modify as needed
Set destModule = otherbook.VBProject.VBComponents("Sheet1") 'name of where it will go
'Appends the lines from CopyModule to DestModule
With destModule.CodeModule
.InsertLines .CountOfLines + 1, copyModule.CodeModule.lines(1, copyModule.CodeModule.CountOfLines)
End With
End Sub
I am making a simple form that extract the data from my excel sheet, such as name, date of birth, and address. And inserting them into my word form, I am doing 20-30 sheets everytime, so I think it might be able to save the copying & pasting time.
I tried to follow this tutorial: http://www.makeuseof.com/tag/integrate-excel-data-word-document/
And created a button with a simple label named m_name, for member's name. But it tells me Compile error: User-defined type not defined. And flaged on line 1.
I am using Word 2003, (I am not able to find the Tools > Reference as the guide was asking for). I am not sure if it is related to this error.
Private Sub CommandButton1_Click()
Dim objExcel As Excel.Application
Dim exWb As Excel.Workbook
Set exWb = objExcel.Workbooks.Open("U:\test.xls")
ThisDocument.m_name.Caption = exWb.Sheets("Member's Data").Cells(3, 3)
exWb.Close
Set exWb = Nothing
End Sub
Yes, it's very important to set references according to the tutorial.
However, change these two lines:
Dim objExcel As Excel.Application
Dim exWb As Excel.Workbook
to:
Dim objExcel As Object
Set objExcel = CreateObject("Excel.Application")
and the code should work, too.
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;
}