Schadrack Rurangwa Schadrack Rurangwa - 7 months ago 38
Vb.net Question

How to set condition in excel vba based on cell value?

I want to ask one question.
I have codes of how I can save last row of excel in vba,
But in my codes I want to add one condition where for example last row selected will be saved if cell.value of column E = "disable" then

'save like this
WB.SaveAs filename:="C:\New Folder\" & filename, FileFormat:=51

End If

This is my codes

Sub Increment()
' change A1 to the cell you wish to Increment
Range("A1").Value = Range("A1").Value + 1
End Sub

Private Sub CommandButton1_Click()
Call SaveLastLine
End Sub

Sub SaveLastLine()

'Variable declaration
Dim score As String
Dim WB As Workbook, _
filename As String

'Turn off screen updating and alerts
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'Make a copy of the last line of Active Sheet

newName = ActiveSheet.Name
Range("B1").Select
Selection.End(xlDown).Select
Range(Selection, Selection.End(xlToRight)).Select

Selection.Copy
Workbooks.Add
ActiveSheet.Paste

Call Increment

FileNane1 = Range("A1").Value

Set WB = ActiveWorkbook
filename = newName & FileNane1 & ".xlsx"
WB.SaveAs filename:="C:\New Folder\" & filename, FileFormat:=51

'Restore screen updating and alerts

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub


Is there someone who can help me to add one condition in my vba codes?

Please anyone can help me.

Answer
FileName1 = Range("A1").Value
if FileName = "disable" then
    Set WB = ActiveWorkbook
    filename = newName & FileNane1 & ".xlsx"
    WB.SaveAs filename:="C:\New Folder\" & filename, FileFormat:=51
end if