VBA IF Cell is NOT Empty input this Text

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
843
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Having trouble coming up with VBA that will kick me out exactly what I need. I am looking for something for example:

If cell B2 is not empty then type this in A2, this in C2. and repeats for B3, B4 until B... is empty.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I think you might be looking for something like this. Let us know if it helps.

VBA Code:
Sub AddStuff()
   
    Dim Sheet As Worksheet
    Dim FoundRange As Range
    Dim LastRow As Long
   
    Set Sheet = Worksheets("Sheet1")
    LastRow = Sheet.Cells(Sheet.Rows.Count, 2).End(xlUp).Row
    If LastRow < 2 Then Exit Sub
   
    On Error Resume Next
    Set FoundRange = Sheet.Range("B2:B" & LastRow).SpecialCells(xlCellTypeConstants)
    On Error GoTo 0
    If FoundRange Is Nothing Then Exit Sub
   
    Sheet.Range("A2:A" & LastRow).ClearContents
    Sheet.Range("C2:C" & LastRow).ClearContents
   
    FoundRange.Offset(0, -1).Value = "AVC"
    FoundRange.Offset(0, 1).Value = "#"
   
End Sub
 
Upvote 0
Ok and if i wanted to add the same logic for other columns would it be something like this:

Sheet.Range("A2:A" & LastRow).ClearContents
Sheet.Range("C2:C" & LastRow).ClearContents
Sheet.Range("D2:D" & LastRow).ClearContents
Sheet.Range("E2:E" & LastRow).ClearContents
Sheet.Range("J2:J" & LastRow).ClearContents
Sheet.Range("M2:M" & LastRow).ClearContents
Sheet.Range("N2:N" & LastRow).ClearContents
Sheet.Range("U2:U" & LastRow).ClearContents

FoundRange.Offset(0, -1).Value = "AVC"
FoundRange.Offset(0, 1).Value = "#"
FoundRange.Offset(0, 2).Value = "12"
FoundRange.Offset(0, 3).Value = "ab"
FoundRange.Offset(0, 4).Value = "NA"
FoundRange.Offset(0, 5).Value = "t"
FoundRange.Offset(0, 6).Value = "US"
 
Upvote 0
Yup. And apologies for the delayed response, I got caught up with work.

Some notes on the previous code. I cleared the contents in case you ran into the case of some values in column B being toggled (empty vs. filled) and wanted the subsequent columns to update accordingly. That was my assumption. You will, however, want to correlate the cleared columns with the offset (based on column B range), which they're not. Since we're finding data in column B, using an Offset(0, -1) would be column A, or one column less, and using Offset(0, 1) would be one column to the right, column C, or the target column number plus one. In other words, you're good through column E in the above code, but column J is 8 columns past column B, so it wouldn't be an offset of 4.

If you're going to do a bunch of these, I would think about writing a separate routine to handle them. Would make the code look a little nicer too. This got fleshed out a bit more, as is usual in making robust and agnostic code, but it should run pretty fast regardless.

VBA Code:
Sub AddStuff()
   
    Dim Sheet As Worksheet
    Dim FoundRange As Range
    Dim LastRow As Long
   
    Set Sheet = Worksheets("Sheet1")
    LastRow = Sheet.Cells(Sheet.Rows.Count, 2).End(xlUp).Row
    If LastRow < 2 Then Exit Sub
   
    On Error Resume Next
    Set FoundRange = Sheet.Range("B2:B" & LastRow).SpecialCells(xlCellTypeConstants)
    On Error GoTo 0
    If FoundRange Is Nothing Then Exit Sub
   
    UpdateColumnValues FoundRange, "A", "AVC", LastRow, 2, Sheet, True
    UpdateColumnValues FoundRange, "C", "#", LastRow, 2, Sheet, True
    UpdateColumnValues FoundRange, "D", "12", LastRow, 2, Sheet, True
    UpdateColumnValues FoundRange, "E", "ab", LastRow, 2, Sheet, True
    UpdateColumnValues FoundRange, "J", "NA", LastRow, 2, Sheet, True
    UpdateColumnValues FoundRange, "M", "t", LastRow, 2, Sheet, True
    UpdateColumnValues FoundRange, "N", "US", LastRow, 2, Sheet, True
    UpdateColumnValues FoundRange, "U", "", LastRow, 2, Sheet, True
   
