Copying selected rows ad keeping all formatting

DaveUK

Board Regular
Joined
Jan 24, 2005
Messages
245
I have a workbook "Book1" which opens a second workbook with this code:

Code:
option explicit

Global MainWorkBook As Workbook
Global DailySchedules As Workbook

Sub OpenDailyScheduleWorkbook()

Dim Filename As String
Dim response As String

Set MainWorkBook = ActiveWorkbook

Application.StatusBar = "IMPORTING WORKBOOK DATA ...PLEASE WAIT..."

    With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    End With


    Filename = Application.GetOpenFilename(FileFilter:="Excel Files,*.xls", Title:="Please choose Workbook to open")
    If Not Chk(Filename) Then
    'If Filename = "False" Then
    response = MsgBox("No file selected" & Chr(10) & "procedure cancelled!", 48, "Error")
    Exit Sub
  
  Else: Set DailySchedules = Workbooks.Open(Filename)
  End If
  
  Application.DisplayAlerts = False
  
     
 ' CODE NEEDED HERE !!


 DailySchedules.Close False
 
    
       
   Application.StatusBar = "Ready"

    With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
    End With
    
End Sub

Function Chk(myFile As String) As Boolean
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Chk = fso.FileExists(myFile)
Set fso = Nothing
End Function


What i need to do is copy all rows from a worksheet on "DailySchedules" called "Works". I need to copy all rows that contain tomorrows date in column F of this worksheet (formatted as "d-mmm-yyyy").
I need to copy all these cells to a worksheet on "MainWorkBook" called "Sheet1", starting at cell "A1". I also want to keep all formatting of everything copied.

The data on worksheet "Works" in workbook "DailySchedules" starts at row 4 (as there is a header on the first 3 rows) and can be an unknown nuber of rows down.

I have indicated in the code above where i need the code to be inserted but do not know how to do what is required.

TIA
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
How about something like this?:

Code:
Dim cell As Range

    For Each cell In Workbooks("DailySchedules.xls").Worksheets("Works").Range("F:F").SpecialCells(xlCellTypeConstants, xlNumbers)
        If cell.Value = Date + 1 Then
            cell.EntireRow.Copy
            Workbooks("MainWorkbook.xls").Worksheets("Sheet1").Range("F" & Rows.Count).End(xlUp).Offset(1, -5).PasteSpecial
        End If
    Next cell
    Workbooks("MainWorkbook.xls").Worksheets("Sheet1").Range("A1").EntireRow.Delete xlUp
    Application.CutCopyMode = False

Try it out on a copy and see if that does what you want. HTH
 
Upvote 0
Thanks for replying.

I am getting a type mismatch error.

The workbooks are not called ("MainWorkbook.xls") or ("DailySchedules.xls"). They are referred to by the names "DailySchedules" and "MainWorkbook" using the SET command.

eg .

Code:
Set MainWorkBook = ActiveWorkbook 

'...

Set DailySchedules = Workbooks.Open(Filename)

How do i work around this problem??

TIA
 
Upvote 0
Use:

Code:
Workbooks(DailySchedules.Name)
instead of

Code:
Workbooks("DailySchedules.xls")

Hope that helps!

Edit: Typo
 
Upvote 0

Forum statistics

Threads
1,224,416
Messages
6,178,504
Members
452,853
Latest member
philipnjk64

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