Link to home
Start Free TrialLog in
Avatar of JaseSt
JaseStFlag for United States of America

asked on

Import content from a sheet in workbook to a sheet in another new workbook

For the attached spreadsheet (MC Consolidated Example.xlsm) when button 'Push to Book 1' is clicked it imports the rows from that sheet to another (Book1 Example.xls) WHERE the date in Col J in MC Consolidated equals today's date.

From there I manually copy what is in Book 1 and paste it into another sheet and save that sheet as Mastercard - smTODAY'S DATE.xls. I'd rather just skip the import to Book 1 and go directly to the Mastercard - sm worksheet.

Here's what I need 'Push to Book 1' to do instead:

As you can see, the data imported to 'Mastercard - smSept9-11-Example.xls' from 'MC Consolidated' has changed slightly: there are three zeroes added to the Credit Card number and the date from Col J is not imported, but other than that everything is exactly the same.

What I need now instead is for when button 'Push to Book 1' is clicked:

1. open up another workbook
2. import data from MC Consolidated where the date in Col J is today's date
3. add three zeroes to the front of the card number (and NOT allowing the cell to convert to scientific notation as it wants to do.) -FYI: the credit card numbers do NOT have a dash in them. I inserted in order to change the numbers.
4. make sure the imported to Col E has two decimal places and does not round off
5. populate the top row with the colors and columns headings showing in ''Mastercard - smSept9-11-Example.xls
6. save this now newly created spreadsheet with the date from Col J so that if that date were 9/9/2011 the spreadsheet would save as:  'Mastercard - smSept9-11.xls'

added bonus if possible, (can ask another related question for this part if desired):

7. after doing point 6 above, then open a new email message (outlook 2007)
8. add the attachment of this newly created Mastercard - sm spreadsheet to it
9. add to the body of the email message the below text:

Dear Nalleli,

Please see attached file for regular load request.
PIC:99554Freedom

Please let me know you received this email.

Thank you.

Michael

10. insert into the subject line this text:

MC - smTODAY'S DATE (example: MC-smSept10-11)

 MC-Consolidated-Example.xlsm Book1-Example.xls Mastercard---smSept9-11-Example.xls
Avatar of Jacques Geday
Jacques Geday
Flag of Canada image

Here it is.
1) open your present workbook and saveas a new name
2) goto Module2 in VBA make sure the lower left icon is clicked so you can view each Sub alone.
3) Delete the present processCC Sub
4) Select ALL in the below code and right click copy and paste in Module2 after any End Sub
5) Save the workbook
6) display Sheet MC Consolidated
7) Right click on the PushToBook1 button and choose Assign Macro.
8) Select the item that end with: Module2.PushToBook and then press OK
9 Save the workbook and exit
10) start the workbook and try PushToBook1 button.
11) after running if it complete successfully it will save the newly created workbook in the same path as the actual file location under the name that you requested.

Pls verify and advise.
gowflow
Sub processCC(sheet As Worksheet)
   Dim length As Integer, I As Integer
   Dim CC, CCFormat As String
   Dim dbl As Double
   Dim LastRow As Long, Row As Long
   
   LastRow = sheet.Cells(Rows.count, "D").End(xlUp).Row
   sheet.Range("D:D").NumberFormat = 0
   sheet.Range("D:D").NumberFormat = "@"
   For Row = 2 To LastRow
      If sheet.Range("D" & Row).Value <> "" Then
         CC = ""
         length = Len(sheet.Range("D" & Row).Value)
         For I = 1 To length
            If Mid(sheet.Range("D" & Row).Value, I, 1) <> " " Then CC = CC & Mid(sheet.Range("D" & Row).Value, I, 1)
         Next
         If Left(CC, 3) <> "000" Then CC = "000" & Format(CC, "0")
         sheet.Range("D" & Row) = CC
      End If
   Next

End Sub





Sub PushToBook()
Dim WS As Worksheet
Dim WSS As Worksheet
Dim NewWS As Worksheet
Dim MaxRow, I, J As Long
Dim NewWb As Workbook
Dim NewWorkB As String

