+ Reply to Thread
Results 1 to 4 of 4

Tasks in Macro not working

Hybrid View

  1. #1
    Registered User
    Join Date
    05-22-2019
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    66

    Tasks in Macro not working

    Can someone help me why Tasks 17 and 18 in the macro below not working?

    Sub ExportDataToSS()
        Dim currentSheet As Worksheet
        Dim newWorkbook As Workbook
        Dim rawDataSheet As Worksheet
        Dim toSSSheet As Worksheet
        Dim Calculations2 As Worksheet
        Dim lastRow As Long
        Dim i As Long
        Dim emptyRow As Long
        Dim consecutiveEmpty As Long
        Dim lastMonthName As String
        Dim lastMonthYear As String
        
        ' Task 1: Name this macro "Export Data to SS"
        
        ' Task 2: Define the current sheet as the ActiveSheet
        Set currentSheet = ActiveSheet
        
        ' Define the sheet "Calculation" in the workbook as "Calculation"
        Dim calculationSheet As Worksheet
        Set calculationSheet = ThisWorkbook.Sheets("Calculation")
        
        ' Task 3: Create a new Excel workbook
        Set newWorkbook = Workbooks.Add
        
        ' Task 4: Create three sheets in the new Excel workbook
        Set rawDataSheet = newWorkbook.Sheets.Add(After:=newWorkbook.Sheets(newWorkbook.Sheets.Count))
        rawDataSheet.Name = "raw data"
        
        Set toSSSheet = newWorkbook.Sheets.Add(After:=rawDataSheet)
        toSSSheet.Name = "To SS"
        
          Set calculations2Sheet = newWorkbook.Sheets.Add(After:=toSSSheet)
        calculations2Sheet.Name = "Calculations2"
        
        ' Task 5: Align all cells in the new sheets of Task 5 left
        rawDataSheet.Cells.HorizontalAlignment = xlLeft
        toSSSheet.Cells.HorizontalAlignment = xlLeft
        calculations2Sheet.Cells.HorizontalAlignment = xlLeft
    
        
        ' Task 6: Copy data from ActiveSheet to "raw data" and from "Calculation" to "Calculations2" as values without format
        currentSheet.UsedRange.Copy
        rawDataSheet.Range("A1").PasteSpecial xlPasteValues
        
        calculationSheet.UsedRange.Copy
        calculations2Sheet.Range("A1").PasteSpecial xlPasteValues
        
        Application.CutCopyMode = False ' Clear the clipboard
        
        ' Task 7: Type headers in the first cells of column A until H of the sheet "To SS"
        toSSSheet.Range("A1").Value = "campaign_external_id"
        toSSSheet.Range("B1").Value = "amount"
        toSSSheet.Range("C1").Value = "received"
        toSSSheet.Range("D1").Value = "record_type"
        toSSSheet.Range("E1").Value = "contribution_type"
        toSSSheet.Range("F1").Value = "notes"
        toSSSheet.Range("G1").Value = "currency"
        toSSSheet.Range("H1").Value = "donor_person_id"
        
        ' Task 8: Format the data in column B of "To SS" as currency and use the symbol $
        toSSSheet.Range("B:B").NumberFormat = "$#,##0.00"
        
        ' Task 9: Copy data from column C of "raw data" and their corresponding cells in column P of "raw data"
        ' Only copy data if the corresponding cells in column P are not 0 or 0.00
        lastRow = rawDataSheet.Cells(Rows.Count, "C").End(xlUp).Row
        emptyRow = 2 ' Start row in "To SS" sheet
        
        For i = 4 To lastRow
            If rawDataSheet.Cells(i, "P").Value <> 0 Then
                toSSSheet.Cells(emptyRow, "A").Value = rawDataSheet.Cells(i, "C").Value
                toSSSheet.Cells(emptyRow, "B").Value = -rawDataSheet.Cells(i, "P").Value
                toSSSheet.Cells(emptyRow, "F").Value = "Ministry Compensation of " & Format(DateAdd("m", -1, Date), "mmmm yyyy")
                emptyRow = emptyRow + 1
            End If
        Next i
        
        ' Task 10: Copy data from column J of "raw data" and their corresponding cells in column K of "raw data"
        ' Only copy data if the corresponding cells in column K are not 0 or 0.00 or empty
        consecutiveEmpty = 0
        
        For i = 4 To lastRow
            If consecutiveEmpty >= 100 Then Exit For ' Stop after 100 consecutive empty cells
            
            If Not IsEmpty(rawDataSheet.Cells(i, "K").Value) And rawDataSheet.Cells(i, "K").Value <> 0 Then
                toSSSheet.Cells(emptyRow, "A").Value = rawDataSheet.Cells(i, "J").Value
                toSSSheet.Cells(emptyRow, "B").Value = -rawDataSheet.Cells(i, "K").Value
                toSSSheet.Cells(emptyRow, "F").Value = "Ministry Related Expenses of " & Format(DateAdd("m", -1, Date), "mmmm yyyy")
                emptyRow = emptyRow + 1
                consecutiveEmpty = 0
            Else
                consecutiveEmpty = consecutiveEmpty + 1
            End If
        Next i
        
        ' Task 11: Copy data from column C of "raw data" and their corresponding cells in column G and S
        ' Only copy data if the corresponding cells in column G are not 0 or 0.00 or empty
        consecutiveEmpty = 0
        
        For i = 4 To lastRow
            If consecutiveEmpty >= 100 Then Exit For ' Stop after 100 consecutive empty cells
            
            If Not IsEmpty(rawDataSheet.Cells(i, "G").Value) And rawDataSheet.Cells(i, "G").Value <> 0 Then
                toSSSheet.Cells(emptyRow, "A").Value = rawDataSheet.Cells(i, "C").Value
                toSSSheet.Cells(emptyRow, "B").Value = -rawDataSheet.Cells(i, "G").Value
                toSSSheet.Cells(emptyRow, "F").Value = "Adjustment - " & rawDataSheet.Cells(i, "S").Value
                emptyRow = emptyRow + 1
                consecutiveEmpty = 0
            Else
                consecutiveEmpty = consecutiveEmpty + 1
            End If
        Next i
        
        ' Task 12: Copy data from column C of "raw data" and their corresponding cells in column F and S
        ' Only copy data if the corresponding cells in column F are not 0 or 0.00 or empty
        consecutiveEmpty = 0
        
        For i = 4 To lastRow
            If consecutiveEmpty >= 100 Then Exit For ' Stop after 100 consecutive empty cells
            
            If Not IsEmpty(rawDataSheet.Cells(i, "F").Value) And rawDataSheet.Cells(i, "F").Value <> 0 Then
                toSSSheet.Cells(emptyRow, "A").Value = rawDataSheet.Cells(i, "C").Value
                toSSSheet.Cells(emptyRow, "B").Value = -rawDataSheet.Cells(i, "F").Value
                toSSSheet.Cells(emptyRow, "F").Value = "Adjustment - " & rawDataSheet.Cells(i, "S").Value
                emptyRow = emptyRow + 1
                consecutiveEmpty = 0
            Else
                consecutiveEmpty = consecutiveEmpty + 1
            End If
        Next i
        
        ' Task 13: In column C of "To SS" type the last day of the last month. Format as M/D/YY.
        lastMonthName = Format(DateAdd("m", -1, Date), "mmmm")
        lastMonthYear = Format(DateAdd("m", -1, Date), "yyyy")
        
        toSSSheet.Range("C2:C" & emptyRow - 1).Value = DateSerial(CInt(lastMonthYear), Month(Date), 0)
        toSSSheet.Range("C2:C" & emptyRow - 1).NumberFormat = "m/d/yy"
        
        ' Task 14: In column D of "To SS" type "ledger"
        toSSSheet.Range("D2:D" & emptyRow - 1).Value = "ledger"
        
        ' Task 15: In column E of "To SS" type "Expense"
        toSSSheet.Range("E2:E" & emptyRow - 1).Value = "Expense"
        
        ' Task 16: In column G of "To SS" type "USD"
        toSSSheet.Range("G2:G" & emptyRow - 1).Value = "USD"
    
    ' Task 17: Copy data from column T of "Calculations2" and their corresponding cells in column V of "Calculations2"
        ' Only copy data if the corresponding cells in column V are not 0 or 0.00
        lastRow = rawDataSheet.Cells(Rows.Count, "T").End(xlUp).Row
        emptyRow = 2 ' Start row in "To SS" sheet
        
        For i = 2 To lastRow
            If rawDataSheet.Cells(i, "V").Value <> 0 Then
                toSSSheet.Cells(emptyRow, "A").Value = rawDataSheet.Cells(i, "T").Value
                toSSSheet.Cells(emptyRow, "B").Value = -rawDataSheet.Cells(i, "V").Value
                toSSSheet.Cells(emptyRow, "F").Value = "Administration Fees of " & Format(DateAdd("m", -1, Date), "mmmm yyyy")
                emptyRow = emptyRow + 1
            End If
        Next i
    
    ' Task 18: Copy data from column T of "Calculations2" and their corresponding cells in column W of "Calculations2"
        ' Only copy data if the corresponding cells in column W are not 0 or 0.00
        lastRow = rawDataSheet.Cells(Rows.Count, "T").End(xlUp).Row
        emptyRow = 2 ' Start row in "To SS" sheet
        
        For i = 2 To lastRow
            If rawDataSheet.Cells(i, "W").Value <> 0 Then
                toSSSheet.Cells(emptyRow, "A").Value = rawDataSheet.Cells(i, "T").Value
                toSSSheet.Cells(emptyRow, "B").Value = -rawDataSheet.Cells(i, "W").Value
                toSSSheet.Cells(emptyRow, "F").Value = "Credit Card Fees of " & Format(DateAdd("m", -1, Date), "mmmm yyyy")
                emptyRow = emptyRow + 1
            End If
        Next i
    
    
        
    ' Task 19:
        Dim cellValue As String
        For Each cell In toSSSheet.Range("A:A")
            If Len(cell.Value) > 0 Then
                cellValue = cell.Value
                cellValue = Replace(cellValue, "[", "")
                cellValue = Replace(cellValue, "]", "")
                cell.Value = cellValue
            End If
        Next cell
        
    ' Task 20:
    For Each cell In toSSSheet.Range("A2:A" & emptyRow - 1)
        If Len(cell.Value) > 0 Then
            cellValue = cell.Value
            cellValue = Left(cellValue, 3)
            cell.Value = cellValue
        End If
    Next cell
    
    ' Task 21: Delete all sheets in the "To SS" workbook
    Dim sheet As Worksheet
    Application.DisplayAlerts = False ' Disable alerts to avoid confirmation prompt
    
    For Each sheet In newWorkbook.Sheets
        If sheet.Name <> "To SS" Then
            sheet.Delete
        End If
    Next sheet
    ' Task 22: Rename the "To SS" sheet to "Sheet 1"
    toSSSheet.Name = "Sheet1"
    
    Application.DisplayAlerts = True ' Enable alerts
    
        
        ' Cleanup
        Application.CutCopyMode = False
        Set currentSheet = Nothing
        Set newWorkbook = Nothing
        Set rawDataSheet = Nothing
        Set toSSSheet = Nothing
    End Sub

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: Tasks in Macro not working

    What do you want tasks 17 and 18 to do?

    What do they do instead?

    Without being able to see your file the comments are of limited use:

    ' Task 17: Copy data from column T of "Calculations2" and their corresponding cells in column V of "Calculations2"
        ' Only copy data if the corresponding cells in column V are not 0 or 0.00
    Does "corresponding cells" mean "the same row"?

    Where do you want to copy the data to?
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    05-22-2019
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    66

    Re: Tasks in Macro not working

    Tasks 17 & 18 are supposed to do the same thing (different ranges). Copy/paste data from sheet “calculations2” to sheet “ToSS”. Both sheets are in the same workbook.

    Currently, all the macro works perfectly, but tasks 17/18 do not copy/paste anything.

    Yes, corresponding meaning the same row

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: Tasks in Macro not working

    It is not possible to test your code without your file and your data. Please consider attaching a file.

    If I were getting no results I would check the values of the following variables using debug during execution.
    ' Task 17: Copy data from column T of "Calculations2" and their corresponding cells in column V of "Calculations2"
        ' Only copy data if the corresponding cells in column V are not 0 or 0.00
        lastRow = rawDataSheet.Cells(Rows.Count, "T").End(xlUp).Row
        emptyRow = 2 ' Start row in "To SS" sheet
        
        For i = 2 To lastRow
            If rawDataSheet.Cells(i, "V").Value <> 0 Then ' is there non-zero numeric data in column V?
                toSSSheet.Cells(emptyRow, "A").Value = rawDataSheet.Cells(i, "T").Value
                toSSSheet.Cells(emptyRow, "B").Value = -rawDataSheet.Cells(i, "V").Value
                toSSSheet.Cells(emptyRow, "F").Value = "Administration Fees of " & Format(DateAdd("m", -1, Date), "mmmm yyyy")
                emptyRow = emptyRow + 1
            End If
        Next i
    
    ' Task 18: Copy data from column T of "Calculations2" and their corresponding cells in column W of "Calculations2"
        ' Only copy data if the corresponding cells in column W are not 0 or 0.00
        lastRow = rawDataSheet.Cells(Rows.Count, "T").End(xlUp).Row ' Why are you getting lastRow again? You already got it above
                                                                     ' not causing a bug but just dead code
        emptyRow = 2 ' Start row in "To SS" sheet
        
        For i = 2 To lastRow
            If rawDataSheet.Cells(i, "W").Value <> 0 Then ' is there non-zero numeric data in column W?
                toSSSheet.Cells(emptyRow, "A").Value = rawDataSheet.Cells(i, "T").Value
                toSSSheet.Cells(emptyRow, "B").Value = -rawDataSheet.Cells(i, "W").Value
                toSSSheet.Cells(emptyRow, "F").Value = "Credit Card Fees of " & Format(DateAdd("m", -1, Date), "mmmm yyyy")
                emptyRow = emptyRow + 1
            End If
        Next i

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 1
    Last Post: 10-05-2022, 02:12 AM
  2. Replies: 4
    Last Post: 06-26-2021, 09:02 AM
  3. [SOLVED] Lookup tasks and sort by date and exclude completed tasks
    By AlexSchmidt in forum Excel Formulas & Functions
    Replies: 14
    Last Post: 10-19-2017, 04:47 PM
  4. Calculate working days between two dates for multiple tasks
    By Quasar82 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-24-2016, 02:31 PM
  5. [SOLVED] How to calculate average total working days for tasks completion
    By Laras in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-19-2013, 06:38 AM
  6. Outlook Tasks - Adding tasks from a worksheet added today or after
    By dpotta in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-28-2010, 07:15 AM
  7. Cell not working with 2 tasks
    By Rjc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-26-2008, 02:43 PM

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