VBA question involving 2 different workbooks

ummjay

Board Regular
Joined
Oct 1, 2010
Messages
193
Hi!

I have a macro on 1 workbook, where I allow the user to browse the file they want to manipulate, then it should do some vlookups/pivot on that sheet directly, but seems its doing so on my macro workbook.

What's the best way to re-direct the macro to manipulate on the data workbook itself?

VBA Code:
Sub START()
    Dim NewFFN  As Variant
     Dim LastRow As Long
    NewFFN = Application.GetOpenFilename(Title:="Please Select File")
    If NewFFN = False Then
        MsgBox "Macro Terminated Due to No File Selected"
        Exit Sub
    Else
        Workbooks.Open FileName:=NewFFN
    End If
    Application.Calculation = xlCalculationAutomatic   '<- may not be necessary
    
'insert column
With NewFFN

Dim Column As Range
Set Column = Application.Range("C:C")
Column.Insert Shift:=xlShiftToRight, CopyOrigin:=xlFormatFromRightOrBelow

'add name to new column
Range("C1").Value = "Type"

'vlookup
 With ActiveSheet.Activate
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        ActiveSheet.Range("C2:C" & LastRow).Formula = "=VLOOKUP(B2,'[Mapping.xlsx]Sheet1'!A:E,5,FALSE)"
'copy/paste as values.
      ActiveSheet.Range("C2:C" & LastRow).Copy
      ActiveSheet.Range("C2:C" & LastRow).PasteSpecial Paste:=xlPasteValues
      ActiveSheet.Range("A1:J" & LastRow).EntireColumn.AutoFit
'Clear Clipboard
  Application.CutCopyMode = False
End With

'start vlookup function
'Dim ws As Worksheet
'    Dim LastRow As Long
'    Dim TargetRange As Range
'
'    On Error GoTo MyErrorHandler:
'
'    Set ws = Sheets("Sheet1")
'
'    LastRow = ws.Cells(Rows.Count, "X").End(xlUp).Row
'    Set TargetRange = ws.Range("A1:X" & LastRow)
'
'    result = Application.WorksheetFunction.VLookup(Sheets("Sheet1").Range("C2"), TargetRange, 5, False)
'
'    MsgBox result
'
'MyErrorHandler:
'    If Err.Number = 1004 Then
'      MsgBox "Value not found"
'    End If



'START pivot
 ActiveSheet.UsedRange.Select
    ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        Sheets(1).UsedRange).CreatePivotTable TableDestination:="", _
        TableName:="Pivot Summary", DefaultVersion:=xlPivotTableVersion10
    ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
    ActiveSheet.Cells(3, 1).Select
'Insert PIVOT Fields
  With ActiveSheet.PivotTables("Pivot Summary")
    .PivotFields("CType").Orientation = xlRowField
    .PivotFields("CType").Position = 1
    .PivotFields("Product").Orientation = xlRowField
    .PivotFields("Product").Position = 2
    .PivotFields("Side").Orientation = xlRowField
    .PivotFields("Side").Position = 3
  End With
'Insert Qty column to the data field
  With ActiveSheet.PivotTables("Pivot Summary").PivotFields("Volume")
    .Orientation = xlDataField
    .Position = 1
  .NumberFormat = "#,##0;(#,##0)"

  ActiveWorkbook.RefreshAll
  End With
  End With
  
Application.DisplayAlerts = True
Application.ScreenUpdating = True
  
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
What I usually do is declare a few workbook variables, i.e.
VBA Code:
Dim macroWB as Workbook
Dim fileWB as Workbook

Then, at the very top of my procedure after all my variable declarations, the first line of code I run "captures" my Macro Workbook in an object variable, i.e.
VBA Code:
Set macroWB = ActiveWorkbook

Then, right after the line where I open the new workbook, I run a line of code to run to "capture" that Workbook in an object variable, i.e.
VBA Code:
Workbooks.Open FileName:=NewFFN
Set fileWB = ActiveWorkbook

So now that I have captured each workbook in its own object variable, I can easily (and dynamically) bounce back between each workbook with code like:
VBA Code:
macroWB.Activate
and
VBA Code:
fileWB.Activate
 
Upvote 0
Solution

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top