admin管理员组文章数量:1405995
I tried to find the answer before writing my question, but without success.
I need to check if cell, let's say C1, is empty (no values) in all sheets in some excel file and I wan't to see message box with sheets names where C1 is not empty.
Reason for it is, I have file with lots of sheets and now I need to add some links, but not every list have same "form" and there are some list also which I don't need for it so I need to check those two things first.
So far I have this code, but something is wrong, because I always see message box with text: Cell C1 is empty in all sheets., even if I have some values in C1 (I add them in 3 sheets, because of testing this code).
Sub CheckCellC1()
Dim ws As Worksheet
Dim nonEmptyCell As String
nonEmptyCell = ""
' Browse all sheets in the current workbook
For Each ws In ThisWorkbook.Worksheets
' Check if cell C1 is empty
If ws.Range("C1").Value <> "" Then
nonEmptyCell = nonEmptyCell & ws.Name
End If
Next ws
' Result
If nonEmptyCell = "" Then
MsgBox "Cell C1 is empty in all sheets."
Else
MsgBox "Cell C1 isn't empty in those sheets:" & nonEmptyCell
End If
End Sub
What am I missing?
Thank you in advance for your help.
Update: I run my code from a PERSONAL.xlsb file and during "testing" I had open only one excel file.
Really only problem with my code was using ThisWorkbook
instead of ActiveWorkbook
. Adding & vbLf
on the end of the line nonEmptySheets = nonEmptySheets & ws.Name & vbLf
help a lot with message box imput, is easier to read now.
I'm beginner in VBA and always do mistakes like this (I have a bit problem to understand difference between "places" where code is and how it can impact code itself, but that is another topic), so thank you all for helping me with this.
I tried to find the answer before writing my question, but without success.
I need to check if cell, let's say C1, is empty (no values) in all sheets in some excel file and I wan't to see message box with sheets names where C1 is not empty.
Reason for it is, I have file with lots of sheets and now I need to add some links, but not every list have same "form" and there are some list also which I don't need for it so I need to check those two things first.
So far I have this code, but something is wrong, because I always see message box with text: Cell C1 is empty in all sheets., even if I have some values in C1 (I add them in 3 sheets, because of testing this code).
Sub CheckCellC1()
Dim ws As Worksheet
Dim nonEmptyCell As String
nonEmptyCell = ""
' Browse all sheets in the current workbook
For Each ws In ThisWorkbook.Worksheets
' Check if cell C1 is empty
If ws.Range("C1").Value <> "" Then
nonEmptyCell = nonEmptyCell & ws.Name
End If
Next ws
' Result
If nonEmptyCell = "" Then
MsgBox "Cell C1 is empty in all sheets."
Else
MsgBox "Cell C1 isn't empty in those sheets:" & nonEmptyCell
End If
End Sub
What am I missing?
Thank you in advance for your help.
Update: I run my code from a PERSONAL.xlsb file and during "testing" I had open only one excel file.
Really only problem with my code was using ThisWorkbook
instead of ActiveWorkbook
. Adding & vbLf
on the end of the line nonEmptySheets = nonEmptySheets & ws.Name & vbLf
help a lot with message box imput, is easier to read now.
I'm beginner in VBA and always do mistakes like this (I have a bit problem to understand difference between "places" where code is and how it can impact code itself, but that is another topic), so thank you all for helping me with this.
Share Improve this question edited Mar 7 at 8:26 Cz_Libu asked Mar 6 at 14:22 Cz_LibuCz_Libu 216 bronze badges 5 |1 Answer
Reset to default 1Check the Same Cell in All Worksheets
- Your code probably runs on the wrong workbook as suggested by BigBen in the comments.
- You could probably only change
ThisWorkbook
toActiveWorkbook
in your code and it would work. - The following is no significant improvement on your code but contains a few tips in the comments.
- First run it as is, with
ThisWorkbook
. The message box should clarify what's happening since it lists the workbook path.
Sub CheckCellInSheets()
' Define constants.
Const CELL_ADDRESS As String = "C1"
Const DELIMITER As String = vbLf ' line feed ('next row')
' Use only one of the following 3 (probably 3):
' 1.) Reference the workbook containing this code.
' You cannot use this if the code is in another workbook!
Dim wb As Workbook: Set wb = ThisWorkbook
' 2.) Reference the workbook by its name.
' Used when the code is in another workbook and you know
' its name and that it is open!
'Dim wb As Workbook: Set wb = Workbooks("MyBook.xlsx")
' 3.) Reference the active workbook, the one you're looking at.
' Used when the workbook could have any name
' and the code is in another workbook!
' The code can often be run on any workbook!
'Dim wb As Workbook: Set wb = ActiveWorkbook ' active workbook
' Declare additional variables.
Dim ws As Worksheet, NonBlankSheetList As String, IsFirstFound As Boolean
' Loop through all worksheets of the workbook.
For Each ws In wb.Worksheets
' Check if the cell is blank.
' 'CStr' prevents error if error value in cell.
If CStr(ws.Range(CELL_ADDRESS).Value) <> "" Then
' Check if the cell is (truly) empty.
'If IsEmpty(ws.Range(CELL_ADDRESS).Value) Then
If IsFirstFound Then ' all after first
NonBlankSheetList = NonBlankSheetList & DELIMITER & ws.Name
Else ' first
NonBlankSheetList = ws.Name
IsFirstFound = True
End If
End If
Next ws
' Inform.
If IsFirstFound Then
MsgBox "Cell """ & CELL_ADDRESS _
& """ isn't blank in the following sheets of workbook """ _
& wb.FullName & """:" & vbLf & vbLf _
& NonBlankSheetList, vbInformation
Else
MsgBox "Cell """ & CELL_ADDRESS _
& " is blank in all sheets of workbook """ _
& wb.FullName & """!", vbExclamation
End If
End Sub
本文标签: excelCheck if cell C1 is empty in all sheetsStack Overflow
版权声明:本文标题:excel - Check if cell C1 is empty in all sheets - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744970032a2635179.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
""
? Are you running this code from a PERSONAL.xlsb file or an .xlam add-in? – BigBen Commented Mar 6 at 14:24Len(ws.Range("C1")) = 0
? – Dominique Commented Mar 6 at 14:25IsEmpty
for checking if a cell is truly empty. – BigBen Commented Mar 6 at 14:29nonEmptyCell = nonEmptyCell & ws.Name
will give you an output something like Sheet1Sheet2Sheet3 add& vbcr
to the end of that line to put each sheet name on a new row. – Darren Bartrup-Cook Commented Mar 6 at 14:44nonEmptyCell = nonEmptyCell & vbLf & ws.Name & vbTab & "|" & ws.Range("C1").Value & "|"
– CDP1802 Commented Mar 6 at 15:21