Macro Help

sarfarazbutt

Board Regular
Joined
Jun 10, 2009
Messages
55
I have two separate files in excel, FILE(A) contain all the data and FILE(B) is summary file, I would like create macro in file(B) and this macro open file(A) insert one row empty in line 1 and save and return back to File(A).

I would love if this macro ask the path of File(B) as may be need to change the folder.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try this:

Rich (BB code):
Public Sub GetFileB()
  Dim strFileB As String
  Dim wkbFileB As Excel.Workbook
  Dim wshFileB As Excel.Worksheet

  With Application.FileDialog(Office.MsoFileDialogType.msoFileDialogFilePicker)
    .AllowMultiSelect = False
    With .Filters
      .Clear
      .Add "Excel Files", "*.xl*"
    End With
    If .Show = False Then
      Exit Sub
    End If
    strFileB = .SelectedItems(1)
  End With

  On Error Resume Next
  Set wkbFileB = Application.Workbooks.Open(strFileB)
  On Error GoTo 0
  
  If wkbFileB Is Nothing Then
    Call MsgBox("Error opening file" & strFileB, vbOKOnly + vbExclamation)
    Exit Sub
  End If
  
  Set wshFileB = wkbFileB.Worksheets(1) 'change 1 to "SheetName"
  wshFileB.Rows(1).Insert Excel.xlShiftDown
  wkbFileB.Save
  wkbFileB.Close False
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,740
Members
452,940
Latest member
Lawrenceiow

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