![]() |
|
|
|
#1
|
|||
|
|||
|
I have an Excel source worksheet generated daily with filename SearchResultsDailymm.dd.yy where mm.dd.yy is the current date. I need code that will copy Columns A2:J of the source worksheet into Columns A2:J of a template worksheet with filename Daily Plan Status mm.dd.yy. The code would be housed in the target file. Thank you. Last edited by kevinbradley57; 04-19-2018 at 09:25 PM. Reason: Fixed typo |
|
#2
|
|||
|
|||
|
Care to make things simpler for anyone willing to assist by posting a couple of workbooks indicative of what you'd be working with, private/personal data removed of course.
|
|
#3
|
|||
|
|||
|
In addition to copying the data from the source file to the template, I need to fill any blank cells in column E with the value in column F of the same row. It works when I run it as a separate macro, but not when I embed it into the main macro.
|
|
#4
|
|||
|
|||
|
This of any help?
You could apply the required "adjustments" after this. Code:
Sub GetPlanStatus()
Dim wb As Workbook, src As Workbook
Dim sht As Worksheet, cel As Range
Set sht = ThisWorkbook.ActiveSheet
'remove existing data
With sht.UsedRange.Offset(1)
.Borders.LineStyle = xlNone
.ClearContents
End With
'see if source is open
For Each wb In Application.Workbooks
If wb.Name = "SearchResultsDaily " & Format(Date, "m.dd.yy") & ".xls" Then
Set src = wb
Exit For
End If
Next wb
'if yes copy data
If Not src Is Nothing Then
src.Sheets(1).UsedRange.Offset(1).Copy
'if no display message and quit
Else
MsgBox "Workbook " & Chr(34) & "SearchResultsDaily " & _
Format(Date, "m.dd.yy") & ".xls" & Chr(34) & " is not open."
Exit Sub
End If
'paste the copied data
With sht
.Cells(2, 1).PasteSpecial (xlPasteValues)
'name sheet
.Name = Left(src.Name, Len(src.Name) - 4)
' E from F if blank
For Each cel In Intersect(.Columns("E"), .UsedRange)
If cel.Value = "" Then cel.Value = cel.Offset(, 1).Value
Next cel
'position the cursor
.Cells(1).Select
End With
'stop the marching ants
Application.CutCopyMode = False
End Sub
|
|
#5
|
|||
|
|||
|
NoSparks -- That works great! Can you add code that will resort the spreadsheet by column E?
|
|
#6
|
|||
|
|||
|
Sure...
Don't know if you want ascending or descending but you can easily alter that. Change the bottom part of the code where the cursor gets positioned. Code:
'position the cursor for sorting
.Cells(2, 5).Select
'determine last row
lr = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
With .Sort
.SortFields.Clear
.SortFields.Add Key:=Range("E2"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortTextAsNumbers
.SetRange Range("A2:J" & lr)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
'stop the marching ants
Application.CutCopyMode = False
End Sub
|
|
#7
|
|||
|
|||
|
Awesome. Thank you, NoSparks.
|
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Setting a Worksheet Name as a Variable Help | rod147 | Excel Programming | 3 | 11-25-2016 08:50 AM |
Date picker content control to always show current date.
|
lucky16 | Word VBA | 2 | 07-01-2016 01:14 PM |
| Variable to rename a tab in a worksheet via VBA | Chayes | Excel Programming | 5 | 08-07-2012 02:03 AM |
Meaning of current date vs status date when saving baselines
|
ketanco | Project | 1 | 02-08-2012 02:20 PM |
How to call current PC date and/or current PC year
|
KIM SOLIS | Excel | 2 | 11-04-2011 06:09 PM |