Runtime error '1004' Method 'Rows' of object '_Global' failed - vba

I'm running into a problem with some VBA code. i keep getting
Run-time error '1004' Method 'Rows' of object '_Global' failed
I have been running this tool for a few months now, and have had no issues until this morning. The code where the error is occurring is below, commented at the error point.
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("APPR_IND", "SLG_IND", "C2_IND", "C3_IND", "C4_IND", "T2_IND", "T3_IND", "T4_IND", "SLG_APPR_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 Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(MySheetPath)
'Make sure excel is visible on the screen
'Xl.Visible = True
XlBook.Windows(1).Visible = True
'Windows(wBook).Activate
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
Rows("1:1").Select
Selection.Replace What:="act", Replacement:="aht", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
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

Related

Object issue in VBA

I am starting out with VBA and have encountered issues with the following code. Ultimately I just want to store the row for use later. Can someone assist me please?
Sub UpdateQuote()
Dim wb As Workbook
Dim ws As Worksheet
Dim FoundCell As Range
Dim FoundRow As Range
Dim FindValue As String
Set wb = ActiveWorkbook
Set ws = ActiveSheet
FindValue = Sheet24.Range("D3")
Set FoundCell = Sheet20.Range("A:A").Find(What:=FindValue)
Set FoundRow = FoundCell.Row
Application.ScreenUpdating = False
MsgBox FoundRow
End Sub

Index Match function VBA

I am trying to make index/match work on my VBA programme without any success.
I am trying to Match column A from "comparison" with column "A" from previous extract and return column K. All of this while looping through each row. I've already tested everything and it works fine. The only issue now is to make this Index/Match work.
Sub Delta_Analysis()
Dim wb As Excel.Workbook
Set wb = ThisWorkbook
Dim sheet_refresh_instructions As Excel.Worksheet
Set sheet_refresh_instructions = wb.Worksheets("Refresh instructions")
Dim sheet_previous_extract As Excel.Worksheet
Set sheet_previous_extract = wb.Worksheets("Previous extract")
Dim sheet_current_extract As Excel.Worksheet
Set sheet_current_extract = wb.Worksheets("Current extract")
Dim sheet_comparison As Excel.Worksheet
Set sheet_comparison = wb.Worksheets("Comparison")
Dim sheet_historical_changes As Excel.Worksheet
Set sheet_historical_changes = wb.Worksheets("Historical changes")
'Start manipulating objects
sheet_previous_extract.UsedRange.ClearContents
sheet_current_extract.UsedRange.Copy
sheet_previous_extract.Cells(1, 1).PasteSpecial Paste:=xlPasteValues
sheet_current_extract.UsedRange.ClearContents
sheet_refresh_instructions.Activate
Range("C6").Select
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
Workbooks.Open Filename:= _
"http://jira3.ms.com/jira/sr/jira.issueviews:searchrequest-excel-current-fields/53756/SearchRequest-53756.xls?tempMax=1000"
ActiveWindow.Visible = False
Windows("SearchRequest-53756.xls").Visible = False
Dim wb1 As Excel.Workbook
Set wb1 = Excel.Workbooks("SearchRequest-53756.xls")
Dim sheet_Jira As Excel.Worksheet
Set sheet_Jira = wb1.Worksheets("general_report")
sheet_Jira.Activate
sheet_Jira.Range("1:3").EntireRow.Delete
sheet_Jira.UsedRange.Copy
sheet_current_extract.Cells(1, 1).PasteSpecial Paste:=xlPasteValues
wb1.Close
With sheet_comparison
.Range("A2:K" & .Cells(.Rows.Count, "A").End(xlUp).Row).ClearContents
End With
sheet_current_extract.Range("a2", sheet_current_extract.Range("a2").End(xlDown)).Copy
Sheets("Comparison").Select
Range("A2").Select
ActiveSheet.Paste
Dim i As Integer
i = 2
Do While sheet_previous_extract.Cells(i, 1) <> ""
Cells(i, 2) = Application.WorksheetFunction.Index(sheet_previous_extract.Range("K:K"), Application.WorksheetFunction.Match(sheet_comparison.Range("A:A"), sheet_previous_extract.Range("A:A"), 0))
i = i + 1
Loop
End Sub

excel sheet creation and update

I am looking for a way to create sheets in excel based on a list of cells
problem I have is that I would like the script to check if the list was updated and add the additional sheets and not re create all or delete the old copies
1) is it possible from excel (non VBA)
2) if not the code i have for creating a sheet is :
but it will create new entrys if I re-run (and I am looking for update)
Sub AddSheets()
'Updateby Extendoffice 20161215
Dim xRg As Excel.Range
Dim wSh As Excel.Worksheet
Dim wBk As Excel.Workbook
Set wSh = ActiveSheet
Set wBk = ActiveWorkbook
Application.ScreenUpdating = False
For Each xRg In wSh.Range("A1:A7")
With wBk
.Sheets.Add after:=.Sheets(.Sheets.Count)
On Error Resume Next
ActiveSheet.Name = xRg.Value
If Err.Number = 1004 Then
Debug.Print xRg.Value & " already used as a sheet name"
End If
On Error GoTo 0
End With
Next xRg
Application.ScreenUpdating = True
End Sub
Here's another option. I also added a part where it'll name the sheet the column A value. (You can remove that if needed).
Sub AddSheets()
'Updateby Extendoffice 20161215
Dim xRg As Excel.Range
Dim wSh As Excel.Worksheet
Dim wBk As Excel.Workbook
Set wSh = ActiveSheet
Set wBk = ActiveWorkbook
Application.ScreenUpdating = False
For Each xRg In wSh.Range("A1:A7")
With wBk
If Not sheetExists(xRg.Value) and xRg <> "" Then
.Sheets.Add after:=.Sheets(.Sheets.Count)
ActiveSheet.Name = xRg.Value
End If
End With
Next xRg
Application.ScreenUpdating = True
End Sub
Function sheetExists(sheetToFind As String) As Boolean
'http://stackoverflow.com/a/6040454/4650297
Dim sheet As Worksheet
sheetExists = False
For Each sheet In Worksheets
If sheetToFind = sheet.Name Then
sheetExists = True
Exit Function
End If
Next sheet
End Function
Use this function to check if the worksheet already exists, then let it skip over it.
Function WorksheetExists(sName As String) As Boolean
WorksheetExists = Evaluate("ISREF('" & sName & "'!A1)")
End Function
So your code can be:
Sub AddSheets()
'Updateby Extendoffice 20161215
Dim xRg As Variant
Dim wSh As Excel.Worksheet
Dim wBk As Excel.Workbook
Set wSh = ActiveSheet
Set wBk = ActiveWorkbook
Application.ScreenUpdating = False
For Each xRg In wSh.Range("A1:A7")
If Not IsError(xRg) Then
If xRg <> "" Then
If Not WorkSheetExists((xRg)) Then
With wBk
.Sheets.Add after:=.Sheets(.Sheets.Count)
ActiveSheet.Name = xRg.Value
End With
End If
End If
End If
Next xRg
Application.ScreenUpdating = True
End Sub
Function WorksheetExists(sName As String) As Boolean
WorksheetExists = Evaluate("ISREF('" & sName & "'!A1)")
End Function

