引用另一个工作簿vba的活动范围

时间:2014-02-10 16:39:42

标签: excel vba excel-vba

好的,我正在尝试对文件夹中的某些文件执行批量插值宏,并想知道如何从.XLSM中引用ActiveRange并将其反馈到每个所选文件的for next循环中。

Sub Batch_Interpolate_Blanks()
Dim SaveDriveDir As String
Dim MyPath As String
Dim Fname As Variant
Dim N As Long
Dim FnameInLoop As String
Dim mybook As Workbook
Dim myRange As Range
Dim myRange2 As Range
Dim EntireRange As Range

SaveDriveDir = CurDir  
MyPath = Application.DefaultFilePath
ChDrive MyPath
ChDir MyPath
Fname = Application.GetOpenFilename(Title:="Select a file or files", MultiSelect:=True)
If IsArray(Fname) Then
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

 RangeSelect: Set myRange = Application.InputBox(Prompt:= _
    "Please Select the Column you wish to Interpolate. ", _
    Title:="InputBox", Type:=8)

If myRange Is Nothing Then


Else
    myRange.Select

End If


    For N = LBound(Fname) To UBound(Fname)
        FnameInLoop = Right(Fname(N), Len(Fname(N)) - InStrRev(Fname(N), Application.PathSeparator, , 1))
        If bIsBookOpen(FnameInLoop) = False Then

            Set mybook = Nothing
            On Error Resume Next
            Set mybook = Workbooks.Open(Fname(N))
            On Error GoTo 0
         With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Columns("A").Select

    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
    Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
    :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1)), _
    TrailingMinusNumbers:=True

    'Here is where I think I should reference RangeSelect Somehow!!
    'Something Like Workbooks.("Otherworkbook").Activate then make active range = RangeSelect

    Start = ActiveCell
    EndRow = Range("A" & Rows.Count).End(xlUp).Row


    Do Until ActiveCell.Row = EndRow
    Selection.Offset(1, 0).Select
    'Perform my macro function below etc

如果有人能想到办法做到这一点,那就太好了!需要更多信息请询问! 汤姆

编辑:基本上我想引用“主工作簿”的有效范围,并在没有绝对引用的目标工作簿中选择它!

1 个答案:

答案 0 :(得分:0)

这些方面的东西。请注意,您无需选择范围即可使用它们...

Dim c As Range
'using .Cells(1) in case user selected >1 cell
Set c = mybook.ActiveSheet.Range(myRange.Cells(1).Address())
EndRow = Range("A" & Rows.Count).End(xlUp).Row

Do While c.Row <= EndRow
    c.Offset(1, 0).Select
    'etc....
    Set c = c.Offset(1, 0)
Loop