Vba MsgBox - Question

Cuzzaa

Board Regular
Joined
Apr 30, 2019
Messages
86
Hi guys

I am using the below code to import a specific range of data from another spreadsheet into my current workbook I have open. The below code works perfectly.

However, what I am trying to achieve is to amend the code so that my current Msg box presents the following:

Instead of simply just saying 'BOM Import Successful' I would like it to say 'BOM Import Successful' and then on a new line I'd like to add some text, for e.g. 'x number' of items were not found. X number being the number of occurrences that contain the text 'Item Not Found' in data range J14:J64 (sheet is called 'Dashboard').

However, if the count finds 0 occuraces of 'Item Not Found' in data range J14:J64 then I would like the Msg box to simply say the following:

'BOM Import Successful' and then on a new line underneath 'All items were successfully imported!'

Is anyone able to help amend my code please?

Code:
Sub BOMTest()


' Get customer workbook...
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook


' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook


' get the customer workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please select the BOM "
customerFilename = Application.GetOpenFilename(filter, , caption)


Set customerWorkbook = Application.Workbooks.Open(customerFilename)


' assume range is A1 - C10 in sheet1
' copy data from customer to target workbook
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)


targetSheet.Range("F14", "I48").Value = sourceSheet.Range("A2", "K48").Value


' Close customer workbook
customerWorkbook.Close


targetSheet.Range("F7").Value = customerFilename


MsgBox "BOM Import Successful!", vbInformation
End Sub
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try this

Code:
Sub x()
Dim NumItems As Integer


NumItems = 4
MsgBox "BOM Import Successful!" & vbNewLine & NumItems & " number' of items were not found.", vbInformation,ThisWorkbook.Name
End Sub
 
Last edited:
Upvote 0
another option for you to test

Code:
[I][COLOR=#006400]'declare variables[/COLOR][/I]
Dim aCount As Integer, msg As String
Const msg1 = "BOM Import Successful" & vbCr
Const msg2 = " items not found"
Const msg3 = "All items were successfully imported!"


[COLOR=#006400][I]'use count to determine message string[/I][/COLOR]
aCount = WorksheetFunction.CountIf(Sheets("Dashboard").Range("J14:J64"), "Item Not Found")
If aCount = 0 Then msg = msg1 & msg3 Else msg = msg1 & aCount & msg2
MsgBox msg
 
Last edited:
Upvote 0
another option for you to test

Code:
[I][COLOR=#006400]'declare variables[/COLOR][/I]
Dim aCount As Integer, msg As String
Const msg1 = "BOM Import Successful" & vbCr
Const msg2 = " items not found"
Const msg3 = "All items were successfully imported!"


[COLOR=#006400][I]'use count to determine message string[/I][/COLOR]
aCount = WorksheetFunction.CountIf(Sheets("Dashboard").Range("J14:J64"), "Item Not Found")
If aCount = 0 Then msg = msg1 & msg3 Else msg = msg1 & aCount & msg2
MsgBox msg
@Yongle & @nemmi69 thank you for both of your suggestions.

I can't seem to get either to work, would you mind copying my existing vba code with your suggestions implemented please? I think I'm putting them in the wrong locations within my code.

Thank you so much
 
Upvote 0
Is this code sitting in a module?

Seems to work fine for me.
 
Upvote 0
Is this code sitting in a module?

Seems to work fine for me.

Hi, yes it is. This is my code below assigned to a button:

Code:
Sub BOMTest()


' Get customer workbook...
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook


' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook


' get the customer workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please select the BOM "
customerFilename = Application.GetOpenFilename(filter, , caption)


Set customerWorkbook = Application.Workbooks.Open(customerFilename)


' assume range is A1 - C10 in sheet1
' copy data from customer to target workbook
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)


targetSheet.Range("F14", "I48").Value = sourceSheet.Range("A2", "K48").Value


' Close customer workbook
customerWorkbook.Close


targetSheet.Range("F7").Value = customerFilename


MsgBox "BOM Import Successful!", vbInformation
End Sub

Sorry, but where do I place your code within my code above for it to all work together?
 
Upvote 0
Hi,

I have managed to get the below code to work - thank you!

Code:
Sub testoday()




' Get customer workbook...
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook


Dim aCount As Integer, msg As String
Const msg1 = "BOM Import Successful" & vbCr
Const msg2 = " items not found"
Const msg3 = "All items were successfully imported!"






' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook




' get the customer workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please select the BOM "
customerFilename = Application.GetOpenFilename(filter, , caption)




Set customerWorkbook = Application.Workbooks.Open(customerFilename)




' assume range is A1 - C10 in sheet1
' copy data from customer to target workbook
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)




targetSheet.Range("F14", "I48").Value = sourceSheet.Range("A2", "K48").Value




' Close customer workbook
customerWorkbook.Close




targetSheet.Range("F7").Value = customerFilename




aCount = WorksheetFunction.CountIf(Sheets("Dashboard").Range("J14:J64"), "Item Not Found")
If aCount = 0 Then msg = msg1 & msg3 Else msg = msg1 & aCount & msg2
MsgBox msg
End Sub

Please can I ask this, instead of counting the number of instances of 'Item Not Found' in this cell range, please could you help me instead with the code of counting the number of instances which contain a numerical value greater than £0? (ignoring text values).

Is that possible?
 
Upvote 0
Code:
Sub BOMTest()




' Get customer workbook...
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook
Dim aCount As Integer, msg As String
Const msg1 = "BOM Import Successful" & vbCr
Const msg2 = " items not found"
Const msg3 = "All items were successfully imported!"




' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook




' get the customer workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please select the BOM "
customerFilename = Application.GetOpenFilename(filter, , caption)




Set customerWorkbook = Application.Workbooks.Open(customerFilename)




' assume range is A1 - C10 in sheet1
' copy data from customer to target workbook
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)




targetSheet.Range("F14", "I48").Value = sourceSheet.Range("A2", "K48").Value




' Close customer workbook
customerWorkbook.Close




targetSheet.Range("F7").Value = customerFilename
aCount = WorksheetFunction.CountIf(Sheets("Dashboard").Range("J14:J64"), "Item Not Found")


If aCount = 0 Then msg = msg1 & msg3 Else msg = msg1 & aCount & msg2
MsgBox msg
End Sub
 
Upvote 0
Thank you @nemmi69

Please can I be a pain...

Please can I ask this, instead of counting the number of instances of 'Item Not Found' in this cell range, please could you help me instead with the code of counting the number of instances which contain a numerical value greater than £0? (ignoring text values).

Is this possible?
 
Last edited:
Upvote 0
Please can I ask this, instead of counting the number of instances of 'Item Not Found' in this cell range, please could you help me instead with the code of counting the number of instances which contain a numerical value greater than £0? (ignoring text values).

amend line that provides aCount to this
Code:
aCount = WorksheetFunction.CountIf(Sheets("Dashboard").Range("J14:J64"), ">0")
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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