End Sub


Sub UpdateColumnValues( _
    ByVal EmulationRange As Range, _
    ByVal ColumnNumberOrLetter As Variant, _
    ByVal ColumnValue As Variant, _
    ByVal ColumnLastRow As Long, _
    Optional ByVal ColumnStartRow = 2, _
    Optional ByVal ColumnSheet As Worksheet, _
    Optional ByVal Status As Boolean = False _
    )
    
    
    Dim Completed As Boolean
    Dim ColumnOffset As Long
    
    If ColumnSheet Is Nothing Then
        If ActiveSheet Is Nothing Then
            GoTo Finish
        End If
        Set ColumnSheet = ActiveSheet
    End If
    
    On Error Resume Next
    ColumnOffset = GetColumnNumber(ColumnNumberOrLetter) - EmulationRange.Column
    ColumnSheet.Range(ColumnSheet.Cells(ColumnStartRow, EmulationRange.Column), ColumnSheet.Cells(ColumnLastRow, EmulationRange.Column)).Offset(0, ColumnOffset).ClearContents
    EmulationRange.Offset(0, ColumnOffset).Value = ColumnValue
    On Error GoTo 0
    
    If Err.Number > 0 Then GoTo Finish
    Completed = True
    
Finish:
    
    If Status Then
        If Completed Then
            Debug.Print "Process completed successfully for column '" & ColumnNumberOrLetter & "' on '" & ColumnSheet.Name & "' with a value of '" & ColumnValue & "'."
        Else
            Debug.Print "Process incomplete for column '" & ColumnNumberOrLetter & "' on '" & ColumnSheet.Name & "'."
        End If
    End If
    
End Sub


Public Function GetColumnNumber( _
        ByVal ColumnLetter As Variant _
    ) As Long
    Dim Column As Range
    If Not ValidColumnLetter(ColumnLetter) Then Exit Function
    Set Column = ThisWorkbook.Worksheets(1).Cells(1, ColumnLetter)
    GetColumnNumber = Column.Column
End Function


Public Function ValidColumnLetter( _
        ByVal ColumnLetter As Variant _
    ) As Boolean
    Dim Column As Range
    On Error Resume Next
    Set Column = ThisWorkbook.Worksheets(1).Cells(1, ColumnLetter)
    ValidColumnLetter = Not Column Is Nothing
End Function
 
Upvote 0
Solution
Now that is HUGE. I did notice when running the other code at times my column B would randomly have Columns E in it. so is that what you are referring to? This is what i have atm. I ran it about 20 times just now and it hasn't reoccurred.

Sheet.Range("A2:A" & LastRow).ClearContents
Sheet.Range("C2:C" & LastRow).ClearContents
Sheet.Range("D2:D" & LastRow).ClearContents
Sheet.Range("E2:E" & LastRow).ClearContents
Sheet.Range("J2:J" & LastRow).ClearContents
Sheet.Range("M2:M" & LastRow).ClearContents
Sheet.Range("N2:N" & LastRow).ClearContents
Sheet.Range("T2:T" & LastRow).ClearContents
Sheet.Range("U2:U" & LastRow).ClearContents

FoundRange.Offset(0, -1).Value = "AVC"
FoundRange.Offset(0, 1).Value = "F"
FoundRange.Offset(0, 2).Value = "#"
FoundRange.Offset(0, 3).Value = "12"
FoundRange.Offset(0, 8).Value = "ab"
FoundRange.Offset(0, 11).Value = "NA"
FoundRange.Offset(0, 12).Value = "t"
FoundRange.Offset(0, 18).Value = Format(Now, "MM/DD/YYYY")
FoundRange.Offset(0, 19).Value = "USD"

ActiveSheet.Copy
sbSaveExcelDialog
 
Upvote 0

Forum statistics

Threads
1,223,313
Messages
6,171,369
Members
452,397
Latest member
ddneptune

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top