Forum Discussion
kata___
Jan 29, 2021Copper Contributor
VBA: runtime error 1004 : Method range of object - 'Global' failed
Hello everyone, I have this error in my code (red part). I guess it´s a just a little mistake, but I can´t find it. Maybe someone can help me. It is made on CommandButton for sort by two keys and...
HansVogelaar
Jan 29, 2021MVP
Range(cmbx_klic1) refers to the active worksheet. If data is not the active sheet when you run the code, this causes a discrepancy. So try changing Range(cmbx_klic1) to Worksheets("data").Range(cmbx_klic1) and similar for Range(cmbx_klic2)
- kata___Jan 29, 2021Copper Contributor
Thank you, but now there is Run-time error '1004': Application-defined or object-defined error in the same area. Do you possibly have any other ideas where the mistake could be?
- HansVogelaarJan 29, 2021MVP
Does this work?
Private Sub cmdb_razeni_Click() Dim w As Worksheet Dim smer1 As Byte, smer2 As Byte If cmbx_klic1 = cmbx_klic2 Then MsgBox "Oba zadané klí?e jsou shodné. Zvolte odlišné." Exit Sub End If If optb_vzest_1 Then smer1 = xlAscending Else smer1 = xlDescending End If If optb_vzest_2 Then smer2 = xlAscending Else smer2 = xlDescending End If Set w = Worksheets("data") With w.Range("data") If check_razeni = False Then .Sort Key1:=w.Range(cmbx_klic1), Order1:=smer1, Header:=xlYes Else .Sort Key1:=w.Range(cmbx_klic1), Order1:=smer1, _ Key2:=w.Range(cmbx_klic2), Order2:=smer2, Header:=xlYes End If End With frm_menu.Hide End Sub
- kata___Jan 31, 2021Copper Contributor
No, it doesn´t work, there is still Run-time error '1004': Method 'Range' of object'_Worksheet' failed.
Can you think of anything else?