2012-09-02 40 views
5

Tôi đang đọc một số ô của excel sử dụng VBA.Cách lưu trữ một phạm vi từ excel thành một biến Phạm vi?

Function getData(currentWorksheet as Worksheet, dataStartRow as Integer, _ 
dataEndRow as Integer, DataStartCol as Integer, dataEndCol as Integer) 

    Dim dataTable as Range 
    dataTable = currentWorksheet.Range(currentWorksheet.Cells(dataStartRow, _ 
    dataStartCol), currentWorksheet.Cells(dataEndRow, dataEndCol)) 

    getData = dataTable 

EndFunction 

Nó ném lỗi, biến đối tượng hoặc với biến khối không được đặt. Làm thế nào có phạm vi này trong một biến? Xin vui lòng hướng dẫn cho tôi.

Trả lời

19

Khi bạn sử dụng một đối tượng Range, bạn không thể chỉ cần sử dụng cú pháp sau:

Dim myRange as Range 
myRange = Range("A1") 

Bạn phải sử dụng các từ khóa set gán đối tượng Phạm vi:

Function getData(currentWorksheet As Worksheet, dataStartRow As Integer, dataEndRow As Integer, DataStartCol As Integer, dataEndCol As Integer) 

    Dim dataTable As Range 
    Set dataTable = currentWorksheet.Range(currentWorksheet.Cells(dataStartRow, DataStartCol), currentWorksheet.Cells(dataEndRow, dataEndCol)) 

    Set getData = dataTable 

End Function 

Sub main() 
    Dim test As Range 

    Set test = getData(ActiveSheet, 1, 3, 2, 5) 
    test.select 

End Sub 

Lưu ý rằng tất cả các khoảng thời gian được khai báo tôi sử dụng từ khóa Set.


Bạn cũng có thể cho phép chức năng getData của bạn để trả về một đối tượng Range thay vì một Variant mặc dù điều này không liên quan đến vấn đề bạn đang gặp.

0

currentWorksheet là gì? Nó hoạt động nếu bạn sử dụng được xây dựng trong ActiveSheet.

dataStartRow=1 
dataStartCol=1 
dataEndRow=4 
dataEndCol=4 
Set currentWorksheet=ActiveSheet 
dataTable = currentWorksheet.Range(currentWorksheet.Cells(dataStartRow, dataStartCol), currentWorksheet.Cells(dataEndRow, dataEndCol)) 
+0

các currentWorksheet được thông qua như là một augmenter đến một hàm trong Đó là Sheet2.Nó vẫn còn ném cùng một lỗi mặc dù tôi nói Set currentWorksheet - ActiveSheet –

+0

@PriyankThakkar Hãy cập nhật câu hỏi của bạn để hiển thị * hoàn thành * bối cảnh sau đó.Vì mã tôi đăng không hoạt động (tôi đã thử nghiệm nó –

+0

chắc chắn, sẽ làm điều đó –

0

Xác định GetData là gì. Tại thời điểm nó không được xác định.

0

Khai báo mờ của bạn dưới dạng biến thể và kéo dữ liệu như bạn làm từ một mảng. ví dụ:

Dim y As Variant 
y = Range("A1:B2") 

Bây giờ phạm vi excel của bạn là tất cả 1 biến (mảng), y

Để kéo dữ liệu, gọi vị trí mảng trong phạm vi "A1: B2" hay bất cứ điều gì mà bạn chọn. ví dụ:

Msgbox y(1, 1) 

Điều này sẽ trả lại hộp trên cùng bên trái trong phạm vi "A1: B2".

-1

đây là một ví dụ cho phép để thực hiện mã trên mỗi dòng trong những khu vực mong muốn (chọn một trong hai từ trên & đáy lựa chọn, từ lựa chọn

Sub doROWSb()   'WORKS for do selected rows  SEE FIX ROWS ABOVE (small ver) 
Dim E7 As String 'note: workcell E7 shows: BG381 
E7 = RANGE("E7") 'see eg below 
Dim r As Long  'NOTE: this example has a paste formula(s) down a column(s). WILL REDUCE 10 HOUR DAYS OF PASTING COLUMNS, DOWN TO 3 MINUTES? 
Dim c As Long 
Dim rCell As RANGE 
'Dim LastRow As Long 
r = ActiveCell.row 
c = ActiveCell.Column 'might not matter if your code affects whole line anyways, still leave as is 

Dim FirstRow As Long 'not in use, Delete if only want last row, note: this code already allows for selection as start 
Dim LastRow As Long 


If 1 Then  'if you are unable to delete rows not needed, just change 2 lines from: If 1, to if 0 (to go from selection last row, to all rows down from selection) 
With Selection 
    'FirstRow = .Rows(1).row     'not used here, Delete if only want last row 
    LastRow = .Rows(.Rows.Count).row  'find last row in selection 
End With 
application.CutCopyMode = False    'if not doing any paste op below 
Else 
    LastRow = Cells(Rows.Count, 1).End(xlUp).row 'find last row used in sheet 
End If 
application.EnableEvents = True    'EVENTS need this? 
application.ScreenUpdating = False   'offset-cells(row, col) 
'RANGE(E7).Select 'TOP ROW SELECT 
RANGE("A1") = vbNullString     'simple macros on-off switch, vb not here: If RANGE("A1").Value > 0 Then 


For Each rCell In RANGE(Cells(r, c), Cells(LastRow, c)) 'new 
    rCell.Select 'make 3 macros for each paste macro below 
'your code here: 

If 1 Then  'to if 0, if want to paste formulas/formats/all down a column 
    Selection.EntireRow.Calculate  'calcs all selected rows, even if just selecting 1 cell in each row (might only be doing 1 row aat here, as part of loop) 
Else 
'dorows() DO ROWS() 
'eg's for paste cells down a column, can make 3 separate macros for each: sub alte() altf & altp 
     Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 'make sub alte() add thisworkbook: application.OnKey "%{e}", "alte" 
     'Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False  'make sub altf() add thisworkbook: application.OnKey "%{f}", "altf" 
     'Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False   'amke sub altp() add thisworkbook: application.OnKey "%{p}", "altp" 
End If 
Next rCell 

'application.CutCopyMode = False   'finished - stop copy mode 
'RANGE("A2").Select 
goBEEPS (2), (0.25)  'beeps secs 
application.EnableEvents = True    'EVENTS 

'note: workcell E7 has: SUBSTITUTE(SUBSTITUTE(CELL("address",$BG$369),"$",""),"","") 
'other col eg (shows: BG:BG): =SUBSTITUTE(SUBSTITUTE(CELL("address",$BG2),"$",""),ROW(),"")&":"& SUBSTITUTE(SUBSTITUTE(CELL("address",$BG2),"$",""),ROW(),"") 
End Sub 


'OTHER: 
Sub goBEEPSx(b As Long, t As Double) 'beeps secs as: goBEEPS (2), (0.25) OR: goBEEPS(2, 0.25) 
    Dim dt 'as double 'worked wo as double 
    Dim x 
    For b = b To 1 Step -1 
    Beep 
    x = Timer 
    Do 
    DoEvents 
    dt = Timer - x 
    If dt < 0 Then dt = dt + 86400 '86400 no. seconds in a day, in case hit midnight & timer went down to 0 
    Loop Until dt >= t 
    Next 
End Sub 
Các vấn đề liên quan