Results 1 to 3 of 3

Macro to Clear Text and then insert text and sum

Threaded View

Howardc1001 Macro to Clear Text and then... 01-02-2024, 05:12 AM
ByteMarks Re: Macro to Clear Text and... 01-02-2024, 10:19 AM
Howardc1001 Re: Macro to Clear Text and... 01-02-2024, 11:32 AM
  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,873

    Macro to Clear Text and then insert text and sum

    I have tried to write code to do the following:

    1) I need VBA to first clear "Variance" in Col V if it exists as well as value in same row in Col W
    2) I then need to insert "Variance" after last item in Col V on sheet "Data" and insert formula in Col W in the same rows as this text. The formula must = one row above Col W -2 rows above Col W
    If "Variance" is in say V4, then formula must be =W3-W2 for example


    Where "Variance" is in Col V, then code is inserting "Variance" 2 rows below last item when run a second time

    See my code below

     Sub ClearAndInsertVarianceAndFormula()
        Dim lastDataRow As Long
        Dim varianceRow As Long
        
        ' Set the sheet and column
        Dim ws As Worksheet
        Set ws = ThisWorkbook.Sheets("Data")
        
        ' Find the last row in Column V with data
        lastDataRow = ws.Cells(ws.Rows.Count, "V").End(xlUp).Row
        
        ' Check if "Variance" already exists
        Dim varianceCell As Range
        Set varianceCell = ws.Columns("V").Find(What:="Variance", LookIn:=xlValues, LookAt:=xlWhole)
        
        If Not varianceCell Is Nothing Then
            ' Get the row where "Variance" exists
            varianceRow = varianceCell.Row
            
            ' Clear "Variance" and value in Column W in the same row
            ws.Cells(varianceRow, "V").ClearContents
            ws.Cells(varianceRow, "W").ClearContents
        End If
        
        ' Insert "Variance" one row below the last row with data in Column V
        ws.Cells(lastDataRow + 1, "V").Value = "Variance"
        
        ' Set the row where "Variance" is inserted
        varianceRow = lastDataRow + 1
        
        ' Insert formula in Column W for the same row
        ws.Cells(varianceRow, "W").Formula = "=" & ws.Cells(varianceRow - 1, "W").Address & "-" & ws.Cells(varianceRow - 2, "W").Address
    End Sub
    Last edited by Howardc1001; 01-02-2024 at 05:31 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Macro to insert blank rows below the text in Col D and copy the text in the new row
    By rehana402003 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-16-2018, 02:25 PM
  2. Macro - Insert text based on text in a list
    By sedanstr in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-13-2017, 05:02 PM
  3. [SOLVED] Look for specific text string, insert row below, insert text to that row
    By jwats in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-19-2015, 03:08 PM
  4. Macro that will insert a simple text string in top cell of text file
    By Davek11 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-25-2014, 07:31 PM
  5. IF Macro, insert text
    By jamessaunders in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-12-2007, 08:01 AM
  6. Replies: 2
    Last Post: 10-05-2005, 06:05 PM
  7. Need a macro to insert text in a cell that already has text.Excel
    By go1angel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-04-2005, 02:05 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