Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Clear Cell Contents on VBA

Status
Not open for further replies.

mitzen

Structural
Jul 13, 2016
5
0
0
PH
Hi i'm new on vba and want to clear my inputs and autosave on one click.

i have tried this but have an inconsistent results (will clear on exit and sometimes retain its info.)
does anyone who's a vba expert that is willing to help :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub Button1_Click()
Range("C18").ClearContents
Range("C21").ClearContents
Range("G6").ClearContents
Range("G7").ClearContents
Range("G8").ClearContents
Range("G22").ClearContents
MsgBox "Thank You"
Application.Quit
Application.DisplayAlerts = False
ActiveWorkbook.Close
If ThisWorkbook = False Then
ThisWorkbook = Save
End If
End Sub
 
Replies continue below

Recommended for you

It's because of the ActiveWorkbook.Close event is before the ThisWorkbook save event, so it's closing without saving

Code:
Private Sub Button1_Click()
Range("C18").ClearContents
Range("C21").ClearContents
Range("G6").ClearContents
Range("G7").ClearContents
Range("G8").ClearContents
Range("G22").ClearContents
MsgBox "Thank You"
ThisWorkbook.Save
Application.Quit
End Sub
 
Hiding the alert is something like 'Application.DisplayAlerts = False' (place it just after the MsgBox, I guess).
 
hi LRJ, tnx for the reply... kris suggestion is working :)

i have another problem...

how to select cells to print? i am stuck with only 1 cell selected LOL :D

my code below

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub commandbutton2_click()

Dim filename As String, lineText As String
Dim myrng As Range

filename = ThisWorkbook.Path & "\Results-" & Format(Now, "ddmmyy") & ".txt"

Open filename For Output As #1

Set myrng = Range("A17")

For i = 1 To myrng.Rows.Count
For j = 1 To myrng.Columns.Count
lineText = IIf(j = 1, "", lineText & ",") & myrng.Cells(i, j)
Next j
Print #1, lineText
Next i

Close #1

End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

i have tried to

Set myrng = Range(Cells(1, 4), Cells(12, 1))

i don't know why it only reads up to 12th row... type mismatch will occur if i go more than 12 rows.


and if you are too kind to share, is it possible to automatically open the csv/text file after this code? many thanks in advance...
 
Apologies - I misunderstood your post and didn't read the previous answer fully (i.e. the part with the same code as I suggested). D'oh!

Regarding your code above, I wonder if this part is correct:

Range(Cells(1, 4), Cells(12, 1)

This goes through the range D1:A12 - I suspect VBA will struggle with this shape of array.

To open files you use: Workbooks.Open - see here:
 
Excel ranges are defined from the Top Left cell to the Bottom Right cell. In fact, both TopLeft and BottomRight are properties of ranges. So your range is A1:D12.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Status
Not open for further replies.
Back
Top