logo elektroda
logo elektroda
X
logo elektroda

[Solved] Title: Macro VBA Error: Method Range of Object_Global Failed - Troubleshooting & Solutions

Martini1005 4695 3
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18820501
    Martini1005
    Level 7  
    Hello,
    The macro returns error method range of object _global failed. Asking for help - what's wrong?
    Code: VB.net
    Log in, to see the code


    Sub copy_nazwy_kolory()
    Dim i As Integer, j As Long, wynik As Long, wiersz As Long, zakres As Variant
    Dim kol As String
    wiersz = ActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count
    zakres = Range("f1:f" & wiersz)
    For i = 1 To UBound(zakres, 2)
    For j = wiersz To wynik + 1 Step -1
    If zakres(j, i) "" Then wynik = j: Exit For
    Next j
    Next i
    kol = Worksheets("Nazwy+kolory").Range("T1").Value
    Sheets("Nazwy+kolory").Select 'dodawanie zakresu
    Range("f11:" & kol & wynik).Select ' zmiana zakresu kolumn do skopiowania
    Selection.Copy
    Sheets.Add 'dodawanie arkusza
    ActiveSheet.Name = "nowy"
    Sheets("nowy").Select
    Range("A2").Select 'wklejanie
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Application.CutCopyMode = False
    Sheets("Nazwy+kolory").Select

    End Sub
    Code: VB.net
    Log in, to see the code
  • ADVERTISEMENT
  • Helpful post
    #2 18820755
    clubs
    Level 38  
    I do not go into what is going to be going on there but you have a wrong declaration 'Dim kol As String' as it is 'long'
    and an error in the syntax 'Range ("f11:" &' as already 'Range ("f11: f" &'
  • ADVERTISEMENT
  • Helpful post
    #3 18821114
    Maciej Gonet
    VBA, Excel specialist
    The cause of the error is the result of a mis-declaration or misuse of the col variable. It is declared as String (text), and in the text of the procedure, its value is read from the T1 cell, which contains the numeric value 50. Then the following is used:
    Code: VBScript
    Log in, to see the code
    which, after substituting the values of the variables, gives the result
    Code: VBScript
    Log in, to see the code
    which makes no sense and generates an error. The question is, what does this variable col mean? If the column number, it would be the AX column, which does not stand out in any particular way in the sheet. So what exactly is going to be copied to this new sheet?
  • #4 18821654
    Martini1005
    Level 7  
    Problem solved. A variable that lists the penultimate column (and therefore the range to be copied) should be taken from the V1 field, not T1. Thanks for the help.
ADVERTISEMENT