简体   繁体   中英

Referencing the active range of another workbook vba

Ok, so I am trying to perform a batch interpolation macro on some files in a folder and would like to know how I can refrence the ActiveRange from the .XLSM and feed it back into the for next loop for each selected file.

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

If someone can think of a way to do this it would be great! Any more info needed Please Ask! Tom

Edit:Essentially I want to reference the active range of a 'Master Workbook' and select it in a destination workbook without an absolute reference!

Something along these lines. Note you don't need to Select ranges in order to work with them...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM