+ Reply to Thread
Results 1 to 47 of 47

VBA Concatenate problem

Hybrid View

  1. #1
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    VBA Concatenate problem

    Hi,

    I have a few different worksheets I have been working on for a while, but I am just adding some new functionality in for emailing that I have been working on. My problem is that before it created the output file by copying the data in to a temp worksheet then copy the temp worksheet to a new workbook.

    The way I do this now is by creating a new workbook straight away, avoiding the need to create a temp worksheet.

    This is probably me just being daft, but I have an odd field on the output that concatenates a few fields.. Below is an example of my original code.


    Set wsData = Sheets("OUTPUT LIST")
    Set wsCSV = Worksheets.Add(After:=Sheets(Sheets.Count))
    
    With wsData
    
        wsCSV.Range("A1") = "EMP_ID"
        .Range("EmpRng").Copy wsCSV.Range("A2")
        
        wsCSV.Range("B1") = "ID_UNITS"
        .Range("HrsRng").Copy wsCSV.Range("B2")
    
        wsCSV.Range("C1") = "ID_RATE"
        .Range("RateRng").Copy wsCSV.Range("C2")
    
        wsCSV.Range("D1") = "ID_VALUE"
        .Range("ValRng").Copy wsCSV.Range("D2")
     
        wsCSV.Range("E1") = "PAYROLL_ID"
        .Range("CORng").Copy wsCSV.Range("E2")
    
        wsCSV.Range("F1") = "GEN_CODE"
        .Range("GLRng").Copy wsCSV.Range("F2")
    
        wsCSV.Range("G1") = "DESCRIPTION"
        .Range("FDRng").Copy wsCSV.Range("G2")
        
        wsCSV.Range("H1") = "ID_DATE"
        .Range("FDRng").Copy wsCSV.Range("H2")
        
        NR = 2
        For cell = 1 To (Range("FDRng").Cells.SpecialCells(xlCellTypeConstants).Count)
            Range("G" & NR) = .Range("FDRng").Cells(cell) & " - " & _
                .Range("TDRng").Cells(cell)
            NR = NR + 1
        Next cell
    End With
    
    wsCSV.Move
    ActiveSheet.Name = "OVERTIME_CSV"

    This is the new code I have but I am not sure how to get the concatenated field in?

        Set Sourcewb = ThisWorkbook
            
            With Sourcewb
                
                Set wsData = .Sheets("OUTPUT")
    
            End With
        
                        Set Destwb = Application.Workbooks.Add
                
    
                            With Destwb.Worksheets("Sheet1")
    
                                .Range("A1") = "EMP_ID"
                                .Range("B1") = "ID_UNITS"
                                .Range("C1") = "ID_RATE"
                                .Range("D1") = "ID_VALUE"
                                .Range("E1") = "PAYROLL_ID"
                                .Range("F1") = "GEN_CODE"
                                .Range("G1") = "DESCRIPTION"
                                .Range("H1") = "ID_DATE"
                                .Range("A2").Name = "Area"
    
                            End With
                        
                                    With wsData
                                
                                        NR = .Cells(.Rows.Count, "A").End(xlUp).Row
                                        .Range("A2:H" & NR).Copy Destination:=Destwb.Worksheets("Sheet1").Range("Area")
                            
                                    End With
                
    
    ActiveSheet.Name = "OVERTIME_CSV"
    Last edited by mcinnes01; 11-04-2010 at 10:01 AM. Reason: First code example related to another sheet I have the same problem with, but for clarity the original relates to the new now

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: VBA Concatenate problem

    Probably this suffices

    Sub snb()
      Thisworkbook.sheets("Output").copy
       Activeworkbook.sheets(1).name="overtime_csv"
       Activeworkbook.saveas .....
    end sub



  3. #3
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    Thanks snb,

    The problem is the data on the output sheet is held in a useful structure, meaning it is held in the way it was entered so it can be checked and ammended before the output file is created. This being said I need a way that I can concatenate the field mentioned, as was done previously, but with in the new code structure.

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: VBA Concatenate problem

    So where's your example worksheet ?

  5. #5
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    Here you go.
    Attached Files Attached Files

  6. #6
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem


  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: VBA Concatenate problem

    if you want to concatenate 2 columns:

    Sub snb()
        Range("FDRng").Offset(, 5) = Evaluate(Replace(Range("FDRng").Address & " & ~_ ~& " & Range("TDRng").Address, "~", Chr(34)))
    End Sub

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA Concatenate problem

    Not sure if this is what you want
    Option Explicit
    
    Sub EMAILnSAVE()
    
        Dim Sourcewb As Object
        Dim Destwb As Object
        Dim cell As Long
        Dim NR As Long
        Dim lCol As Long
        Dim wsData As Worksheet
        Dim SaveStr As String
        Dim tagerror As String
        Dim Email_Send_To, Email_Send_From, Email_Subject, Email_Body As String
        Dim strUserEmail As String
        Dim strFirstClassPassword As String
        Dim errPar As String
        Dim iMsg As Object
        Dim iConfig As Object
        Dim sConfig As Variant
        Dim Deskstr As String
        
            
        strUserEmail = "email@address.ac.uk"
        strFirstClassPassword = ""
    
            Set iMsg = CreateObject("CDO.Message")
            Set iConfig = CreateObject("CDO.Configuration")
                
                iConfig.Load -1
                    Set sConfig = iConfig.Fields
            
                        With sConfig
                            .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
                            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.0.5" 'Name or IP of remote SMTP server
                            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25  'Server Port
                            .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = strUserEmail
                            .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = strFirstClassPassword
                            .Update
                        End With
         
                            With Application
                                .ScreenUpdating = False
                                .EnableEvents = False
                            End With
        
        Set Sourcewb = ThisWorkbook
            
            With Sourcewb
                Set wsData = .Sheets("OUTPUT")
            End With
        
                        Set Destwb = Application.Workbooks.Add
                            With Destwb.Worksheets("Sheet1")
                             For lCol = 1 To 8
                                .Cells(1, lCol) = Choose(lCol, "EMP_ID", "ID_UNITS", "ID_RATE", "ID_VALUE", _
                                "PAYROLL_ID", "GEN_CODE", "DESCRIPTION", "ID_DATE")
                                Next lCol
                                
                            End With
                        
                                    With wsData
                                        NR = .Cells(.Rows.Count, 1).End(xlUp).Row
                                        .Range(.Cells(2, 1), .Cells(.Rows.Count, 8).End(xlUp)).Copy Destination:=Destwb.Worksheets("Sheet1").Range("a2")
                                    End With
                
    
    ActiveSheet.Name = "OVERTIME_CSV"
       
        Deskstr = CreateObject("WScript.Shell").SpecialFolders("Desktop") _
              & Application.PathSeparator & "OVERTIME BACKUP"
        
        If Dir(Deskstr, vbDirectory) = "" Then MkDir Deskstr
    
    SaveStr = Deskstr & Application.PathSeparator & ActiveSheet.Name _
            & " - " _
            & Environ("USERNAME") _
            & " - " _
            & Format(Now, " d-m-yy h.mm AM/PM")
        
     '-----------------------------------------------------------------------------
    
        Email_Send_To = "email@message.ac.uk"
        Email_Send_From = "email@message.ac.uk"
        Email_Subject = "OVERTIME - OLASS " & Format(Now, "mm/yyyy")
        Email_Body = "SENDERS DETAILS - " & Environ("USERNAME")
    
    '------------------------------------------------------------------------------
               
        
        With Destwb
           .SaveAs Filename:=SaveStr & ".csv", FileFormat:=xlCSVWindows, CreateBackup:=False, local:=True
           .Close SaveChanges:=False
           On Error Resume Next
        
        End With
        
                With iMsg
                    
                    Set .Configuration = iConfig
                
                End With
        
                iMsg.to = Email_Send_To
                iMsg.From = Email_Send_From
                iMsg.Subject = Email_Subject
                iMsg.Textbody = Email_Body
                iMsg.AddAttachment SaveStr
                iMsg.Send
    
            On Error GoTo tagerror
    
        
                    If ActiveSheet.Range("a1") = "" Then
                    
                        Application.DisplayAlerts = False
                        ActiveSheet.Delete
                        Application.DisplayAlerts = True
    
                            Sourcewb.Activate
                            Sheets("INPUT").Select
        
                    Else
                        
                        Exit Sub
                    
                    End If
    
    clean_up:
        With Application
           .EnableEvents = True
           .ScreenUpdating = True
        End With
        Exit Sub
        
    tagerror:
        MsgBox "Error: (" & Err.Number & ") " & Err.Description & " at " & Err.Source, vbCritical
        Resume clean_up
        
    End Sub
    Attached Files Attached Files
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  9. #9
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    What I'm trying to do is get it so row "G" with header "Description" on the destwb to be a concate of row "G" - "H" from the Sourcewb e.g. the Start Date - End Date

    So on the Destwb in column G with title DESCRIPTION there should be something like 01/11/2010 - 10/11/2010

  10. #10
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: VBA Concatenate problem

    That's exactly what the code is doing that I suggested in my last post.

  11. #11
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    How do I get it to copy and paste with your code I'm not entirely sure where to place it, I'm still not 100% with VBA thanks for bearing with me!

  12. #12
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: VBA Concatenate problem

    Sub snb()
      Thisworkbook.sheets("Output").copy
       With Activeworkbook
         .saveas .....
         With .sheets(1)
           .name="overtime_csv"
           .cells(1,7).currentregion.resize(,1) = Evaluate(Replace(.cells(1,7).currentregion.resize(,1).Address & " & ~_ ~& " & .cells(1,7).currentregion.resize(,1).offset(,1).Address, "~", Chr(34)))
           .columns(8).delete
         end with
         .Save
       End With
    End Sub

  13. #13
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    I don't think that will work for me as I need it to fit in with the structure of the output file. The output workbook has to have the specific titles on like in my code or what Roy has provided, as these match the database tables to make the import possible. All I want to do is in row G on the output workbook I want row G - row H from the output sheet on the source workbook. Although copying the entire sheet would be a nice easy solution for this purpose I need to concate the 2 date columns for the databases free format description field. Also on my original code that did this on to a tempary worksheet (as detailed at the very top of this thread) I had to make sure it wouldn't continue inserting " - " all the way down the csv file, as this would make the import fail.

  14. #14
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    Right we are getting somewhere now... I've used your code snb, and it is working in the sense on concatenating 2 columns, however it is concatenating column A and B in to column A

        Set Sourcewb = ThisWorkbook
            
            With Sourcewb
                
                Set wsData = .Sheets("OUTPUT")
    
            End With
        
                        Set Destwb = Application.Workbooks.Add
                
    
                            With Destwb.Worksheets("Sheet1")
    
                                .Range("A1") = "EMP_ID"
                                .Range("B1") = "ID_UNITS"
                                .Range("C1") = "ID_RATE"
                                .Range("D1") = "ID_VALUE"
                                .Range("E1") = "PAYROLL_ID"
                                .Range("F1") = "GEN_CODE"
                                .Range("G1") = "DESCRIPTION"
                                .Range("H1") = "ID_DATE"
                                .Range("A2").Name = "Area"
    
                            End With
                        
                                    With wsData
                                
                                        NR = .Cells(.Rows.Count, "A").End(xlUp).Row
                                        .Range("A2:H" & NR).Copy Destination:=Destwb.Worksheets("Sheet1").Range("Area")
                            
                                    End With
                        
    
    
    ActiveSheet.Name = "OVERTIME_CSV"
        
                                    With ActiveSheet
                                    
                                        .Cells(1, 7).CurrentRegion.Resize(, 1) = Evaluate(Replace(.Cells(1, 7).CurrentRegion.Resize(, 1).Address & " & ~_ ~& " & .Cells(1, 7).CurrentRegion.Resize(, 1).Offset(, 1).Address, "~", Chr(34)))
                                    
                                    End With

  15. #15
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    or... with roys cleaner looking version

        Set Sourcewb = ThisWorkbook
            
            With Sourcewb
                
                Set wsData = .Sheets("OUTPUT")
    
            End With
        
                        Set Destwb = Application.Workbooks.Add
                            With Destwb.Worksheets("Sheet1")
                             For lCol = 1 To 8
                                .Cells(1, lCol) = Choose(lCol, "EMP_ID", "ID_UNITS", "ID_RATE", "ID_VALUE", _
                                "PAYROLL_ID", "GEN_CODE", "DESCRIPTION", "ID_DATE")
                                Next lCol
                                
                            End With
                        
                                    With wsData
                                        NR = .Cells(.Rows.Count, 1).End(xlUp).Row
                                        .Range(.Cells(2, 1), .Cells(.Rows.Count, 8).End(xlUp)).Copy Destination:=Destwb.Worksheets("Sheet1").Range("a2")
                                    End With
                
    
    ActiveSheet.Name = "OVERTIME_CSV"
        
                                    With ActiveSheet
                                    
                                        .Cells(1, 7).CurrentRegion.Resize(, 1) = Evaluate(Replace(.Cells(1, 7).CurrentRegion.Resize(, 1).Address & " & ~_ ~& " & .Cells(1, 7).CurrentRegion.Resize(, 1).Offset(, 1).Address, "~", Chr(34)))
                                    
                                    End With

  16. #16
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA Concatenate problem

    You talk about Row G, presumably that's Column G.

    If you use With ....End With then drop the sheet.name code into it
    
                                    With ActiveSheet
                                     .Name = "OVERTIME_CSV"
                                      .Cells(1, 7).CurrentRegion.Resize(, 1) = _
                                      Evaluate(Replace(.Cells(1, 7).CurrentRegion.Resize(, 1).Address & " & ~_ ~& " & .Cells(1, 7) _
                                      .CurrentRegion.Resize(, 1).Offset(, 1).Address, "~", Chr(34)))
                                    End With

  17. #17
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    Hi the problem is, is that :

     .Cells(1, 7).CurrentRegion.Resize(, 1) = _
                                      Evaluate(Replace(.Cells(1, 7).CurrentRegion.Resize(, 1).Address & " & ~_ ~& " & .Cells(1, 7) _
                                      .CurrentRegion.Resize(, 1).Offset(, 1).Address, "~", Chr(34)))
    Is concatenating ***columns A and B including the header in to column A


    Below I have tried changing it, It now concates A and B in to A including the header and G and H in to G including the Header?

    Any ideas? I'm really not very good with defining ranges in VBA

    .Cells(2, 7).CurrentRegion.Resize(, 7) = Evaluate(Replace(.Cells(2, 7).CurrentRegion.Resize(, 7).Address & " & ~ - ~& " & .Cells(2, 7).CurrentRegion.Resize(, 7).Offset(, 1).Address, "~", Chr(34)))

  18. #18
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA Concatenate problem

    Attach a workbook with what you actually need

  19. #19
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    here you go...
    Attached Files Attached Files

  20. #20
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    If you replace:

                   With ActiveSheet
                                     .Name = "OVERTIME_CSV"
                                      .Cells(1, 7).CurrentRegion.Resize(, 1) = _
                                      Evaluate(Replace(.Cells(1, 7).CurrentRegion.Resize(, 1).Address & " & ~_ ~& " & .Cells(1, 7) _
                                      .CurrentRegion.Resize(, 1).Offset(, 1).Address, "~", Chr(34)))
                                    End With
    With

    With ActiveSheet
                                        .Name = "OVERTIME_CSV"
                                        .Cells(2, 7).CurrentRegion.Resize(, 7) = Evaluate(Replace(.Cells(2, 7).CurrentRegion.Resize(, 7) _
                                        .Address & " & ~ - ~& " & _
                                        .Cells(2, 7).CurrentRegion.Resize(, 7).Offset(, 1).Address, "~", Chr(34)))
                                    End With
    It is still concating A and B which it shouldn't but it concates G and H correctly. Although it is not diplaying the dates as dates any more? and the headers are being concatenated?

  21. #21
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA Concatenate problem

    Is this what you are aiming for
    Dim rRng As Range
    Dim LastRw As Long
    With ActiveSheet
       .Name = "OVERTIME_CSV"
       LastRw = .UsedRange.Rows.Count
     Set rRng = .Range(.Cells(2, 9), .Cells(LastRw, 9))
     rRng.FormulaR1C1 = "=TEXT(RC[-2],""dd/mm/yy"") &""-"" &TEXT(RC[-1],""dd/mm/yy"")"
     .Cells(1, 9).Value = "DESCRIPTION"
    .Columns(9).Value = Columns(9).Value
    .Columns("G:H").Delete Shift:=xlToLeft
    End With

  22. #22
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    almost, it is not putting the last rows description in, in column G and it is completely missing column H
    Last edited by mcinnes01; 11-03-2010 at 11:20 AM.

  23. #23
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA Concatenate problem

    It concatenates G & H into I then deletes G & H, leaving the new Column G

  24. #24
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    basically on the csv file I import, the last 2 columns G & H contain a free format date range description in the format "from date - to date" and then the ID Date aka the from date, respectively.

    So:

    _________G_____________________H_______
    ____DESCRIPTION____________ID_DATE____
    01/11/2010 - 30/11/2010_______01/11/2010____

  25. #25
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA Concatenate problem

    So the last result gave the correct text in Column G, but uou want to keep Column H as well?

  26. #26
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    yes, but it missed off the last rows concation for some reason?

    For the row H I was wondering if another line detailing copy column H from sourcewb to column H on destwb would work?

  27. #27
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    Hi Roy,


    This seems to sort out the issue with column H but I can't get my head around why the last row in column G is blank? I don't know if there is a cleaner way of writing what I have patched together?

                                    With wsData
                                        NR = .Cells(.Rows.Count, 1).End(xlUp).Row
                                        .Range(.Cells(2, 1), .Cells(.Rows.Count, 8).End(xlUp)).Copy Destination:=Destwb.Worksheets("OVERTIME_CSV").Range("a2")
                                    End With
                                    
                                        With ActiveSheet
                                            .Name = "OVERTIME_CSV"
                                            LastRw = .UsedRange.Rows.Count
                                            Set rRng = .Range(.Cells(2, 9), .Cells(LastRw, 9))
                                            rRng.FormulaR1C1 = "=TEXT(RC[-2],""dd/mm/yy"") &""-"" &TEXT(RC[-1],""dd/mm/yy"")"
                                            .Cells(1, 9).Value = "DESCRIPTION"
                                            .Columns(9).Value = Columns(9).Value
                                            .Columns("G:H").Delete Shift:=xlToLeft
                                        End With
                                    
                                            With wsData
                                                .Range(.Cells(2, 8), .Cells(.Rows.Count, 8).End(xlUp)).Copy Destination:=Destwb.Worksheets("OVERTIME_CSV").Range("H2")
                                            End With
                                    
                                                With Destwb.Worksheets("OVERTIME_CSV")
                                                    For lCol = 1 To 8
                                                    .Cells(1, lCol) = Choose(lCol, "EMP_ID", "ID_UNITS", "ID_RATE", "ID_VALUE", _
                                                    "PAYROLL_ID", "GEN_CODE", "DESCRIPTION", "ID_DATE")
                                                    Next lCol
                                                End With

  28. #28
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    This is the sample CSV file with the code above
    Attached Files Attached Files

  29. #29
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA Concatenate problem

    Untested, maybe
    
    Dim rRng As Range
    Dim LastRw As Long
    With ActiveSheet
       .Name = "OVERTIME_CSV"
       LastRw = .UsedRange.Rows.Count
     Set rRng = .Range(.Cells(2, 10), .Cells(LastRw, 10))
     rRng.FormulaR1C1 = "=TEXT(RC[-2],""dd/mm/yy"") &""-"" &TEXT(RC[-1],""dd/mm/yy"")"
     Set rRng = .Range(.Cells(2, 9), .Cells(LastRw, 9))
     rRngrRng.FormulaR1C1 = "=TEXT(RC[-2],""dd/mm/yy"") "
     .Cells(1, 9).Value = "DESCRIPTION"
    .Columns(9).Value = Columns(9).Value
    .Columns(10).Value = Columns(10).Value
    .Columns("G:H").Delete Shift:=xlToLeft
    End With
    If not I'll look later

  30. #30
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    I tried the code but it seemed to create a rather strange out put, this is the code and attached is the output:

                                    With wsData
                                        NR = .Cells(.Rows.Count, 1).End(xlUp).Row
                                        .Range(.Cells(2, 1), .Cells(.Rows.Count, 8).End(xlUp)).Copy Destination:=Destwb.Worksheets("OVERTIME_CSV").Range("a2")
                                    End With
                                    
                                            With ActiveSheet
                                                .Name = "OVERTIME_CSV"
                                                LastRw = .UsedRange.Rows.Count
                                                Set rRng = .Range(.Cells(2, 10), .Cells(LastRw, 10))
                                                rRng.FormulaR1C1 = "=TEXT(RC[-2],""dd/mm/yy"") &""-"" &TEXT(RC[-1],""dd/mm/yy"")"
                                                Set rRng = .Range(.Cells(2, 9), .Cells(LastRw, 9))
                                                rRng.FormulaR1C1 = "=TEXT(RC[-2],""dd/mm/yy"") "
                                                .Cells(1, 9).Value = "DESCRIPTION"
                                                .Columns(9).Value = Columns(9).Value
                                                .Columns(10).Value = Columns(10).Value
                                                .Columns("G:H").Delete Shift:=xlToLeft
                                            End With
                                    
                                                    With wsData
                                                        .Range(.Cells(2, 8), .Cells(.Rows.Count, 8).End(xlUp)).Copy Destination:=Destwb.Worksheets("OVERTIME_CSV").Range("H2")
                                                    End With
                                    
                                                            With Destwb.Worksheets("OVERTIME_CSV")
                                                                For lCol = 1 To 8
                                                                .Cells(1, lCol) = Choose(lCol, "EMP_ID", "ID_UNITS", "ID_RATE", "ID_VALUE", _
                                                                "PAYROLL_ID", "GEN_CODE", "DESCRIPTION", "ID_DATE")
                                                                Next lCol
                                                            End With
    Attached Files Attached Files

  31. #31
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    This seems to work I think....


                                    With wsData
                                        NR = .Cells(.Rows.Count, 1).End(xlUp).Row
                                        .Range(.Cells(2, 1), .Cells(.Rows.Count, 8).End(xlUp)).Copy Destination:=Destwb.Worksheets("OVERTIME_CSV").Range("a2")
                                    End With
                                    
                                            With ActiveSheet
                                                .Name = "OVERTIME_CSV"
                                                LastRw = .UsedRange.Rows.Count
                                                Set rRng = .Range(.Cells(2, 9), .Cells(LastRw + 1, 9))
                                                rRng.FormulaR1C1 = "=TEXT(RC[-2],""dd/mm/yy"") &""-"" &TEXT(RC[-1],""dd/mm/yy"")"
                                                .Columns(9).Value = Columns(9).Value
                                                .Columns("G:H").Delete Shift:=xlToLeft
                                            End With
                                    
                                                    With wsData
                                                        .Range(.Cells(2, 8), .Cells(.Rows.Count, 8).End(xlUp)).Copy Destination:=Destwb.Worksheets("OVERTIME_CSV").Range("H2")
                                                    End With
                                    
                                                            With Destwb.Worksheets("OVERTIME_CSV")
                                                                For lCol = 1 To 8
                                                                .Cells(1, lCol) = Choose(lCol, "EMP_ID", "ID_UNITS", "ID_RATE", "ID_VALUE", _
                                                                "PAYROLL_ID", "GEN_CODE", "DESCRIPTION", "ID_DATE")
                                                                Next lCol
                                                            End With

    The change is the last row + 1

  32. #32
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    Right I have the other side to the overtime sheet, which is the expenses sheet, they are the same more or less except the layout of the output file is different and the concatenate is in Column B and relates to 3 Columns from the source workbook.

    This is the original code that copied it to a new sheet exactly the same as the overtime, but now I want it to copy direct to a new book.


    With wsData
        wsCSV.Range("A1") = "EMP_ID"
        .Range("EmpRng").Copy wsCSV.Range("A2")
        
        wsCSV.Range("B1") = "DESCRIPTION"
        NR = 2
        For cell = 1 To (Range("DescRng").Cells.SpecialCells(xlCellTypeConstants).Count)
            Range("B" & NR) = .Range("DescRNG").Cells(cell) & ": " & _
                .Range("FDRng").Cells(cell) & " - " & .Range("TDRng").Cells(cell)
            NR = NR + 1
        Next cell
    
        wsCSV.Range("C1") = "ID_UNITS"
        .Range("UnitRng").Copy wsCSV.Range("C2")
    
        wsCSV.Range("D1") = "ID_RATE"
        .Range("RateRng").Copy wsCSV.Range("D2")
     
        wsCSV.Range("E1") = "ID_VALUE"
        .Range("ValRng").Copy wsCSV.Range("E2")
     
        wsCSV.Range("F1") = "PAYROLL_ID"
        .Range("CORng").Copy wsCSV.Range("F2")
    
        wsCSV.Range("G1") = "GEN_CODE"
        .Range("GLRng").Copy wsCSV.Range("G2")
    
        wsCSV.Range("H1") = "ID_DATE"
        .Range("FDRng").Copy wsCSV.Range("H2")
    End With

  33. #33
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    This is what I have got so far but I am not sure how to adapt the concated field:

    Option Explicit
    
    Sub EMAILnSAVE()
    
        Dim Sourcewb As Object
        Dim Destwb As Object
        Dim cell As Long
        Dim NR As Long
        Dim lCol As Long
        Dim wsData As Worksheet
        Dim SaveStr As String
        Dim tagerror As String
        Dim Email_Send_To, Email_Send_From, Email_Subject, Email_Body As String
        Dim strUserEmail As String
        Dim strFirstClassPassword As String
        Dim errPar As String
        Dim iMsg As Object
        Dim iConfig As Object
        Dim sConfig As Variant
        Dim Deskstr As String
        Dim rRng As Range
        Dim LastRw As Long
        
            
        strUserEmail = "me@myemail.ac.uk"
        strFirstClassPassword = "password"
    
            Set iMsg = CreateObject("CDO.Message")
            Set iConfig = CreateObject("CDO.Configuration")
                
                iConfig.Load -1
                    Set sConfig = iConfig.Fields
            
                        With sConfig
                            .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
                            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.0.5" 'Name or IP of remote SMTP server
                            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25  'Server Port
                            .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = strUserEmail
                            .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = strFirstClassPassword
                            .Update
                        End With
         
                            With Application
                                .ScreenUpdating = False
                                .EnableEvents = False
                            End With
        
        Set Sourcewb = ThisWorkbook
            
            With Sourcewb
                
                Set wsData = .Sheets("OUTPUT")
    
            End With
        
                        Set Destwb = Application.Workbooks.Add
                            
                        
     ActiveSheet.Name = "EXPENSES_CSV"
                        
    
                                    With wsData
                                        NR = .Cells(.Rows.Count, 1).End(xlUp).Row
                                        .Range(.Cells(2, 1), .Cells(.Rows.Count, 8).End(xlUp)).Copy Destination:=Destwb.Worksheets("OVERTIME_CSV").Range("a2")
                                    End With
                                    
                                            With ActiveSheet
                                                .Name = "EXPENSES_CSV"
                                                LastRw = .UsedRange.Rows.Count
                                                Set rRng = .Range(.Cells(2, 9), .Cells(LastRw + 1, 9))
                                                rRng.FormulaR1C1 = "=TEXT(RC[-2],""dd/mm/yy"") &""-"" &TEXT(RC[-1],""dd/mm/yy"")"
                                                .Columns(9).Value = Columns(9).Value
                                                .Columns("G:H").Delete Shift:=xlToLeft
                                            End With
                                    
                                                    With wsData
                                                        .Range(.Cells(2, 8), .Cells(.Rows.Count, 8).End(xlUp)).Copy Destination:=Destwb.Worksheets("OVERTIME_CSV").Range("H2")
                                                    End With
                                    
                                                            With Destwb.Worksheets("EXPENSES_CSV")
                                                                For lCol = 1 To 8
                                                                .Cells(1, lCol) = Choose(lCol, "EMP_ID", "DESCRIPTION", "ID_UNITS", "ID_RATE", "ID_VALUE", _
                                                                "PAYROLL_ID", "GEN_CODE", "ID_DATE")
                                                                Next lCol
                                                            End With
                                               
                                    
        Deskstr = CreateObject("WScript.Shell").SpecialFolders("Desktop") _
              & Application.PathSeparator & "EXPENSES BACKUP"
        
        If Dir(Deskstr, vbDirectory) = "" Then MkDir Deskstr
    
    SaveStr = Deskstr & Application.PathSeparator & ActiveSheet.Name _
            & " - " _
            & Environ("USERNAME") _
            & " - " _
            & Format(Now, " d-m-yy h.mm AM/PM")
        
     '-----------------------------------------------------------------------------
    
        Email_Send_To = "me@myemail.ac.uk"
        Email_Send_From = "me@myemail.ac.uk"
        Email_Subject = "EXPENSES - OLASS " & Format(Now, "mm/yyyy")
        Email_Body = "SENDERS DETAILS - " & Environ("USERNAME")
    
    '------------------------------------------------------------------------------
               
        
        With Destwb
           .SaveAs Filename:=SaveStr & ".csv", FileFormat:=xlCSVWindows, CreateBackup:=False, local:=True
           .Close SaveChanges:=False
           On Error Resume Next
        
        End With
        
                With iMsg
                    
                    Set .Configuration = iConfig
                
                End With
        
                iMsg.To = Email_Send_To
                iMsg.From = Email_Send_From
                iMsg.Subject = Email_Subject
                iMsg.Textbody = Email_Body
                iMsg.AddAttachment SaveStr
                iMsg.Send
    
            On Error GoTo tagerror
    
        
                    If ActiveSheet.Range("a1") = "" Then
                    
                        Application.DisplayAlerts = False
                        ActiveSheet.Delete
                        Application.DisplayAlerts = True
    
                            Sourcewb.Activate
                            Sheets("INPUT").Select
        
                    Else
                        
                        Exit Sub
                    
                    End If
    
    clean_up:
        With Application
           .EnableEvents = True
           .ScreenUpdating = True
        End With
        Exit Sub
        
    tagerror:
        MsgBox "Error: (" & Err.Number & ") " & Err.Description & " at " & Err.Source, vbCritical
        Resume clean_up
        
    End Sub

  34. #34
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    I've tried introducing the original code but I cant seem to get it to work, the debugger stops on :

            With Sourcewb.wsData
    This is the code...

    Option Explicit
    
    Sub EMAILnSAVE()
    
        Dim Sourcewb As Object
        Dim Destwb As Object
        Dim Dsht As Worksheet
        Dim cell As Long
        Dim NR As Long
        Dim lCol As Long
        Dim wsData As Worksheet
        Dim SaveStr As String
        Dim tagerror As String
        Dim Email_Send_To, Email_Send_From, Email_Subject, Email_Body As String
        Dim strUserEmail As String
        Dim strFirstClassPassword As String
        Dim errPar As String
        Dim iMsg As Object
        Dim iConfig As Object
        Dim sConfig As Variant
        Dim Deskstr As String
        Dim rRng As Range
        Dim LastRw As Long
        
            
        strUserEmail = "me@myemail.ac.uk"
        strFirstClassPassword = "password"
    
            Set iMsg = CreateObject("CDO.Message")
            Set iConfig = CreateObject("CDO.Configuration")
                
                iConfig.Load -1
                    Set sConfig = iConfig.Fields
            
                        With sConfig
                            .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
                            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.0.5" 'Name or IP of remote SMTP server
                            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25  'Server Port
                            .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = strUserEmail
                            .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = strFirstClassPassword
                            .Update
                        End With
         
                            With Application
                                .ScreenUpdating = False
                                .EnableEvents = False
                            End With
        
        Set Sourcewb = ThisWorkbook
            
            With Sourcewb
                
                Set wsData = .Sheets("OUTPUT")
    
            End With
        
                        Set Destwb = Application.Workbooks.Add
                            
            ActiveSheet.Name = "EXPENSES_CSV"
                        
                    With Destwb
                    
                        Set Dsht = .Sheets("EXPENSES_CSV")
                        
                    End With
                    
        Sourcewb.Activate
    
            With Sourcewb.wsData
    
                .Range("EmpRng").Copy Destwb.Dsht.Range("A2")
            
                        NR = 2
                            For cell = 1 To (Range("DescRng").Cells.SpecialCells(xlCellTypeConstants).Count)
                            Destwb.Dsht.Range("B" & NR) = .Range("DescRNG").Cells(cell) & ": " & _
                            .Range("FDRng").Cells(cell) & " - " & .Range("TDRng").Cells(cell)
                        NR = NR + 1
                    Next cell
    
                .Range("UnitRng").Copy Destwb.Dsht.Range("C2")
    
                .Range("RateRng").Copy Destwb.Dsht.Range("D2")
    
                .Range("ValRng").Copy Destwb.Dsht.Range("E2")
    
                .Range("CORng").Copy Destwb.Dsht.Range("F2")
    
                .Range("GLRng").Copy Destwb.Dsht.Range("G2")
    
                .Range("FDRng").Copy Destwb.Dsht.Range("H2")
        
            End With
                                               
                                               Destwb.Activate
                                               
                                                            With Destwb.Dsht
                                                                For lCol = 1 To 8
                                                                .Cells(1, lCol) = Choose(lCol, "EMP_ID", "DESCRIPTION", "ID_UNITS", "ID_RATE", "ID_VALUE", _
                                                                "PAYROLL_ID", "GEN_CODE", "ID_DATE")
                                                                Next lCol
                                                            End With
                                                            
        Deskstr = CreateObject("WScript.Shell").SpecialFolders("Desktop") _
              & Application.PathSeparator & "EXPENSES BACKUP"
        
        If Dir(Deskstr, vbDirectory) = "" Then MkDir Deskstr
    
    SaveStr = Deskstr & Application.PathSeparator & ActiveSheet.Name _
            & " - " _
            & Environ("USERNAME") _
            & " - " _
            & Format(Now, " d-m-yy h.mm AM/PM")
        
     '-----------------------------------------------------------------------------
    
        Email_Send_To = "me@myemail.ac.uk"
        Email_Send_From = "me@myemail.ac.uk"
        Email_Subject = "EXPENSES - OLASS " & Format(Now, "mm/yyyy")
        Email_Body = "SENDERS DETAILS - " & Environ("USERNAME")
    
    '------------------------------------------------------------------------------
               
        
        With Destwb
           .SaveAs Filename:=SaveStr & ".csv", FileFormat:=xlCSVWindows, CreateBackup:=False, local:=True
           .Close SaveChanges:=False
           On Error Resume Next
        
        End With
        
                With iMsg
                    
                    Set .Configuration = iConfig
                
                End With
        
                iMsg.To = Email_Send_To
                iMsg.From = Email_Send_From
                iMsg.Subject = Email_Subject
                iMsg.Textbody = Email_Body
                iMsg.AddAttachment SaveStr
                iMsg.Send
    
            On Error GoTo tagerror
    
        
                    If ActiveSheet.Range("a1") = "" Then
                    
                        Application.DisplayAlerts = False
                        ActiveSheet.Delete
                        Application.DisplayAlerts = True
    
                            Sourcewb.Activate
                            Sheets("INPUT").Select
        
                    Else
                        
                        Exit Sub
                    
                    End If
    
    clean_up:
        With Application
           .EnableEvents = True
           .ScreenUpdating = True
        End With
        Exit Sub
        
    tagerror:
        MsgBox "Error: (" & Err.Number & ") " & Err.Description & " at " & Err.Source, vbCritical
        Resume clean_up
        
    End Sub
    Last edited by mcinnes01; 11-04-2010 at 05:46 AM. Reason: Destwb.Dsht.Range("B" & NR) = .Range("DescRNG").Cells(cell) & ": " & _

  35. #35
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA Concatenate problem

    Have you set the variables Sourcewb & wsdata?

  36. #36
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    Is that the bit :

        Dim Sourcewb As Object
        Dim Destwb As Object
    or is Set Sourcewb...?

  37. #37
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA Concatenate problem

    What you have posted declares the Variables. You need to give them a value using Set wsData=.

    If they are fixed you could declare them as a Constant

  38. #38
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    I thought I have here??:

        Set Sourcewb = ThisWorkbook
            
            With Sourcewb
                
                Set wsData = .Sheets("OUTPUT")
    
            End With
        
                        Set Destwb = Application.Workbooks.Add
                            
            ActiveSheet.Name = "EXPENSES_CSV"
                        
                    With Destwb
                    
                        Set Dsht = .Sheets("EXPENSES_CSV")
                        
                    End With

  39. #39
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA Concatenate problem

    I think an updated workbook might help.

    You don't need those With Statements

      Set sourcewb = ThisWorkbook
      Set wsData = sourcewb.Sheets("OUTPUT")
      Set destwb = Application.Workbooks.Add
      ActiveSheet.Name = "EXPENSES_CSV"
      Set Dsht = Destwb.Sheets("EXPENSES_CSV")

  40. #40
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    Here is an example for you, it is more or less the same as overtime except there are many more fields on the output tab for data checking.

    Current code:

    Option Explicit
    
    Sub EMAILnSAVE()
    
        Dim Sourcewb As Object
        Dim Destwb As Object
        Dim Dsht As Worksheet
        Dim cell As Long
        Dim NR As Long
        Dim lCol As Long
        Dim wsData As Worksheet
        Dim SaveStr As String
        Dim tagerror As String
        Dim Email_Send_To, Email_Send_From, Email_Subject, Email_Body As String
        Dim strUserEmail As String
        Dim strFirstClassPassword As String
        Dim errPar As String
        Dim iMsg As Object
        Dim iConfig As Object
        Dim sConfig As Variant
        Dim Deskstr As String
        Dim rRng As Range
        Dim LastRw As Long
        
            
        strUserEmail = "me@myemail.ac.uk"
        strFirstClassPassword = "password"
    
            Set iMsg = CreateObject("CDO.Message")
            Set iConfig = CreateObject("CDO.Configuration")
                
                iConfig.Load -1
                    Set sConfig = iConfig.Fields
            
                        With sConfig
                            .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
                            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.0.5" 'Name or IP of remote SMTP server
                            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25  'Server Port
                            .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = strUserEmail
                            .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = strFirstClassPassword
                            .Update
                        End With
         
                            With Application
                                .ScreenUpdating = False
                                .EnableEvents = False
                            End With
            
      Set Sourcewb = ThisWorkbook
      Set wsData = Sourcewb.Sheets("OUTPUT")
      Set Destwb = Application.Workbooks.Add
      ActiveSheet.Name = "EXPENSES_CSV"
      Set Dsht = Destwb.Sheets("EXPENSES_CSV")
                    
        Sourcewb.Activate
    
            With Sourcewb.wsData
    
                .Range("EmpRng").Copy Destwb.Dsht.Range("A2")
            
                        NR = 2
                            For cell = 1 To (Range("DescRng").Cells.SpecialCells(xlCellTypeConstants).Count)
                            Destwb.Dsht.Range("B" & NR) = .Range("DescRNG").Cells(cell) & ": " & _
                            .Range("FDRng").Cells(cell) & " - " & .Range("TDRng").Cells(cell)
                        NR = NR + 1
                    Next cell
    
                .Range("UnitRng").Copy Destwb.Dsht.Range("C2")
    
                .Range("RateRng").Copy Destwb.Dsht.Range("D2")
    
                .Range("ValRng").Copy Destwb.Dsht.Range("E2")
    
                .Range("CORng").Copy Destwb.Dsht.Range("F2")
    
                .Range("GLRng").Copy Destwb.Dsht.Range("G2")
    
                .Range("FDRng").Copy Destwb.Dsht.Range("H2")
        
            End With
                                               
                                               Destwb.Activate
                                               
                                                            With Destwb.Dsht
                                                                For lCol = 1 To 8
                                                                .Cells(1, lCol) = Choose(lCol, "EMP_ID", "DESCRIPTION", "ID_UNITS", "ID_RATE", "ID_VALUE", _
                                                                "PAYROLL_ID", "GEN_CODE", "ID_DATE")
                                                                Next lCol
                                                            End With
                                                            
        Deskstr = CreateObject("WScript.Shell").SpecialFolders("Desktop") _
              & Application.PathSeparator & "EXPENSES BACKUP"
        
        If Dir(Deskstr, vbDirectory) = "" Then MkDir Deskstr
    
    SaveStr = Deskstr & Application.PathSeparator & ActiveSheet.Name _
            & " - " _
            & Environ("USERNAME") _
            & " - " _
            & Format(Now, " d-m-yy h.mm AM/PM")
        
     '-----------------------------------------------------------------------------
    
        Email_Send_To = "me@myemail.ac.uk"
        Email_Send_From = "me@myemail.ac.uk"
        Email_Subject = "EXPENSES - OLASS " & Format(Now, "mm/yyyy")
        Email_Body = "SENDERS DETAILS - " & Environ("USERNAME")
    
    '------------------------------------------------------------------------------
               
        
        With Destwb
           .SaveAs Filename:=SaveStr & ".csv", FileFormat:=xlCSVWindows, CreateBackup:=False, local:=True
           .Close SaveChanges:=False
           On Error Resume Next
        
        End With
        
                With iMsg
                    
                    Set .Configuration = iConfig
                
                End With
        
                iMsg.To = Email_Send_To
                iMsg.From = Email_Send_From
                iMsg.Subject = Email_Subject
                iMsg.Textbody = Email_Body
                iMsg.AddAttachment SaveStr
                iMsg.Send
    
            On Error GoTo tagerror
    
        
                    If ActiveSheet.Range("a1") = "" Then
                    
                        Application.DisplayAlerts = False
                        ActiveSheet.Delete
                        Application.DisplayAlerts = True
    
                            Sourcewb.Activate
                            Sheets("INPUT").Select
        
                    Else
                        
                        Exit Sub
                    
                    End If
    
    clean_up:
        With Application
           .EnableEvents = True
           .ScreenUpdating = True
        End With
        Exit Sub
        
    tagerror:
        MsgBox "Error: (" & Err.Number & ") " & Err.Description & " at " & Err.Source, vbCritical
        Resume clean_up
        
    End Sub
    Attached Files Attached Files
    Last edited by mcinnes01; 11-04-2010 at 08:48 AM.

  41. #41
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA Concatenate problem

    I'm sorry but what are you trying to do now, the lst code was concatenating columns

  42. #42
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    I am trying to concate columns still, just in a different format.

    It concate columns I & ": " & S & " - " & T off the source workbook to column B on the Destination workbook. My problem is there are a lot more columns on the source workbook and the concated column is in column B not G now

  43. #43
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA Concatenate problem

    I've cleared a lot of error, see what this produces.
    Attached Files Attached Files

  44. #44
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    Thats it, thank you! It looks like I was just refering to the wrong or overly complicated sheets. Thanks a lot

  45. #45
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA Concatenate problem

    Did you debug the code?

  46. #46
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Re: VBA Concatenate problem

    I didn't get any bugs, I have had to add a few extra fields in so a lot of my named ranges had to be changed, I imagine the that any bugs were probably related to ranges that were defined wrong. My only issue now which I think I remember correctly happended when I first used the CSV code, is that the csv output file lists commas down to row 65536 (when opened in notepad). Its strange though because I have my original which I had fixed this problem on and putting the code side by side it is identical other than the couple on new columns I have added in the exact same code structure

  47. #47
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA Concatenate problem

    There were several errors that I found, like Sourcewb.wsData, which needed changing to wsData

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1