2012-05-01 10 views
12

Questa è una specie di sciocco, ma mi è stato bloccato per un po 'in questa semplice affermazione:Gamma VBA String

Dim range1 as Range 
    Dim mysheet as String 
    Dim myrange as String 

    mysheet = "Sheet1" 
    range = "A1:A10" 

range1 = Worksheets(mysheet).Range(myrange) 

ho testare tutte le soluzioni che ho trovato su internet ad esempio this, this e this, ma nulla.

Per tutto il tempo che mi dà errori: 1004 "Errore definito dall'applicazione" o "variabile oggetto o con non impostato".

Ho provato quanto segue:

range1 = ThisWorkbook.Worksheets(mysheet).Range(myrange) 

range1 = ActiveWorkbook.Worksheets(mysheet).Range(myrange) 

range1 = Sheets(mysheet).Range(myrange) (and the combinations above) 

range1 = Worksheets(mysheet).Range(Cells(1,1), Cells(1,10)) (and the combinations with This/Active workbook) 

e

with This/ActiveWorkbook 
range1 = .Worksheets(mysheet).Range(myrange) 
end with 

Nessuno ha funzionato.

Questo è un fatto stupido, ma sono stato bloccato per un po 'di tempo: s

Qualcuno mi può aiutare?

Davvero grazie in anticipo.

Con i migliori saluti,

risposta

25

è necessario utilizzare Set per assegnare oggetti:

Set range1 = Worksheets(mysheet).Range(myrange) 
+0

Davvero grazie ... sapevo che era davvero semplice, ma ero cieco. –

+1

+1 Bello :-) –