Check if there is a value in the cell before pasting value

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Morning,

I am using the code shown below but getting confused trying to get it to work with some new added code.

I am in need of checking if a value is present in cell P5
If the cell is empty continue to paste value but if the cell has a present value then show MsgBox

ORIGINAL CODE WITHOUT ANY CHEKS
Rich (BB code):
    Sub HYPERLINKP5()
    Dim srcWS As Worksheet, destWS As Worksheet
    Set srcWS = ActiveWorkbook.Worksheets("INV")
    Set destWS = ActiveWorkbook.Worksheets("DATABASE")
    srcWS.Range("L4").Copy destWS.Range("P5")
    
    With Sheets("DATABASE")
    .Range("P5").Font.Size = 14
    End With
    
    With Sheets("DATABASE")
    Worksheets("DATABASE").Activate
    Worksheets("DATABASE").Range("P5").Select
    
    Const FILE_PATH As String = "C:\Users\Ian\Desktop\REMOTES ETC\DR\DR COPY INVOICES\"
    If ActiveCell.Column = Columns("P").Column Then
        If Len(Dir(FILE_PATH & ActiveCell.Value & ".pdf")) Then
            ActiveCell.Hyperlinks.Add Anchor:=ActiveCell, Address:=FILE_PATH & ActiveCell.Value & ".pdf"
            
        Else
            ActiveCell.Hyperlinks.Delete
            MsgBox (FILE_PATH & ActiveCell.Value & ".pdf" & vbNewLine & vbNewLine & "FILE IS NOT IN FOLDER SPECIFIED, PLEASE CHECK PATH IS CORRECT"), vbCritical
        End If
    Else
        MsgBox "PLEASE SELECT AN INVOICE NUMBER.", vbExclamation, "HYPERLINKING THE INVOICE NUMBER"
    End If
    End With
End Sub

ADDING THE CHECK CODE BUT FAILING WITH THE BASIC PART IN RED

I would like to check if cell P5 is empty, If the cell is empty then continue to paste new value in cell P5, as shown using blue code.
If cell P5 has a value in it Then i need to see a MsgBox advising the user, Basically dont paste the new value & to allow the user to go check it out first

Rich (BB code):
    Sub HYPERLINKP5()
    Dim srcWS As Worksheet, destWS As Worksheet
    Set srcWS = ActiveWorkbook.Worksheets("INV")
    Set destWS = ActiveWorkbook.Worksheets("DATABASE")
    srcWS.Range("L4").Copy destWS.Range("P5")
    
    With Sheets("DATABASE")
    .Range("P5").Font.Size = 14
    End With
    
    With Sheets("DATABASE")
    Worksheets("DATABASE").Activate
    Worksheets("DATABASE").Range("P5").Select
    
    
    
    If Range("P5").Value = "" Then
    Continue with pasting new value
    
    Otherwise
    MsgBox "Value in cell test message"
        
    
    
    Const FILE_PATH As String = "C:\Users\Ian\Desktop\REMOTES ETC\DR\DR COPY INVOICES\"
    If ActiveCell.Column = Columns("P").Column Then
        If Len(Dir(FILE_PATH & ActiveCell.Value & ".pdf")) Then
            ActiveCell.Hyperlinks.Add Anchor:=ActiveCell, Address:=FILE_PATH & ActiveCell.Value & ".pdf"
            
        Else
            ActiveCell.Hyperlinks.Delete
            MsgBox (FILE_PATH & ActiveCell.Value & ".pdf" & vbNewLine & vbNewLine & "FILE IS NOT IN FOLDER SPECIFIED, PLEASE CHECK PATH IS CORRECT"), vbCritical
        End If
    Else
        MsgBox "PLEASE SELECT AN INVOICE NUMBER.", vbExclamation, "HYPERLINKING THE INVOICE NUMBER"
    End If
    End With
End Sub
End Sub
 
Ok my last suggestion will be like below. It is up to you to try. Have a nice day 🙋🏻
VBA Code:
Sub HYPERLINKP5()
    Dim srcWS As Worksheet, destWS As Worksheet
    Set srcWS = ActiveWorkbook.Worksheets("INV")
    Set destWS = ActiveWorkbook.Worksheets("DATABASE")
    If Trim(destWS.Range("P5").Value) = "" Then 'If P5 is empty do everything
      srcWS.Range("L4").Copy destWS.Range("P5")

      With destWS
      .Range("P5").Font.Size = 14
      .Activate
      .Range("P5").Select
 
      Const FILE_PATH As String = "C:\Users\Ian\Desktop\REMOTES ETC\DR\DR COPY INVOICES\"
      If ActiveCell.Column = Columns("P").Column Then
          If Dir(FILE_PATH & ActiveCell.Value & ".pdf") <> "" Then
              ActiveCell.Hyperlinks.Add Anchor:=ActiveCell, Address:=FILE_PATH & ActiveCell.Value & ".pdf"  
          Else
            ActiveCell.Hyperlinks.Delete
            MsgBox (FILE_PATH & ActiveCell.Value & ".pdf" & vbNewLine & vbNewLine & "FILE IS NOT IN FOLDER SPECIFIED, PLEASE CHECK PATH IS CORRECT"), vbCritical
        End If
      Else
        MsgBox "PLEASE SELECT AN INVOICE NUMBER.", vbExclamation, "HYPERLINKING THE INVOICE NUMBER"
      End If
      End With
    Else 'If not empty
      MsgBox "P5 is not empty."
    End If
End Sub
 
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,215,084
Messages
6,123,029
Members
449,092
Latest member
ikke

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