Search code examples
excelvbacopy-paste

Copy blank/empty cell and pastespecial add to entire column by column header name


I'm trying write a MACRO to do the following (As a small portion of a large MACRO project):

(Consider that I will have a varying number of COLUMNS and ROWS each time the report is generated, so, the columns needed manipulated will be different each time. This is why I need to use Header names.)

  1. Select a BLANK cell within the existing report data
  2. COPY the cell
  3. SELECT all data, except for header, in COLUMN where header name = "PROC#/REV CODE"
  4. PASTE SPECIAL ADD (this step is to get two separate sets of data into the same (EQUAL IF) comparable data type.
  5. REPEAT this entire process for COLUMN where header name = "FEE RATE".

Recorded MACRO looks like this (need to replace "RANGE("K2")" with unknown EMPTY cell location. Need to replace "Range("G2:G4786")" with Column Header NAME):

Sub CopySpacePasteAdd()
Range("K2").Select
Selection.Copy
Range("G2").Select
A
Range("G2:G4786").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _
    False, Transpose:=False
Application.CutCopyMode = False

Thank you SO much for any assistance!


Solution

  • Edit; This code is not that pretty, but it will convert text into numbers for the range below the specific headers.

    Dim lRow As Long
    
    lRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = 1 To Columns.Count
        If Cells(1, i) = "PROC#/REV CODE" Then
                With Cells(1, i).Offset(1).Resize(lRow)
                    .NumberFormat = "General"
                    .Value = .Value
                End With
    
        ElseIf Cells(1, i) = "FEE RATE" Then
                With Cells(1, i).Offset(1).Resize(lRow)
                    .NumberFormat = "General"
                    .Value = .Value
                End With
    
        Exit For
    
        End If
    Next