Modifying a closed excel sheet with VBA

I am trying to open one spredsheet from another so that it isn't in view. I would then like to change the column format of one of the columns to date using the TextToColumns feature. The changes should then be saved and the file closed automatically.
When I run the following it says No data was selected to parse. Any thoughts?
Sub Test()
Dim xlApp As New Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
xlApp.Visible = False
Set xlWB = xlApp.Workbooks.Open("directory of file")
Set xlWS = xlWB.Worksheets("Sheet 1")
xlWS.Unprotect
xlWS.Columns("F:F").Select
Selection.TextToColumns Destination:=Range("F1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 4), TrailingMinusNumbers:=True
Set xlWS = Nothing
xlApp.DisplayAlerts = False
xlWB.Close True
Set xlWB = Nothing
xlApp.Quit
Set xlApp = Nothing
End Sub
A bit of research should have given you the result, but I will provide it anyway...
'Since you want the Workbook to be invisible, we have to open it in a new Excel Application
Dim xlApp As New Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
xlApp.Visible = False
'Open Workbook from specified path
Set xlWB = xlApp.Workbooks.Open("YOUR FILEPATH HERE")
'Select Worksheet from opened Workbook
Set xlWS = xlWB.Worksheets("YOUR WORKSHEET NAME HERE")
'Do something
'example
xlWS.Name = "Asdf"
'Cleanup
Set xlWS = Nothing
xlWB.Close 'True to save changes, False to discard changes
Set xlWB = Nothing
xlApp.Quit
Set xlApp = Nothing
This should get you started...
Sub Test()
Dim wbk As Workbook
'Open the workbook that is closed
Set wbk = Workbooks.Open("C:\OtherWorkbook.xlsx")
'Change the format of the first column
wbk.Worksheets(1).Range("A:A").NumberFormat = "0.00"
'Close the workbook and save changes
wbk.Close True
End Sub

Excel hang while copying data from one workbook to another

The Excel hang if the user click the button in the sheet. The button allowed the user to run the following VBA code. If the user runs the code from VBA editor, it's working fine. Kindly help. The code is as the following. I'm trying to copy data from current excel file to the other excel file newly created.
Sub clickBreak()
i = 12
Dim workBookName As String
Dim workBookName2 As String
Dim wb2 As Workbook
Dim wb1 As Workbook
Dim pasteStart As Range
workBookName = Application.ActiveWorkbook.FullName
workBookName2 = Insert(workBookName, "_2", InStr(workBookName, ".xls") - 1) & ".xls"
MsgBox workBookName2
Dim xlobj As Object
Set xlobj = CreateObject("Scripting.FileSystemObject")
xlobj.CopyFile workBookName, workBookName2, True
Set xlobj = Nothing
Set wb1 = Workbooks.Open(Filename:=workBookName)
Set pasteStart = [A12:A15]
wb1.Sheets("contents").Range("A12:A15").Copy
Set wb2 = Workbooks.Open(Filename:=workBookName2)
wb2.Sheets("contents").Range("A12:A:15").PasteSpecial xlPasteAll
wb2.Save
End Sub
clickBreak is not an event handler. If the name of your button is Break you must name the sub
BreaK_Click() for it to act as an event handler for the button click event:
Sub BreaK_Click()
...
End Sub
Full Code:
Sub BreaK_Click()
i = 12
Dim workBookName As String
Dim workBookName2 As String
Dim wb2 As Workbook
Dim wb1 As Workbook
Dim pasteStart As Range
workBookName = Application.ActiveWorkbook.FullName
workBookName2 = Insert(workBookName, "_2", InStr(workBookName, ".xls") - 1) & ".xls"
MsgBox workBookName2
Dim xlobj As Object
Set xlobj = CreateObject("Scripting.FileSystemObject")
xlobj.CopyFile workBookName, workBookName2, True
Set xlobj = Nothing
Set wb1 = Workbooks.Open(Filename:=workBookName)
Set pasteStart = [A12:A15]
wb1.Sheets("contents").Range("A12:A15").Copy
Set wb2 = Workbooks.Open(Filename:=workBookName2)
wb2.Sheets("contents").Range("A12:A:15").PasteSpecial xlPasteAll
wb2.Save
End Sub
I got the answer
Sub clickBreak()
Dim workBookName As String
Dim workBookName2 As String
Dim wbTarget As Workbook
Dim wbThis As Workbook
Dim strName As String
Set wbThis = ActiveWorkbook
strName = ActiveSheet.Name
workBookName = Application.ActiveWorkbook.FullName
workBookName2 = Insert(workBookName, "_2", InStr(workBookName, ".xls") - 1) & ".xls"
Dim xlobj As Object
Set xlobj = CreateObject("Scripting.FileSystemObject")
xlobj.CopyFile workBookName, workBookName2, True
Set xlobj = Nothing
Set wbTarget = Workbooks.Open(workBookName2)
wbTarget.Sheets("contents").Range("A1").Select
wbTarget.Sheets("contents").Range("A12:A15").ClearContents
wbThis.Activate
Application.CutCopyMode = False
wbThis.Sheets("contents").Range("A12:A15").Copy
wbTarget.Sheets("contents").Range("A12:A15").PasteSpecial
Application.CutCopyMode = False
wbTarget.Save
wbTarget.Close
wbThis.Activate
'clear memory
Set wbTarget = Nothing
Set wbThis = Nothing
End Sub
Thanks you for spending time on my question and giving feedback. Sorry for answering my own question, I just want to share my resolution with the other who will be having the same problem.
I got reference from this http://en.kioskea.net/faq/24666-excel-vba-copy-data-to-another-workbook