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.
 
The way the code is in your last post, you'd basically have to make two calls and they would have to match in locations. This is not ideal programming. We should call it once and have it run the action based on a single [column] input. This reduces the potential for error and narrows the view for debugging/maintenance. In other words, better practices. I'd recommend ditching the above and going with the latest code i sent. The bottom 3 routines are just helper and you should only focus on the top routine. It may look like a lot but it's not really. It's more fleshed out because of it being a bit more robust and I handled some errors in it.
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Only issue is im getting a syntax error on this one. just want it to tell me current day

UpdateColumnValues FoundRange, "T", "Format(Now, "MM/DD/YYYY")", LastRow, 2, Sheet, True
 
Upvote 0
Take the quotes off the Format function.

VBA Code:
UpdateColumnValues FoundRange, "T", Format(Now, "MM/DD/YYYY"), LastRow, 2, Sheet, True
 
Upvote 0
Any idea how it is putting 8/31/1987 when doing that?

UpdateColumnValues FoundRange, "T", Format(Now, "MMDDYY"), LastRow, 2, Sheet, True
 
Upvote 0
You have a format applied to that cell range (column T). The value passed by
VBA Code:
Format(Now, "MMDDYY")
is "032020", which is a string, but when applied to the grid Excel see's it as numeric and applies that. The cells also have a short date format applied, so you see the date of August 31, 1987, or 8/31/1987. So the result is expected and correct. If you want the cell to show "32020" (without quotation marks) then change the 'ClearContents' portion of the code to 'Clear', which will clear all values and formats.
 
Upvote 0
Yup that's it I had it as date but was inputting a value. So I changed it to MM/DD/YYYY. I kept the clear contents.

I have another small code that will open a dialogbox and set the file as csv is there a way to pre-populate the final name? when it does.

Dim wb As Boolean
ChDir "MY PATH"
wb = Application.Dialogs(xlDialogSaveAs).Show(Arg2:=xlCSV)
 
Upvote 0
You can with Application.GetSaveAsFileName(). Here's an example of prompting for a filename and saving a copy. This assumes the workbook structure is valid for CSV file format.

VBA Code:
Sub GetNameAndSaveAsCSV()

    Dim TargetBook As Workbook
    Dim TargetPath As String
    Dim TargetName As String
    Dim SaveName As Variant
    
    TargetPath = "C:\"
    TargetName = "Initial filename.csv"
    
    SaveName = Application.GetSaveAsFilename(InitialFileName:=TargetPath & TargetName, FileFilter:="CSV File (*.csv),*.csv")
    If SaveName = False Then
        ' User cancelled
    Else
        
        If ExistingFile(SaveName) Then
            ' File already exists...
        Else
            
            ThisWorkbook.SaveCopyAs SaveName
            
        End If
        
    End If
    
End Sub
 
Upvote 0
This part is giving me a compile error:

If ExistingFile(SaveName) Then
' File already exists...
 
Upvote 0
Ah, right, it's a separate function call. Here is the function:

VBA Code:
Public Function ExistingFile( _
       ByVal FilePath As String _
       ) As Boolean
    Dim Attributes As Integer
    On Error Resume Next
    Attributes = GetAttr(FilePath)
    ExistingFile = (Err.Number = 0) And (Attributes And vbDirectory) = 0
    Err.Clear
End Function
 
Upvote 0

Forum statistics

Threads
1,223,315
Messages
6,171,394
Members
452,398
Latest member
InvoicingNoob

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