PDA

View Full Version : vb if help



Emoncada
12-12-2007, 11:27 AM
i have this script
Selection.Copy

ChDir "C:\"
Workbooks.Open Filename:="C:\SN barcodes.xls"

How can i say if there is nothing selected to give a msgbox.

If Selection = "" then
MsgBox "Nothing was selected"
Exit Sub

Else
Selection.Copy

ChDir "C:\"
Workbooks.Open Filename:="C:\SN barcodes.xls"

Something like this.

Zack Barresse
12-12-2007, 12:21 PM
Hello there,

Please describe in as much detail as you can what you are trying to do. There is little to no need to select anything. You should also test for the existence of the workbook to open if you are going to be using it, you should also set it to a workbook variable.

Emoncada
12-12-2007, 12:39 PM
ok sorry for the little detail. Basically they select a range of serial numbers. Then when they click the Cmdbutton it will run this script which then calls another spreadsheet pastes the range in there then opens up another program that will print that range as barcodes. I just want for when the cmdbutton is clicked if no range was selected to notify the user. If then a range is selected to run the spript.

figment
12-12-2007, 12:47 PM
i may be wrong but in my experiance as long as a workbook is open something is selected. is there something else you can test for? do the selections have to be in a certain column? or certain row? or are there a set number of cells that must be selected?

Emoncada
12-12-2007, 12:56 PM
Well i would like the test if selection = "".
If what ever cell is selected or range of cells are selected if they are blank they would fail and give them the msgbox. If there is anything in the cells then it would run the script. If it helps the range would need to be in column F. No where else

figment
12-12-2007, 01:02 PM
now that i can work with try this

Sub testing()
Range("A1") = selecttest
End Sub


Function selecttest()
Dim ra As Range
Dim val As Boolean
val = False
For Each ra In Selection
If ra.Column <> 6 Then 'all of the selection must be in column f
selecttest = False
Exit Function
End If
If ra.Value <> "" Then val = True 'if any of the selection has a
Next 'value then the function is true
If val Then
selecttest = True
Else
selecttest = False
End If
End Function

Emoncada
12-12-2007, 01:26 PM
I see where you are going with this but is it possible to just add something to test selection?

Emoncada
12-12-2007, 01:27 PM
without having to do a true false thing?

Emoncada
12-12-2007, 01:28 PM
If not i will try work that script into my current script.

Emoncada
12-12-2007, 01:28 PM
This is my current script.
Sub cmdbtnFetchFile_Click()

Dim strAddress As String, res As String

Application.CutCopyMode = False

res = Selection
If res = "" Then
MsgBox "Sorry. Please select serial numbers to print!", vbInformation, "Error!"
Exit Sub

Else
Selection.Copy

ChDir "C:\"
Workbooks.Open Filename:="C:\SN barcodes.xls"

Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Range("A2:A65536").Select
Selection.ClearContents

Range("B2:B65536").Select
Selection.Cut

Range("A2").Select
ActiveSheet.Paste

ActiveWorkbook.Save
ActiveWorkbook.Close

On Error GoTo NoFile
strAddress = "C:\Incoming grabber.lab"
ActiveWorkbook.FollowHyperlink Address:=strAddress
End If
Exit Sub

NoFile:
MsgBox "Sorry. File is not available at this time", vbInformation, "Error!"

End Sub

figment
12-12-2007, 01:33 PM
a test is always a true false thing, but if you dont want to use the function and the selection will always only be one range then you can use this

If Selection <> "" And Selection.Column = 6 Then
if the range is more then one cell you will need to use the function, but all you do there is add the function to one of the modules and use this if statment

If Not(selecttest) Then
MsgBox "Sorry. Please select serial numbers to print!", vbInformation, "Error!"
Exit Sub

Else

Zack Barresse
12-12-2007, 04:42 PM
Not sure if I fully understand what you are looking for, and I do not know what a .lab extension is, but maybe...

Option Explicit

Sub cmdbtnFetchFile_Click()
Dim wb As Workbook, ws As Worksheet, strAddress As String, rngSelection As Range
Application.CutCopyMode = False
Call ToggleEvents(False)
Set rngSelection = Selection
If rngSelection.Cells.Count = 1 And rngSelection(1, 1).Value = "" Then
MsgBox "Sorry. Please select serial numbers to print!", vbInformation, "Error!"
GoTo ExitHere
End If
ChDir "C:\" '<-- Do you really need this??
If WbOpen("SN barcodes.xls") = True Then Set wb = Workbooks("SN barcodes.xls")
If wb Is Nothing Then Set wb = Workbooks.Open("C:\SN barcodes.xls")
Set ws = wb.Sheets("Sheet1") '<-- CHANGE AS NECESSARY
ws.Range("B2").Resize(rngSelection.Rows.Count, rngSelection.Columns.Count).Value = rngSelection.Value
ws.Range("A2:A" & ws.Rows.Count).ClearContents
ws.Range("A2:A" & ws.Cells(ws.Rows.Count, "B").End(xlUp).Row).Value = ws.Range("B2:B" & ws.Cells(ws.Rows.Count, "B").End(xlUp).Row).Value
wb.Close SaveChanges:=True
On Error GoTo NoFile
strAddress = "C:\Incoming grabber.lab"
ActiveWorkbook.FollowHyperlink Address:=strAddress
ExitHere:
Call ToggleEvents(True)
Exit Sub
NoFile:
MsgBox "Sorry. File is not available at this time", vbInformation, "Error!"
Resume ExitHere
End Sub

Public Sub ToggleEvents(ByVal blnState As Boolean)
'Originally written by firefytr
With Application
.DisplayAlerts = blnState
.EnableEvents = blnState
.ScreenUpdating = blnState
If blnState Then .CutCopyMode = False
If blnState Then .StatusBar = False
End With
End Sub

Public Function WbOpen(wbName As String) As Boolean
'Originally found by Jake Marx
On Error Resume Next
WbOpen = Len(Workbooks(wbName).Name)
End Function

HTH

Emoncada
12-13-2007, 06:41 AM
That works only thing if i grab 2 or more cells that are blank. It doesn't fail it grabs the blank cells and runs with it.