VBA excel comp ability mode issue - vba

Good afternoon everyone. I create a first ever Macro with some of your help and it worked fine until I tested with actual source file that comes from Service-Now report and the only option there .XLS so when I open Source file in Excel 2013 it open in Compatibility Mode and macro give me 'Run time error '9' "Subscript out of range". What should I do to make it work?
Sub HELLO()
Dim x As Workbook
Sheets("Sheet1").Cells.Clear
'## Open workbook first:
Set x = Workbooks.Open("C:\Users\500722\Desktop\dashboard\task.xls")
'Now, transfer values from x to y:
Sheet1.Cells(1, 1) = x.Sheets("Sheet1").Range("A1")
With x.Sheets("Sheet1").UsedRange
'Now, paste to y worksheet:
Sheet1.Range("A1").Resize( _
.Rows.Count, .Columns.Count) = .Value
End With
x.Close
End Sub

As you don't have a worksheet called "Sheet1" in the workbook you opened, your code will fail when you try to access that sheet.
Change all occurrences of x.Sheets("Sheet1") to x.Sheets("Page1") and your problem will probably go away.

Related

Copy from a closed workbook to an open workbook

I'm trying to create code to copy from an unopened excel workbook to an open book.
This is the code I've been using:
Sub foo()
Dim x As Workbook
Dim y As Workbook
Set x = Workbooks.Open("R:\Manufacturing\First Off Log\First Off Log.xlsm", ReadOnly:=True)
Set y = Workbooks.Open("R:\Manufacturing\First Off Log\Analysis\First Off Log Analysis.xlsm")
x.Sheets("Sheet1").Range("A:K").Copy
y.Sheets("Data Input").Range("A:K").PasteSpecial
'Close x:
x.Close
End Sub
'First Off Log Analysis' will already be open. The code above reopens the workbook and causes it to crash!
Any help would be really appreciated!
Thank you! :-)
Concerning that First Off Log.xlsm is the workbook where the code is placed, it is opened already. Thus instead of:
Set x = Workbooks.Open("R:\Manufacturing\First Off Log\First Off Log.xlsm", ReadOnly:=True)
write
Set x = ThisWorkbook

Copy Data from Other sheet to current open Sheet?

I am trying to copy data from one excel to another and then refreshing all the pivot tables in open sheet. facing issue here that below Macro telling me to open the sheet always where i need to copy my data. please help me as i dont want to open again my second file.
Sub UReport()
'
' UReport Macro
'
' Keyboard Shortcut: Ctrl+Shift+T
'
Dim x As Workbook
Dim y As Workbook
'## Open both workbooks first:
Set x = Workbooks.Open("C:\Reports\HC Report.xlsx")
Set y = Workbooks.Open("C:\Reports\Main Tracker.xlsx")
'Now, copy what you want from x:
x.Sheets("HC Report").Range("A2:FI7004").Copy
'Now, paste to y worksheet:
y.Sheets("My Data").Range("A2:FI7004").PasteSpecial
Sheets("Dashboard").Select
ActiveWorkbook.RefreshAll
Sheets("My Data").Select
'Close x:
x.Close
End Sub
Sheets("Dashboard").Select
ActiveWorkbook.RefreshAll
Sheets("My Data").Select
This is ambiguous. The ActiveWorkbook is y. If the sheet "Dashboard" doesn't exist in y then you will have an error. If it's this problem then use :
x.Sheets("Dashboard").Select
Just as you did previously with the other sheets !
By the way, if you don't want any screen updating as file opening can be long (and maybe cause errors), use
Application.ScreenUpdating = false 'at the beginning of sub
' code here
Application.ScreenUpdating = true ' at the end of sub

copying ranges from workbooks (without folder path version)

