Default value in cell

Bill Morris

New Member
Joined
Aug 1, 2011
Messages
38
Hi

I have a spreadsheet whereby I want a formula ir VBA code to apply depending upon if another cell contains anything.

For example

A1 contain text. In B1 the user can add text, but if it is left blank then I want a dash ("-") as the default value.

If A2 doesn't contain anything then I want B2 to be left blank.

Thanks
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Are you running this "after the fact", or do you want it to run dynamically upon things being entered in A1?
 
Upvote 0
Are you running this "after the fact", or do you want it to run dynamically upon things being entered in A1?

Hi

I don't mind. As long as the empty cell contains the default character(s) when the doc is saved the it doesn't matter.
 
Upvote 0
How about something like this?

If you go into the VB Editor, and place the following code in the ThisWorkbook module, it will run automatically whenever you go to save your file. Note it is written to run against line 1 on Sheet3. Made adjustments as necessary.
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)


'   If cell A1 on Sheet3 is blank, make sure cell B1 is also blank...
    If Sheets("Sheet3").Range("A1") = "" Then
        Sheets("Sheet3").Range("B1") = ""
    Else
'   ... else if A1 is not blank, but B1 is, enter default into cell B1
        If Sheets("Sheet3").Range("B1") = "" Then Sheets("Sheet3").Range("B1") = "-"
    End If
    
End Sub
 
Upvote 0
How about something like this?

If you go into the VB Editor, and place the following code in the ThisWorkbook module, it will run automatically whenever you go to save your file. Note it is written to run against line 1 on Sheet3. Made adjustments as necessary.
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)


'   If cell A1 on Sheet3 is blank, make sure cell B1 is also blank...
    If Sheets("Sheet3").Range("A1") = "" Then
        Sheets("Sheet3").Range("B1") = ""
    Else
'   ... else if A1 is not blank, but B1 is, enter default into cell B1
        If Sheets("Sheet3").Range("B1") = "" Then Sheets("Sheet3").Range("B1") = "-"
    End If
    
End Sub


Thank you for your help....I'll give it a go!
 
Upvote 0
This should do that:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)


    Dim myRange As Range
    Dim cell As Range
        
'   Find last row in column A that is populated and set range
    Set myRange = Range("A1:" & Cells(Rows.Count, "A").End(xlUp).Address)


'   Loop through all cells in range
    For Each cell In myRange
    
'   If column A on Sheet3 is blank, make sure cell column B is also blank...
        If cell = "" Then
            cell.Offset(0, 1) = ""
        Else
'   ... else if column A is not blank, but column B is, enter default into column B
            If cell.Offset(0, 1) = "" Then cell.Offset(0, 1) = "-"
        End If
    Next cell
    
End Sub
 
Upvote 0
This should do that:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)


    Dim myRange As Range
    Dim cell As Range
        
'   Find last row in column A that is populated and set range
    Set myRange = Range("A1:" & Cells(Rows.Count, "A").End(xlUp).Address)


'   Loop through all cells in range
    For Each cell In myRange
    
'   If column A on Sheet3 is blank, make sure cell column B is also blank...
        If cell = "" Then
            cell.Offset(0, 1) = ""
        Else
'   ... else if column A is not blank, but column B is, enter default into column B
            If cell.Offset(0, 1) = "" Then cell.Offset(0, 1) = "-"
        End If
    Next cell
    
End Sub


You, my friend, are a genius! I need to buy myself a VBA book and learn some of this stuff!

Thanks!
 
Upvote 0
could be as simple as

Code:
Set myRange = sheets("Data").Range("A1:" & Cells(Rows.Count, "A").End(xlUp).Address)
 
Upvote 0

Forum statistics

Threads
1,215,452
Messages
6,124,916
Members
449,195
Latest member
Stevenciu

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