I am working in Access and need to pull data from an Excel file. The file is very large with several sheets that I don't need, so I want to take the values from one sheet and put them in a new workbook in order to simplify/speed up later calculations. Currently, it is including formatting along with the values and some of my later macros are not working even though they worked when I manually pasted the values into the new sheet. I tried running this directly in Excel (removing the references to the Excel.Application variable) and it worked fine. Is there something different I should be doing since this is being run in Access?
Private Sub CopyImportFile()
Dim xl As Excel.Application
Dim SourceBook As Excel.Workbook
Dim SourceSheet As Excel.Worksheet
Dim ImportBook As Excel.Workbook
Dim ImportSheet As Excel.Worksheet
Dim SourceSheetPath As String
SourceSheetPath = "File Path"
Set xl = CreateObject("Excel.Application")
Set SourceBook = GetObject(SourceSheetPath)
Set SourceSheet = SourceBook.Worksheets("Data Tape")
Set ImportBook = xl.Workbooks.Add()
SourceSheet.UsedRange.Copy
ImportBook.Sheets(1).Cells(1,1).PasteSpecial Paste:=xlPasteValues
xl.ActiveWorkbook.SaveAs FileName:="New File Path"
SourceBook.Close False
ImportBook.Close
Set xl = Nothing
Set ImportBook = Nothing
Set SourceBook = Nothing
Set ImportSheet = Nothing
Related
I recently started working with Excel.CustomProperties. To be clear, this is not Excel.CustomDocumentProperties. I need to store certain worksheet-specific settings for use by various VBA macros, and CustomProperties is perfect for the task. Or so I thought.
Unfortunately, I found the CustomProperties collection gets left behind if the worksheet is copied. This happens whether the copy is done in the UI by the user, or in VBA with the Worksheet.Copy method.
Is this by design, or is it an oversight by Microsoft?
If by design, why?
Are there any tricks to make it work if the user copies the sheet in the UI?
Sample code below...
Public Sub CustomPropertyTest()
'Run this is new blank workbook.
Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim wss As Excel.Sheets
Dim ws1 As Excel.Worksheet
Dim cps1 As Excel.CustomProperties
Dim cp1 As Excel.CustomProperty
Dim ws2 As Excel.Worksheet
Dim cps2 As Excel.CustomProperties
Dim cp2Count As Long
Dim wsLast As Long
Set xl = Excel.Application
Set wb = xl.ActiveWorkbook
Set wss = wb.Worksheets
Set ws1 = wb.ActiveSheet
Set cps1 = ws1.CustomProperties
Set cp1 = cps1.Add("cpTestName", "cpTestValue")
'CustomProperty now exists in first worksheet.
wsLast = wss.Count
ws1.Copy After:=wss.Item(wsLast)
'This can also be done in the UI by right-clicking on the worksheet tab
'and choosing "Move or copy...".
wsLast = wss.Count
Set ws2 = wss.Item(wsLast)
Set cps2 = ws2.CustomProperties
cp2Count = cps2.Count
' cp2Count is = 0
' The CustomProperties collection was not copied.
' CustomProperties.Item(1) exists in first worksheet, but not the copied one.
' Why?
Stop
End Sub
Private Sub CommandButton1_Click()
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
Set wkb = Workbooks("asd.xlsx")
Set wks = wkb.Worksheets("Sheet1")
Sheets("Sheet1").Shapes.AddChart.Chart.SetSourceData Source:=wks.Range("A1:B98")
End Sub
This is the code I've written to create a simple chart into my current workbook with data from another workbook in the same folder. Even though the relative path is the same the program returns runtime error 9, which says that I'm referencing a non-existent file. I've even tried putting the absolute path into the wkb variable, but doesn't work there as well.
Any help on this? TIA.
So, The main reason I was running in to this issue was that Excel needs the other workbook(s) to be open while plotting graphs AND the workbook in which the graph to be plotted as active. So I've modified the code to open the other workbooks, make the one in which I'm plotting the graph active, plot
the graph and then close.
target_workbook.xlsm is the workbook in which the graph has to be drawn, and asd.xlsx is workbook which contains the data.
Private Sub CommandButton1_Click()
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range
Workbooks.Open "C:\full_path_to_file\asd.xlsx"
Workbooks("target_workbook.xlsm").Activate
Set wb = Workbooks("asd.xlsx")
Set ws = wb.Worksheets("Sheet ABC")
Set rng = ws.Range("F3:F98,H3:H98")
MsgBox ("Test-1!")
Set chrt = Sheets("Sheet1").ChartObjects.Add(Left:=5, Width:=600, Top:=7, Height:=350)
chrt.Chart.SetSourceData Source:=rng
chrt.Chart.ChartType = xlLine
MsgBox ("test-2")
Workbooks("asd").Close SaveChanges:=False
End Sub
I have an Access database that runs a macro that opens some Excel files and formats the sheets to prepare them for later use. This code has been running fine until my company applied the latest Office Updates, and now I am getting a compile error "Method or data member not found" and its happening on the line...
wDate = Mid(XlSheet.Range("B4").Value, 13, Len(XlSheet.Range("B4").Value))
singling out the "Range". I cannot figure out why this started happening. Thanks for any assistance. Full code below...
Function ExcelProcess()
'Variables to refer to Excel and Objects
Dim MySheetPath As String
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
Dim MyFile As Variant
Dim MySheet As Variant
Dim wBook As Variant
Dim wSheet As Variant
Dim wDate As Variant
Dim rng As Range
Dim cel As Range
MyFile = Array("w1.xlsx", "w2.xlsx", "w3.xlsx")
MySheet = Array("T2_IND", "APPR_IND", "SLG_APPR_IND", "SLG_IND", "C2A_IND", "C3_IND", "C4_IND", "T3_IND", "T4_IND", "C2B_IND")
For Each wBook In MyFile
' Tell it location of actual Excel file
MySheetPath = "\\fs1\Training\CSC_Training_Ops\Training Only\Buzzard\Pulled Data\" & wBook
'Open Excel and the workbook
Set XlBook = GetObject(MySheetPath)
'Make sure excel is visible on the screen
XlBook.Windows(1).Visible = True
For Each wSheet In MySheet
'Define the sheet in the Workbook as XlSheet
Set XlSheet = XlBook.Worksheets(wSheet)
wDate = Mid(XlSheet.Range("B4").Value, 13, Len(XlSheet.Range("B4").Value))
XlSheet.Range("A15").FormulaR1C1 = "WE_Date"
If XlSheet.Range("A16").Value <> "No data found" Then
Set rng = XlSheet.Range(XlSheet.Range("A16"), XlSheet.Range("A16").End(xlDown).Offset(-1))
For Each cel In rng.Cells
With cel
.FormulaR1C1 = wDate
.NumberFormat = "m/d/yyyy"
End With
Next cel
End If
XlSheet.Rows("1:14").Delete Shift:=xlUp
XlSheet.Range("A1").End(xlDown).EntireRow.Delete Shift:=xlUp
Next
XlBook.Close SaveChanges:=True
Next
'Clean up and end with worksheet visible on the screen
Set Xl = Nothing
Set XlBook = Nothing
Set XlSheet = Nothing
End Function
There isn't any apparent problems with the code itself.
Since this broke with when you updated the office, I would venture a guess that it is an issue with the reference.
Go to Tools->References->
Remove all references to Excel Object Library
Save & Close the Macro Worksheet (Shouldn't be necessary, but only takes a sec)
Re-Open
Add in reference to only the latest version of Microsoft Excel 1X.0 Object Library
If this does not solve the issue, you may have to run a repair on office
Control Panel -> Add Remove Programs
Locate Microsoft Excel (Or office suite)
Run Repair
Finally, it was suggested to try late binding. Remove the references to the Microsoft Excel Object Library and update your declarations to:
Dim Xl As Object
Dim XlBook As Object
Dim XlSheet As Object
Set Xl = CreateObject("Excel.Application")
Hope this helps!
Is it possible you got upgraded from Office 14.0 to 15.0 or 16.0? Hit Alt+F11 > Tools References and look for errors in the window that opens. Search for the correct reference and click it. As others have suggested, consider rewriting the code using some late binding methodologies.
I am relatively new to VBA, and am at a loss as to why I cannot paste into Excel from Word. The following macro ends up pasting the value into Word, even though I think I'm activating the Excel document.
The macro is meant to be run from Word; values from various FormFields then need to be pasted into cells in an existing Excel file.
I searched for a similar issue, though what was returned seemed to be variations of what I am experiencing, and I could not modify those answers to this.
Any help would be appreciated.
Sub Transfer()
Dim WD As Object
Dim ED As Excel.Application
Dim EDS As Excel.Workbook
Set WD = ActiveDocument
Set ED = CreateObject("excel.application")
ED.Visible = True
Workbooks.Open FileName:= _
"C:\Users\Documents\AppealData.xlsx"
ActiveWorkbook.Activate
Set EDS = ActiveWorkbook
WD.FormFields("AppNum").Copy
EDS.Activate
EDS.Sheets("Sheet1").Range("A1").Select
Selection.Paste
End Sub
Your Selection is referring to the current application. To refer to the Excel application you need to use ED.Selection. But it is a bad idea to rely on Activate and Select anyway.
I suggest you change your code to:
Sub Transfer()
Dim WD As Document
Dim ED As Excel.Application
Dim EDS As Excel.Workbook
Set WD = ActiveDocument
Set ED = CreateObject("excel.application")
ED.Visible = True
'Avoid "Activate"
Set EDS = ED.Workbooks.Open(FileName:= _
"C:\Users\Documents\AppealData.xlsx")
WD.FormFields("AppNum").Copy
'Avoid "Activate" and "Select" and "Selection"
'"Paste" is a worksheet Method, use "PasteSpecial" for a Range
'Use "xlPasteValues" to avoid formatting issues
EDS.Sheets("Sheet1").Range("A1").PasteSpecial Excel.xlPasteValues
End Sub
This here should work for you.
Sub Transfer()
Dim oExcel As Excel.Application
Dim oWB As Workbook
Set oExcel = New Excel.Application
Set oWB = oExcel.Workbooks.Open("C:\Users\Documents\AppealData.xlsx")
oExcel.Visible = True
Workbooks("Book1").Worksheets("Sheet1").Cells(1, 1).Value = _
CStr(Documents("Document1").FormFields("AppNum").Result)
End Sub
The code snippet below is intended to open a specific worksheet of a specific XL file and make it the active worksheet. However, the debug.print gives an output of different sheets. Shouldn't objws.activate make objws the active worksheet?
Dim XLApp As New Excel.Application
Dim ObjXL As Excel.Workbook
Dim ObjWS As Excel.Worksheet
Set ObjXL = XLApp.Workbooks.Open(TargetXL)
Set ObjWS = ObjXL.Worksheets(TargetWS)
ObjWS.Activate
Debug.Print ObjWS.Application.ActiveSheet.Name & "," & ObjWS.Name
Your debug line is printing the name of the active sheet of the application, which may be a sheet in a workbook other than the workbook you have just opened. If you activate that workbook first, then the application's active sheet should become the sheet in that workbook.
I.e.
Dim XLApp As New Excel.Application
Dim ObjXL As Excel.Workbook
Dim ObjWS As Excel.Worksheet
Set ObjXL = XLApp.Workbooks.Open(TargetXL)
Set ObjWS = ObjXL.Worksheets(TargetWS)
ObjXL.Activate ' add this line
ObjWS.Activate
Debug.Print ObjWS.Application.ActiveSheet.Name & "," & ObjWS.Name
You may also want consider whether you really need to activate the worksheet. If the user specifically needs to be viewing the sheet after your VBA has run, then this is a valid reason. If however, you only need to act on the sheet in your code, then you can just reference all it's properties by using the ObjWS variable without actually activating it.
Try Select() instead.