I was trying to understand how to skip the 1st row (A) of my sheet when i use below code which check whether any value available on my excel sheet.
Sub IsActiveSheetEmpty()
If WorksheetFunction.CountA(Cells) = 0 Then
MsgBox ActiveSheet.Name & " is empty"
Else
MsgBox ActiveSheet.Name & " is not empty"
End If
End Sub
Thanks in Advance
Try the following (does implicitly reference active sheet so you may want to explicitly state the worksheet name.) It will use the last row and column for which ever version of Excel you are using:
Sub IsActiveSheetEmpty()
If WorksheetFunction.CountA(Range(Cells(2, "A"), Cells(Cells.Rows.Count, Cells.Columns.Count))) = 0 Then
MsgBox ActiveSheet.Name & " is empty"
Else
MsgBox ActiveSheet.Name & " is not empty"
End If
End Sub
Option Explicit
Sub IsActiveSheetEmpty()
Dim myRange As Range
Set myRange = Range("A2",Cells(2,1).SpecialCells(XlCellType.xlCellTypeLastCell))
If WorksheetFunction.CountA(myRange) = 0 Then
MsgBox ActiveSheet.Name & " is empty"
Else
MsgBox ActiveSheet.Name & " is not empty"
End If
End Sub
Seems like it should be a boolean-returning function rather than sub, but:
Option explicit
Sub IsActiveSheetEmpty()
Dim ws as worksheet
Set ws = Activesheet
With ws
If application.CountA(.range(.range("B1"),.cells(.rows.count,.columns.count))) = 0 Then
MsgBox .Name & " is empty"
Else
MsgBox .Name & " is not empty"
End If
End with
End Sub
Untested, written on mobile. Does it do what you want?
Edit: Think you mean column A?
Sub IsActiveSheetEmpty()
If WorksheetFunction.CountA("A2:A5") = 0 Then 'skips A1
MsgBox ActiveSheet.Name & " is empty"
Else
MsgBox ActiveSheet.Name & " is not empty"
End If
End Sub
Related
Sub lookuphcpcs()
On Error GoTo errorbox:
Dim hcpcs_code As Long
Dim desc As Variant
hcpcs_code = ActiveCell.Value
If Len(hcpcs_code) > 0 Then
desc = Application.WorksheetFunction.VLookup(Active_cell, 'C:\Users\Username\Desktop\[Fruit Code.xlsx]Sheet1'!$A$2:$B$7, 2, False)
MsgBox "Description for HCPCS Code " & hcpcs_code & " is """ & desc & """"
Else
MsgBox "You did not enter any input!"
End If
Exit Sub
errorbox:
If Err.Number = 1004 Then
MsgBox "No Description found under HCPCS list!"
End If
End Sub
I am not able to put table array value under Vlookup in VBA to point to another excel sheet.
How do I do that?
First, when working with Vlookup you need to handle errors, such as when Vlookup was unable to find a match, you can use If Not IsError(Application.VLookup(.... to achieve this.
Second, in your case you don't need to use On Error GoTo errorbox:, just use the Vlookup error handling I wrote in the first point.
Third, you can use If Trim(ActiveCell.Value2) <> "" Then to verify there is a valid text or number inside ActiveCell rather than empty spaces.
Fourth, you should avoid using ActiveCell, and use fully qualified object instead.
Last, you want to make sure "Fruit Code.xlsx" workbook is open before using the Vlookup, as suggested by #Tim Williams in the comments above.
Modified Code
Option Explicit
Sub lookuphcpcs()
Dim desc As Variant
Dim SourceWb As Workbook
' error trapping in case Fruit Code workbook is closed
On Error Resume Next
Set SourceWb = Workbooks("Fruit Code.xlsx")
On Error GoTo 0
If SourceWb Is Nothing Then
Set SourceWb = Workbooks.Open("C:\Users\Username\Desktop\Fruit Code.xlsx") ' open workbook if it's closed
End If
If Trim(ActiveCell.Value2) <> "" Then ' make sure cell has a string other than space
If Not IsError(Application.VLookup(ActiveCell.Value2, SourceWb.Sheets("Sheet1").Range("A2:B7"), 2, 0)) Then
desc = Application.VLookup(ActiveCell.Value2, SourceWb.Sheets("Sheet1").Range("A2:B7"), 2, 0)
MsgBox "Description for HCPCS Code " & ActiveCell.Value2 & " is """ & desc & """"
Else
MsgBox "No Description found under HCPCS list!"
Exit Sub
End If
Else
MsgBox "You did not enter any input!"
End If
End Sub
I have a excel file and there are 20 sheets
I need to search and find several sheet on my excel that want and add email to M1 Cell
Could you please help me.
Application.ScreenUpdating = False
On Error Resume Next
Application.DisplayAlerts = False
döngü:
For i = 1 To Worksheets.Count
If Worksheets(i).Name = "ABC" Then
Sheets("ABC").Select
Range("M1").Select
ActiveCell.FormulaR1C1 = "abc#hotmail.com"
If Worksheets(i).Name = "ABC2" Then
Sheets("ABC2").Select
Range("M1").Select
ActiveCell.FormulaR1C1 = "abc2#hotmail.com"
GoTo döngü:
pass:
Next i
Application.ScreenUpdating = True
MsgBox "mail assign done"
End Sub
You can simply use below sub.
Sub WriteEmail()
On Error GoTo HarunErrHandler
Sheets("ABC").Range("M1") = "abc#hotmail.com"
Sheets("ABC2").Range("M1") = "abc2#hotmail.com"
Exit Sub
HarunErrHandler:
MsgBox "No such sheet found.", vbInformation, "Info"
'MsgBox("Error Number: " & Err.Number & vbCrLf & Err.Description, vbCritical, "Error")
End Sub
Many thanks Harun i made a little change with your comments.
Following code worked well.
Thank you so much
Sub WriteEmail()
Sheets("ABC").Range("M1") = "abc#hotmail.com"
On Error Resume Next
Sheets("ABC2").Range("M1") = "abc2#hotmail.com"
On Error Resume Next
Sheets("ABC3").Range("M1") = "abc3#hotmail.com"
On Error Resume Next
MsgBox ("process done")
End Sub
I'm not sure why, but when my if statement is false and my msgbox is called I have to click okay 4 times for it to go away. How do I correct this?
My code:
Option Explicit
Private tskCol As String
Private unitCol As String
Private Sub txtTask_Change()
End Sub
Public Sub UserForm_Initialize()
tskCol = Application.InputBox("Enter Column Letter for Task Names", Type:=2)
unitCol = Application.InputBox("Enter Column Letter for Number of Units", Type:=2)
End Sub
Private Sub cmdAdd_Click()
Dim LastRow As Long, i As Long
LastRow = ActiveSheet.Range(tskCol & Rows.Count).End(xlUp).Row
'Copy input values to sheet
For i = 2 To LastRow
If CStr(ActiveSheet.Range(tskCol & i).Value) = CStr(Me.txtTask.Value) Then
ActiveSheet.Range(unitCol & i).Value = Me.txtQuantity.Value
Else
MsgBox "Task Not Found!"
End If
Next i
'Clear input controls
Me.txtTask.Value = ""
Me.txtQuantity.Value = ""
End Sub
To answer your specific question, try changing this line on the Else statement:
Msgbox "Task Not Found"
to
If i = LastRow Then Msgbox "Task Not Found"
You might also want to put Exit For if the task is found. Refactoring IF statement:
If CStr(ActiveSheet.Range(tskCol & i).Value) = CStr(Me.txtTask.Value) Then
ActiveSheet.Range(unitCol & i).Value = Me.txtQuantity.Value
Exit For '/* no need to continue the loop, task is found */
Else
'/* only call msgbox if all rows are processed */
If i = LastRow Then Msgbox "Task Not Found"
End If
My code is fairly simple but a bit puzzling. I might be committing a minor error - pardon my newbie-ness. The Sheets.Add.Name line still gets executed despite having Boolean = True, thus a new worksheet is created with the Sheet# naming convention.
Sharing my code:
Private Sub create_analyst_btn_Click()
Dim strUser As String
Dim DateToday As String
Dim ws As Worksheet
Dim boolFound As Boolean
strUser = newanalyst_form.user_User.Value
For Each ws In Worksheets
If ws.Name Like strUser Then boolFound = True: Exit For
Next
If boolFound = True Then
MsgBox ("User already exists.")
Else
DateToday = Format(Date, "-yyyy-mm-dd")
Sheets.Add.Name = strUser & DateToday
Unload Me
End If
End Sub
I don't see the point of the first If statement and I would refactor your code to the following:
For Each ws In Worksheets
If ws.Name Like "*" & strUser & "*" Then
MsgBox ("User already exists.")
Exit For
Else
DateToday = Format(Date, "-yyyy-mm-dd")
Sheets.Add.Name = strUser & DateToday
Unload Me
End If
Next ws
The logic here is that if the name already exists before calling the subroutine, we would discover this while iterating, display a warning message in an alert box, and exit. Otherwise, the name/date would be added to the sheet.
I am trying to determine by VBA in Excel 2013 if the ActiveCell is not just in any table, but in a specific table.
Below is the code as is, but only detects ActiveCell being in any table. The commented out line is what I'm looking for, but obviously it doesn't work.
...
Set rng = Intersect(.EntireRow, ActiveCell.ListObject.DataBodyRange)
'Set rng = Intersect(.EntireRow, ActiveCell.ListObjects("myTable").DataBodyRange)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "Please select the cell of a row within the consensus input table.", vbCritical, "Delete Evaluator"
Else
...
Any suggestions on the right syntax for this?
Thanks!
To test if ActiveCell is in the body of Table1:
Sub qwerty()
If Intersect(ActiveCell, ActiveSheet.ListObjects("Table1").DataBodyRange) Is Nothing Then
MsgBox "activecell not in Table1"
Else
MsgBox "activecell in Table1"
End If
End Sub
Generally, we're interested in work being performed within the DataBodyRange of a table and Excel provides us a shortcut for that area of a Table. For a table named "myTable", you directly access the DataBodyRange in code using [myTable].
Thus, for inclusive table location testing of the ActiveCell one could test as follows:
If Not Intersect(ActiveCell, [myTable]) Is Nothing Then
A more general solution, adaptable to other tables
Sub Demo()
Dim r As Range
Dim lo As ListObject
Set r = ActiveCell
Set lo = r.ListObject
If Not lo Is Nothing Then
Select Case lo.Name
Case "Table1"
If r.Row = lo.Range.Row Then
MsgBox "In Table1 Header"
Else
MsgBox "In Table1 Body"
End If
Case "SomeOtherTable"
'...
End Select
Else
MsgBox "Not in any table"
End If
End Sub
A Range object has a ListObject property that will return the table of the Range. All you have to do is to test if the cell is in any table:
If ActiveCell.ListObject Is Nothing Then
...
and see if it is in your specific table:
If ActiveCell.ListObject.Name = "MyTable" Then
...
and you're done!
Much cleaner than using Application.Intersect(...). chris neilsen's answer alludes to this as well.
I use the following line of code:
ActiveCell.ListObject.Name
or sub:
Sub IsActiveCellInTable()
'If active cell in table then get name'
Dim strTblName As String
'Disable error checking'
On Error Resume Next
strTblName = ActiveCell.ListObject.Name
'Reset error checking'
On Error GoTo 0
If strTblName <> "" Then
MsgBox "Cell (" & ActiveCell.Address & ") is included in: " & strTblName
Else
MsgBox "Cell (" & ActiveCell.Address & ") is not included in table."
End If
End Sub