I've recently been looking for ways to speed up copying data from one worksheet to another. And I came across this nice piece of code (however this was posted in 2013).
Could you please help? I don't want to specify any path to workbooks (like in the example below). I have both worksheets open and would like to address them by filename.
I've tried changing "workbooks.open" to "window("xxx").activate" but that doesn't work.
thank you!
Sub foo()
Dim x As Workbook
Dim y As Workbook
'## Open both workbooks first:
Set x = Workbooks.Open(" path to copying book ")
Set y = Workbooks.Open(" path to destination book ")
x.Sheets("name of copying sheet").Range("A1").Copy
y.Sheets("sheetname").Range("A1").PasteSpecial
End Sub
Sub foo()
Dim x As Workbook
Dim y As Workbook
'Replace the text between the "" with the exact name of the workbook
Set x = Workbooks("ActualNameOfWorkBook.xls")
Set y = Workbooks("ActualNameOfOtherWorkBook.xls")
x.Sheets("name of copying sheet").Range("A1").Copy
y.Sheets("sheetname").Range("A1").PasteSpecial
End Sub
When using PasteSpecial you need to add the XlPasteTypewhat (what parameter/s from the copied range you want to use). Some options of XlPasteTypewhat are: xlPasteAll , xlPasteFormulas, xlPasteValues etc.
You can read more about it at MSDN.
In the example below I am using xlPasteAll.
Code
Sub foo()
Dim x As Workbook
Dim y As Workbook
'## Open both workbooks first:
Set x = Workbooks.Open("file_name_x.xslx") '<-- don;t forget to add the extension, .xslx or .xlsm
Set y = Workbooks.Open("file_name_y.xslx") '<-- don;t forget to add the extension, .xslx or .xlsm
x.Sheets("name of copying sheet").Range("A1").Copy
y.Sheets("sheetname").Range("A1").PasteSpecial xlPasteAll '<-- add parameter after the PasteSpecial
End Sub

Macro stops after the first row , automation error

I was writing a macro and when I run the program, it runs fine when reading the first row but when it loops around and does the second row, I get an error, saids automation problem and the macro quits. I was wondering what's going on that it works fine for the first loop but not the second.
Basically, what I want the macro to do it read rows 8 - 25, if the cell has a date in cell (i) (i being 8, 9, 10, etc..), column B then copy row and paste it to another workbook.
Any body have any ideas? thanks! :)
Sub Update()
Dim Request As Workbook
Dim blank As Worksheet
Dim oakfield As Workbook
Set Request = Workbooks("Request_Microbiological_Analysis(blank).xlsm")
Set blank = Request.Worksheets("blank")
Set oakfield = Workbooks.Open("O:\_Public\Quality_Oakfield.xlsm")
With ThisWorkbook
Dim i As Long
For i = 8 To 25
If IsDate(Cells(i, 2)) Then
blank.Cells(i, "A").Resize(, 12).Copy
oakfield.Worksheets("Microlog").Range("A" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial xlPasteValuesAndNumberFormats
ActiveWorkbook.Save
ActiveWorkbook.Close
ElseIf IsEmpty(Cells(i, 2)) Then
MsgBox "Oakfield Quality Updated"
End If
Next i
End With
MsgBox "Quality System Updated"
End Sub
Your issue here appears to be due to the fact that you close ActiveWorkbook in your loop, but then don't open it again. An automation error typically occurs when a workbook object is being referenced when it isn't open. You need to wait until after the loop to close your workbook.

Using Excel VBA, how do I keep my original code executing after a 'thisworkbook.close' event in a 2nd workbook?

In Excel 2007, I have a sheet with a list of other Excel documents, all of which have their own VBA. My code opens the first workbook on the list, lets its vba run, then when it has completed, marks it as complete, and opens the next workbook in the list.
All this works fine unless I let one of the other workboks close itself with 'thisworkbook.close'. This stops the VBA running in the original workbook, as well as itself. If I comment this line out, it all works, but I would rather keep just the master workbook and one sub workbook open at one time.
Also, it is unpractical in this case to move all the VBA to the master workbook.
The code before is a highly simplified version to show the issue:
Workbook1's code:
Sub RunReports()
Dim wkb1 As Workbook
Dim wks1 As Worksheet
Dim lngR As Long
Dim strReport As String
Set wkb1 = ThisWorkbook
Set wks1 = wkb1.Sheets(strDay)
For lngR = 4 To 1048576
strReport = wks1.Cells(lngR, 1).Value
'open the report. Its own VBA will take care of everything else
Workbooks.Open strReport
'mark the report as complete
wks1.Cells(lngR, 2).Value = "done"
Next lngR
End Sub
the code in the worksheets that are opened:
Private Sub Workbook_Open()
ThisWorkbook.Sheets("Sheet1").Cells(1, 1).Value = Now()
ThisWorkbook.Save
Application.Wait (Now() + TimeValue("00:00:05"))
ThisWorkbook.Close
End Sub
If I comment out 'thisworkbook.close', it will open them all, update the time they were opened, and save them. If not, it does everything up to the first 'thisworkbook.close', closes the first sub workbook, and stops all VBA execution.
Does anyone have any ideas how to keep the "master" workbook's vba code running after the "sub" workbook's code has finished, when the 'sub' workbook's code contains a 'thisworkbook.close' (edited to make the question clear)
Use standard COM ways of doing things. Take a reference to each workbook (not excel.application) using GetObject(filename). Do what you want then don't close it in sub book, but set the reference to nothing in your master (which happens when you do set exceldoc = nothing or reach an End Function/Sub). Don't do both as that's voodoo programming.