Access/Excel VBA Failing - excel

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.

Related

Does setting Application.ScreenUpdating to False applies to every workbook that has subsequently been opened within a subroutine?

OS: Windows 10 Pro Version 20H2 (OS Build 19042.1052)
Application: Microsoft 365 Excel Version 2106 (Build 14131.20278)
VBA: Microsoft Visual Basic for Applications 7.1 Version 1110
The module that contains the code resides in a Macro-enabled workbook, which opens other workbooks in a folder and modify each if it passes certain criteria. The workbook itself that contains the code is not being altered, it just holds the code.
I'm wondering if I need to turn Application.ScreenUpdating off and on for every subsequent workbook that will be opened within a subroutine, such as in this one:
Option Explicit
Sub Test_Code()
'Variable declaration
Dim fsoLib As Scripting.FileSystemObject
Dim strSrcPath As String
Dim fsoFolder As Scripting.Folder
Dim fsoFile As Scripting.File
Dim fsoFiles As Scripting.Files
Dim wb As Excel.Workbook
Dim wbs As Excel.Workbooks
'Assign values to variables
Set fsoLib = New Scripting.FileSystemObject
strSrcPath = "/SourcePath"
Set fsoFolder = fsoLib.GetFolder(strSrcPath)
Set fsoFiles = fsoFolder.Files
Set wbs = Excel.Workbooks
'Modify workbooks
For Each fsoFile In fsoFiles
Set wb = wbs.Open(fsoFile)
Application.ScreenUpdating = False 'TURNS OFF SCREEN UPDATING
'Code to modify workbooks here..
Application.ScreenUpdating = True 'TURNS SCREEN UPDATING BACK ON
wb.Close(True)
Next
'Housekeeping
Set fsoLib = Nothing
strSrcPath = vbEmpty
Set fsoFolder = Nothing
Set fsoFile = Nothing
Set fsoFiles = Nothing
Set wb = Nothing
Set wbs = Nothing
'Success indicator
Debug.Print "You have reached the end of the line.."
End Sub
..or do I just place it on top and bottom, respectively? Like in this one:
Option Explicit
Sub Test_Code()
Application.ScreenUpdating = False
'Rest of the code here..
Application.ScreenUpdating = True
End Sub
I couldn't find any resources about it on the internet that's current. I found one old discussion here but the OP had mixed results depending on whether it is run from the VBA IDE or otherwise.
I'd appreciate any help from you guys. Thank you all very much.

Create Copy Of Excel Worksheet using Word VBA

With an already-opened workbook, I want to create a copy of a worksheet ("Template") and re-name it with the value I have in an array. When I debug the code the copy is created when I execute the line but I still get an error 424: object required.
I've tried On Error Resume Next but since I already used On Error GoTo in my sub it isn't read.
Dim oXL As Excel.Application 'Requires loading "Microsoft Excel 16.0 Object Library" from Tools -> References
Dim oWB As Excel.Workbook
Set oXL = New Excel.Application
Set oWB = oXL.Workbooks.Open(FileName:=WorkbookToWorkOn) 'Opens Excel
oXL.Visible = True 'Shows Excel window while running code. Set to false after finished editing.
Dim ws As Worksheet
For i = LBound(seller_names) To UBound(seller_names)
'On Error Resume Next 'Doesn't work
Set ws = oXL.ActiveWorkbook.Sheets("Template").Copy(After:= _
oXL.ActiveWorkbook.Sheets(oXL.ActiveWorkbook.Sheets.Count))
ws.name = seller_names(i)
Next i
Sheets.Copy doesn't return a reference to the copied sheet, so you need to first copy the sheet, then get a reference to it:
With oXL.ActiveWorkbook
.Sheets("Template").Copy After:= .Sheets(.Sheets.Count)
Set ws = .Sheets(.Sheets.Count)) '<< get the copy
ws.name = seller_names(i)
End With
On Error Resume Next should always work - but is not always a good solution - unless you have "Break on all errors" enabled in your VBA Options.

Using xlpastevalues in Access VBA

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

VBA - Run Macro from Word to paste values into Excel

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

Editing Excel spreadsheats from Word with VBA

How do I edit excel spreadsheets from word using VBA?
First you need to set a reference to the version of Excel you are running. In the VBE go to Tools>References and click Microsoft Excel 12.0 Object Library (12.0 for 2007, 11.0 for 2003) etc.
Then you can code something like this (opens a new instance of Excel, opens, edits and saves a new workbook). You'd use GetObject to access a running instance of Excel:
Sub EditExcelFromWord()
Dim appExcel As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Set appExcel = CreateObject("Excel.Application")
With appExcel
.Visible = True
Set wb = .Workbooks.Add
Set ws = wb.Worksheets(1)
ws.Range("A1").Value2 = "Test"
wb.SaveAs ThisDocument.Path & Application.PathSeparator & "temp.xls"
Stop 'admire your work and then click F5 to continue
Set ws = Nothing
Set wb = Nothing
Set appExcel = Nothing
End With
End Sub

Resources