Method 'Range of object '_Global' failed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bpremlatha
    New Member
    • Nov 2014
    • 7

    Method 'Range of object '_Global' failed

    Code:
    Sub WorkBook_Open()
    RemoveEmptyRow
    ConcatenateColumn
    DeleteBlankColumns
    SaveFile
    End Sub
    
    Sub SaveFile()
    'ActiveWorkbook.Save
    ThisWorkbook.Saved = True
    Application.Quit
    Application.ActiveWindow.Close SaveChanges:=True
    ActiveWorkbook.Close SaveChanges:=True
    'ActiveWorkbook.SaveCopyAs ("D:\save\" & Format(Now, "ddmmyy") & ".xls")
    'ActiveWorkbook.SaveAs filename:="D:\RESULT\" & Format(Now, "ddmmyy") & ".xls", FileFormat:=xlCurrentPlatformText, CreateBackup:=False
    'ActiveWorkbook.SaveAs Filename:="D:\RESULT\Text.txt", FileFormat:=xlCurrentPlatformText, CreateBackup:=False
    End Sub
    
    Sub RemoveEmptyRow()
    Dim i As Long
        Dim DelRange As Range
        On Error GoTo Whoa
        Application.ScreenUpdating = False
        'Path = ThisWorkbook.Path
        'Path = ActiveWorkbook.Path
        For i = 1 To 1500
            If Application.WorksheetFunction.CountA(Range("A" & i & ":" & "DG" & i)) = 0 Then
                If DelRange Is Nothing Then
                    Set DelRange = Rows(i)
                Else
                    Set DelRange = Union(DelRange, Rows(i))
                End If
            End If
        Next i
        If Not DelRange Is Nothing Then DelRange.Delete Shift:=xlUp
    LetsContinue:
        Application.ScreenUpdating = True
        Exit Sub
    Whoa:
        MsgBox Err.Description
        Resume LetsContinue
     ActiveWorkbook.Save
    End Sub
    
    Sub ConcatenateColumn()
     'Path = ActiveWorkbook.Path
     'For i = A To Cells(Rows.Count, "BE").End(xlUp).Row
    For i = 1 To Cells(Rows.Count, "BE").End(xlUp).Row
           Cells(i, "BE").Value = Cells(i, "BE").Value & Cells(i, "BF").Value
    Next i
    For i = 1 To Cells(Rows.Count, "AE").End(xlUp).Row
           Cells(i, "AE").Value = Cells(i, "AE").Value & Cells(i, "AF").Value & Cells(i, "AG").Value
    Next i
    For i = 1 To Cells(Rows.Count, "G").End(xlUp).Row
           Cells(i, "G").Value = Cells(i, "G").Value & Cells(i, "H").Value
    Next i
    For i = 1 To Cells(Rows.Count, "K").End(xlUp).Row
            Cells(i, "K").Value = Cells(i, "K").Value & Cells(i, "L").Value
    Next i
    For i = 1 To Cells(Rows.Count, "M").End(xlUp).Row
            Cells(i, "M").Value = Cells(i, "M").Value & Cells(i, "N").Value
    Next i
     For i = 1 To Cells(Rows.Count, "AI").End(xlUp).Row
            Cells(i, "AI").Value = Cells(i, "AI").Value & Cells(i, "AJ").Value & Cells(i, "AK") & Cells(i, "AL")
    Next i
     For i = 1 To Cells(Rows.Count, "AM").End(xlUp).Row
             Cells(i, "AM").Value = Cells(i, "AM").Value & Cells(i, "AN").Value & Cells(i, "AO").Value
    Next i
     For i = 1 To Cells(Rows.Count, "AP").End(xlUp).Row
             Cells(i, "AP").Value = Cells(i, "AP").Value & Cells(i, "AQ").Value & Cells(i, "AR").Value
    Next i
     For i = 1 To Cells(Rows.Count, "AS").End(xlUp).Row
             Cells(i, "AS").Value = Cells(i, "AS").Value & Cells(i, "AT").Value
    Next i
     For i = 1 To Cells(Rows.Count, "AV").End(xlUp).Row
             Cells(i, "AV").Value = Cells(i, "AV").Value & Cells(i, "AW").Value
    Next i
     For i = 1 To Cells(Rows.Count, "BA").End(xlUp).Row
             Cells(i, "BA").Value = Cells(i, "BA").Value & Cells(i, "BB").Value & Cells(i, "BC").Value
    Next i
     For i = 1 To Cells(Rows.Count, "BL").End(xlUp).Row
    Cells(i, "BL").Value = Cells(i, "BL").Value & Cells(i, "BM").Value
    Next i
     For i = 1 To Cells(Rows.Count, "BP").End(xlUp).Row
    Cells(i, "BP").Value = Cells(i, "BP").Value & Cells(i, "BQ").Value
    Next i
     For i = 1 To Cells(Rows.Count, "BS").End(xlUp).Row
    Cells(i, "BS").Value = Cells(i, "BS").Value & Cells(i, "BT").Value
    Next i
    For i = 1 To Cells(Rows.Count, "BS").End(xlUp).Row
    Cells(i, "BS").Value = Cells(i, "BS").Value & Cells(i, "BT").Value
    Next i
    For i = 1 To Cells(Rows.Count, "BW").End(xlUp).Row
    Cells(i, "BW").Value = Cells(i, "BW").Value & Cells(i, "BX").Value
    Next i
    For i = 1 To Cells(Rows.Count, "CA").End(xlUp).Row
    Cells(i, "CA").Value = Cells(i, "CA").Value & Cells(i, "CB").Value
    Next i
    For i = 1 To Cells(Rows.Count, "CH").End(xlUp).Row
       Cells(i, "CH").Value = Cells(i, "CH").Value & Cells(i, "CI").Value & Cells(i, "CJ").Value
    Next i
    For i = 1 To Cells(Rows.Count, "CL").End(xlUp).Row
    Cells(i, "CL").Value = Cells(i, "CL").Value & Cells(i, "CM").Value
    Next i
    For i = 1 To Cells(Rows.Count, "CO").End(xlUp).Row
    Cells(i, "CO").Value = Cells(i, "CO").Value & Cells(i, "CP").Value
    Next i
    For i = 1 To Cells(Rows.Count, "CR").End(xlUp).Row
    Cells(i, "CR").Value = Cells(i, "CR").Value & Cells(i, "CS").Value
    Next i
    For i = 1 To Cells(Rows.Count, "CT").End(xlUp).Row
    Cells(i, "CT").Value = Cells(i, "CT").Value & Cells(i, "CU").Value
    Next i
    For i = 1 To Cells(Rows.Count, "CV").End(xlUp).Row
    Next i
    End Sub
    
    Sub DeleteBlankColumns()
     'Path = ActiveWorkbook.Path
     'Declaring the variable lColumn as long to store the last Column number
    Dim lColumn As Long
    'Declaring the variable iCntr as long to use in the For loop
    Dim iCntr As Long
    'Assigning the last Column value to the variable lColumn
    lColumn = 111
    iCntr = 1
    'Using for loop
    'We are checking the each cell value if it cell is 0 (equals to zero value)
    'And deleting the Column if true
    For iCntr = lColumn To 1 Step -1
    If Cells(1, iCntr) = 0 Then
    Columns(iCntr).Delete
    End If
    Next
    ActiveWindow.Visible = True
    End Sub
    I Want to run all script in workbook_open() which will run the macro automatically after open. But, once I open the excel file using bat file, this error coming out.
    "Method 'Range of object'_Global' failed"
    Please help me
  • twinnyfo
    Recognized Expert Moderator Specialist
    • Nov 2011
    • 3658

    #2
    This thread may more properly belong in MS Excel.....

    You have not told us at which line you are receiving this error. I don't see any "_Global" anywhere in your code, which makes me think it the error may be firing from somewhere else. But, I wouldn't know where to start without some more information.

    Comment

    • bpremlatha
      New Member
      • Nov 2014
      • 7

      #3
      The error shown at line 48. I have use personal macro vba. Personal macro script available every-time we open the excel no matter which file. In my case, I have use batch file to run the excel file which contain data. After that, I use
      Code:
      Sub Workbook_Open() 
      
      end sub
      to auto run the script. I have call Sub SaveFile() , Sub RemoveEmptyRow( ), Sub ConcatenateColu mn(), and Sub DeleteBlankColu mns() in Sub Workbook_Open() in order to run the script inside. I use this script to format the file because of empty rows, got some extra column which takes the data from previous column, and got blank column.
      After I open the excel using batch file, the error is shown. Then. I click ok and go to developer, click macro and manually run the Workbook_open() on that data file.
      Thank you for your response. Sorry for unclear question. Ask anything that you want to know to help me. I will answer. Please help.

      Comment

      • twinnyfo
        Recognized Expert Moderator Specialist
        • Nov 2011
        • 3658

        #4
        Has this code always worked but now has come up with errors? Or, is this new code that you are trying to get to run properly?

        Line 48 is a comment line, and has no meaning for the compiler, so it will not cause the error.

        One possibility for your error may have to do with how you are referring to your cells. The Cells() property requires numeric values, not text. So
        Code:
        Cells(Rows.Count, "BE")
        should probably be
        Code:
        Cells(Rows.Count, 57)
        etc.

        I've made similar mistakes in the past, as Excel referencing is a bit different than Excel VBA referencing.

        Hope this hepps!

        Comment

        Working...