+ Reply to Thread
Results 1 to 3 of 3

vba if query issues

Hybrid View

BaronVonBeefDip vba if query issues 11-16-2018, 11:12 AM
AB33 Re: vba if query issues 11-16-2018, 12:11 PM
mjr veverka Re: vba if query issues 11-16-2018, 07:39 PM
  1. #1
    Registered User
    Join Date
    09-22-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    14

    vba if query issues

    Hello,

    the code below is to search column 3 for any value put into column 1. then it is meant to look in column 4 and if that is empty paste in the information but if it has data in it to insert a line then paste the data. the issue I am having is that it inserts the line regardless of whether there is information or not. can someone tell me how to fix this and explain what I did wrong so that I don't make the same mistake in the future.

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Target = Empty Then Exit Sub
    Dim move As Range
    Dim Info As Range
    Dim full As Range
        If Not Application.Intersect(Range("A:A"), Target) Is Nothing Then
          Application.EnableEvents = False
                    Set Info = Target
                    Set move = Columns(3).Find(Target.Value, , xlValues, xlWhole, searchdirection:=xlPrevious)
                        move.Select
                        ActiveCell.Offset(0, 1).Range("A1").Select
                        Set full = ActiveCell
                        If full Is Nothing Then
                            If Not move Is Nothing Then
                                move.EntireRow.Select
                                ActiveCell.Offset(0, 0).Range("A1").Select
                                Set move = ActiveCell
                                Info.Select
                                Selection.ClearContents
                                ActiveCell.Offset(0, 3).Range("A1:U1").Select
                                Selection.Copy
                                move.Select
                                ActiveCell.Offset(0, 3).Range("A1").Select
                                Selection.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats
                            
                            Else
                                MsgBox "Location not found", vbInformation
                            End If
                        Else
                            If Not move Is Nothing Then
                                move.EntireRow.Select
                                Selection.insert Shift:=xlDown
                                ActiveCell.Offset(0, 0).Range("A1").Select
                                Set move = ActiveCell
                                Info.Select
                                Selection.ClearContents
                                ActiveCell.Offset(0, 3).Range("A1:U1").Select
                                Selection.Copy
                                move.Select
                                ActiveCell.Offset(0, 3).Range("A1").Select
                                Selection.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats
                            
                            Else
                                MsgBox "Location not found", vbInformation
                            End If
                        End If
                        
        End If
        full.Select
        
        
          Application.EnableEvents = True
    End Sub
    apologies if the code is messy or inefficient, I am quite new to coding with VBA,

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: vba if query issues

    Not entirely clear to me if you are copying the search value or the target, but along these lines.

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Target = Empty Then Exit Sub
    Dim move As Range
    Dim Info As Range
    Dim full As Range
        If Not Application.Intersect(Range("A:A"), Target) Is Nothing Then
          Application.EnableEvents = False
          
                    Set move = Columns(3).Find(Target.Value, , xlValues, xlWhole, searchdirection:=xlPrevious)
                    
                     If Not move Is Nothing Then
                            If move.Offset(, 1) = "" Then
                              move.Offset(, 1) = move
                            Else
                                move.EntireRow.Insert Shift:=xlDown
                                move.Offset(-1) = move
                                     
                                     
                            End If
                    Else
                        MsgBox "Location not found", vbInformation
                          
                        
                    End If
    
        
          End If
          Application.EnableEvents = True
    End Sub

  3. #3
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,278

    Re: vba if query issues

    Quote Originally Posted by BaronVonBeefDip View Post
    1. ... the code below is to search column 3 for any value put into column 1
    2. ... then it is meant to look in column 4
    and
    3. if that is empty paste in the information
    but
    4. if it has data in it to insert a line then paste the data ...

    I don't know if I understood correctly (maybe attach your example, i.e. layout and expected result ... ?)
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub
        If Trim(Target.Cells(1).Value) = "" Then Exit Sub
        If Target.Cells.Count > 1 Then Exit Sub
        
        Dim SrchRsltOfCol3 As Range
        'Set SrchRsltOfCol3 = Columns(3).Find(What:=Target.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious) '???
        Set SrchRsltOfCol3 = Columns(3).Find(What:=Target.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows) '???
        
        If Not SrchRsltOfCol3 Is Nothing Then
            Dim ValInCol4 As Variant, rw As Long
            ValInCol4 = Trim(SrchRsltOfCol3.Offset(0, 1).Value)
            rw = SrchRsltOfCol3.Row
            Application.EnableEvents = False
            If ValInCol4 <> "" Then Rows(rw).Insert Shift:=xlDown
            Target.ClearContents
            Range("D" & rw & ":X" & rw).Value = Range("D" & Target.Row & ":X" & Target.Row).Value
            Application.EnableEvents = True
        Else
            MsgBox "Location not found", vbOKOnly, "Info!"
        End If
    End Sub
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] power query issues on formating
    By JEAN1972 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-15-2017, 02:27 PM
  2. Excel 2010 Query issues
    By bassam in forum Excel General
    Replies: 0
    Last Post: 06-23-2014, 03:37 AM
  3. Replies: 0
    Last Post: 04-22-2014, 07:09 PM
  4. Web Query Issues
    By nguyeda in forum Excel General
    Replies: 1
    Last Post: 02-06-2009, 03:50 AM
  5. Replies: 2
    Last Post: 12-10-2007, 07:43 PM
  6. Issues cleaning up External Query VBA
    By mkaake in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-15-2007, 11:26 AM
  7. [SOLVED] Web Query Refresh Issues
    By Hfly in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-15-2006, 06:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1