If MsgBox("This process will create a new workbook with today's date and load in it all records in sheet 'MC Consolidated' that beare today's date." & Chr(10) & Chr(10) _
    & "Are you ready to start this process ?", vbQuestion + vbYesNo, "Push To Book") = vbYes Then

    Set WS = ActiveSheet
    Set NewWb = Workbooks.Add
    Set NewWS = NewWb.Sheets("Sheet1")
    NewWS.Name = Format(Now, "mm-dd-yyyy")
    
    'Mastercard - smSept9-11.xls'
    NewWb.SaveAs Filename:=ThisWorkbook.Path & "\Mastercard - sm" & Format(Now, "Mmmd-yy") & ".xls", FileFormat:=xlExcel8
    NewWorkB = NewWb.Name
    
    J = 1
    MaxRow = WS.UsedRange.Rows.count
    WS.UsedRange.AutoFilter Field:=10, Criteria1:=">=" & Date, Operator:=xlAnd, Criteria2:="<=" & Date
    For I = 1 To MaxRow
        If WS.Range(I & ":" & I).EntireRow.Hidden = False Then
            WS.Range("A" & I & ":I" & I).copy NewWS.Cells(J, 1)
            J = J + 1
        End If
    Next I
    WS.ShowAllData
    WS.AutoFilterMode = False
    
    processCC NewWS
       
    With NewWS.Columns("A:I")
       .EntireColumn.AutoFit
       .HorizontalAlignment = xlCenter
    End With
       
    Application.DisplayAlerts = False
    
    For Each WSS In NewWb.Worksheets
        If WSS.Name <> NewWS.Name Then WSS.Delete
    Next WSS
    
    
    NewWb.Close Savechanges:=True
    
    Set NewWb = Nothing
    Set NewWS = Nothing
    Application.DisplayAlerts = True
    
    WS.Activate
    MsgBox ("Workbook: '" & NewWorkB & "' has been created successfully")
    
End If
End Sub

Open in new window

Avatar of JaseSt

ASKER

Works perfectly, gowflow.

However, I'm not sure I want it to close after saving. I need to look it over to verify everything was imported correctly and that I don't need to edit anything before I send it. So, if you wouldn't mind, please don't have it close after saving.

Also, I'd like it NOT to save in the file you have it saving to. I save this file under a different folder (MasterCardLoadRequests) which can be seen in the attached image.

Any hope of a code opening a new email message and attaching to it as described in points 7-9?  Thank you!

User generated image
Well for saving in a location then diffrent story will have to build a routine to ask where to save and save it in registry this is my style in prgramming. I don't like these Fix path to be built into the code I find it cheap and binding to the user !

Will work on this part and give you the details once available. I kep the (send email w attachment part) to an other question as also here it is a whole process that need to be done correctly and also always user set not Developper IMPOSED on user !

gowflow
ASKER CERTIFIED SOLUTION
Avatar of Jacques Geday
Jacques Geday
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JaseSt

ASKER

Works perfectly, gowflow. Of course I will try it out tomorrow and the next day just to make sure, but I think you got it. Thank you.

Do you want me to give you the other part of this in a related question?
yes sure pls go ahead, although it is taking time of me but I am getting nicely hooked on your project and keep sending questions I don't have a problem. I feel you will end up with something nice. It seems you forgot your earlier wish to post a question for having fix the workbook. If you do so I do not know how to answer that without posting the workbook.
gowflow
Avatar of JaseSt

ASKER

Put in the code and will try it out on my tomorrow morning.
Avatar of JaseSt

ASKER

gowflow, back to this question answered and solved, please note the two attached screenshots. Not sure why MC Consolidated is pulling in another date. It did the same thing yesterday, bringing in a record to MC Consolidated from a date that wasn't today's date. In both cased, the record brought over was the last entry (row) on the page. User generated image User generated image
Is the data coming from an MCR or HMF Account sheet or a regular sheet ?
gowflow
I would presume it is coming from MCR or HMF Account sheet. PLs confirm
gowflow
I am not getting the trace of this question
Avatar of JaseSt

ASKER

Data coming from a MCR sheet
Don't know why it is doing this but yor post shows Asiacom sheet so its the MCR/HMF routine. Anyway do this fix:

1) Make a new SAVE as your latest mastercard workbook and choose a new name
2) goto vba and doubleclick module1 choose to view 1 sub at a time and display the Sub DailyLoadMCRHMF
3) Replace following line in the code
-------------
If Not Row.Hidden And dataSheet.Cells(Row.Row, cashCol) > 0 And Not Row.Row = 1 Then
-------------
By this following line
-------------
If Not Row.Hidden And dataSheet.Cells(Row.Row, cashCol) > 0 And Not Row.Row = 1 And dataSheet.Cells(Row.Row, dateCol).Value = Date Then
-------------

The best way to do it is to comment out the actual line by puting a single quote in the begining of the line ' once you put it the line turn green. then you paste just under it the new line

4) Save the workbook and exit
5) open it and try it.
gowflow
Avatar of JaseSt

ASKER

Yep, that worked! You are a genius. Thank you!
Welcome sorry for not getting it right from the first time !
gowflow