Due to Vlookup formula unable to copy cell value

Rakesh Kamani

New Member
Joined
Feb 25, 2020
Messages
33
Office Version
  1. 2013
Platform
  1. Windows
Hi,
when I run the command button the data is copied from one sheet to another sheet, but the cell value column "L" and column "V" do not copy into another sheet (i.e Column "F" and column "G") are available due to the Vlookup formula's on Column "L" and column "V". Could you please any one correction below code
wS.Range("L7").Offset(intI + 1, 0).Resize(intC, 1).Copy wD.Range("F7")
wS.Range("V7").Offset(intI + 1, 0).Resize(intC, 1).Copy wD.Range("G7")

Full Code:
VBA Code:
Private Sub CommandButton1_Click()

On Error Resume Next

Application.ScreenUpdating = False

Dim wB As Workbook

Dim wS As Worksheet
Dim wT As Worksheet
Dim wD As Worksheet
Dim wDB As Worksheet


Dim strF As String
Dim strT As String

Dim intI As Integer
Dim intJ As Integer
Dim intC As Integer
Dim intS As Integer
Dim intR As Integer
Dim intX As Integer
Dim intY As Integer

Dim rngDB As Range
Dim rngCell As Range

Set wS = ThisWorkbook.Worksheets("SourceSheet")
Set wT = ThisWorkbook.Worksheets("templateSheet")
Set wDB = ThisWorkbook.Worksheets("DatabaseSheet")
'Set wB = Application.Workbooks.Add

Set rngDB = wDB.Range("B85:B181")
'Do

   'If wB.Worksheets.Count = 1 Then Exit Do

   'wB.Worksheets(2).Delete
'Loop

strF = "FG"
intS = 1
intI = 0
intJ = 0

Do
   If wS.Range("A7").Offset(intI, 0).Value = "" Or wS.Range("A7").Offset(intI, 0).Value = "EoF" Then Exit Do
   strT = wS.Range("A7").Offset(intI, 0).Value
   If VBA.InStr(1, strT, strF) Then
  
      'If intS = 1 Then
         'Set wD = wB.Worksheets(1)
         'intS = intS + 1
      'Else
         'Set wD = wB.Worksheets.Add
      'End If
      Set wD = ThisWorkbook.Worksheets.Add
'      Set wD = ThisWorkbook.Worksheets.Add
'      intC = 0
      intJ = 1
      Do
     
         If VBA.InStr(1, wS.Range("A7").Offset(intI + intJ, 0).Value, strF) Or VBA.InStr(1, wS.Range("A7").Offset(intI + intJ, 0).Value, "EoF") Then Exit Do
        
'          intC = 0
         intJ = intJ + 1
        
     
      Loop
      intC = intJ - 1
      wD.Name = strT
      wT.Range("A1:V6").Copy wD.Range("A1")
      wS.Range("A7").Offset(intI + 1, 0).Resize(intC, 1).Copy wD.Range("B7")
      wS.Range("I7").Offset(intI + 1, 0).Resize(intC, 1).Copy wD.Range("A7")
      wS.Range("B7").Offset(intI + 1, 0).Resize(intC, 1).Copy wD.Range("C7")
      wS.Range("C7").Offset(intI + 1, 0).Resize(intC, 1).Copy wD.Range("D7")
      wS.Range("L7").Offset(intI + 1, 0).Resize(intC, 1).Copy wD.Range("F7")
      wS.Range("V7").Offset(intI + 1, 0).Resize(intC, 1).Copy wD.Range("G7")

     
      intX = 0
      Do
         If wD.Range("A7").Offset(intX, 0).Value = "" Then Exit Do
        
         For Each rngCell In rngDB
           
            If VBA.InStr(1, rngCell.Value, wD.Range("A7").Offset(intX, 0).Value) Then
              
               intR = rngCell.Row
               intY = rngCell.MergeArea.Cells.Count
           
               Exit For
            End If
        
         Next rngCell
        
        
         wD.Range("A7").Offset(intX + 1, 0).Resize(intY - 1, 26).Insert Shift:=xlDown
         wD.Range("H7").Offset(intX, 0).Resize(intY, 1).Value = wDB.Cells(intR, 4).Resize(intY, 1).Value
         wD.Range("I7").Offset(intX, 0).Resize(intY, 1).Value = wDB.Cells(intR, 5).Resize(intY, 1).Value
         intX = intX + intY
        
      Loop
     
     
   End If
  
   intI = intI + intJ
Loop
Application.ScreenUpdating = True
MsgBox "created Functional Group from  SourceSheet"

End Sub
 
Last edited by a moderator:

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try
VBA Code:
      wD.Range("F7").Resize(intC).Value = Ws.Range("L7").Offset(intI + 1, 0).Resize(intC, 1).Value
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0
Thank you , its working fine.

Try
VBA Code:
      wD.Range("F7").Resize(intC).Value = Ws.Range("L7").Offset(intI + 1, 0).Resize(intC, 1).Value
Need help from your end for Small correction . Instead of you given copy the value from column-L to column-F: Please update this VLOOKUP formula in this line: =VLOOKUP($A7,'SourceSheet'!$I$5:$V$83,4,0)
 
Upvote 0
That code has nothing to do with vlookup, so don't understand what you are asking.
 
Upvote 0
That code has nothing to do with vlookup, so don't understand what you are asking.
when I run the command button the data is copied from one sheet to another sheet, but the cell value column "L" and column "V" copied into another sheet (i.e Column "F" and column "G") are available now column -F and column-G its sowing value, instead of value please update same formula in column F7 & G7. formula is : =VLOOKUP($A7,'SourceSheet'!$I$5:$V$83,4,0) and =VLOOKUP($A7,'SourceSheet'!$I$5:$V$83,14,0). for example how updated like column O,P,Q and column S,T,V Could you please correction below code for Column F and Column G.

VBA Code:
     wS.Range("L7").Offset(intI + 1, 0).Resize(intC, 1).Copy
      wD.Range("F7").PasteSpecial xlPasteValues
 
      wS.Range("V7").Offset(intI + 1, 0).Resize(intC, 1).Copy
      wD.Range("G7").PasteSpecial xlPasteValues


Private Sub CommandButton1_Click()

On Error Resume Next

Application.ScreenUpdating = False

Dim wB As Workbook

Dim wS As Worksheet
Dim wT As Worksheet
Dim wD As Worksheet
Dim wDB As Worksheet


Dim strF As String
Dim strT As String

Dim intI As Integer
Dim intJ As Integer
Dim intC As Integer
Dim intS As Integer
Dim intR As Integer
Dim intX As Integer
Dim intY As Integer

Dim rngDB As Range
Dim rngCell As Range

Dim strFormulea As String


Set wS = ThisWorkbook.Worksheets("SourceSheet")
Set wT = ThisWorkbook.Worksheets("templateSheet")
Set wDB = ThisWorkbook.Worksheets("DatabaseSheet")
'Set wB = Application.Workbooks.Add

Set rngDB = wDB.Range("B85:B188")
'Do

   'If wB.Worksheets.Count = 1 Then Exit Do

   'wB.Worksheets(2).Delete
'Loop

strF = "FG"
intS = 1
intI = 0
intJ = 0

Do
   If wS.Range("A7").Offset(intI, 0).Value = "" Or wS.Range("A7").Offset(intI, 0).Value = "EoF" Then Exit Do
   strT = wS.Range("A7").Offset(intI, 0).Value
   If VBA.InStr(1, strT, strF) Then
  
      'If intS = 1 Then
         'Set wD = wB.Worksheets(1)
         'intS = intS + 1
      'Else
         'Set wD = wB.Worksheets.Add
      'End If
      Set wD = ThisWorkbook.Worksheets.Add
'      Set wD = ThisWorkbook.Worksheets.Add
  '    intC = 0
      intJ = 1
      Do
     
         If VBA.InStr(1, wS.Range("A7").Offset(intI + intJ, 0).Value, strF) Or VBA.InStr(1, wS.Range("A7").Offset(intI + intJ, 0).Value, "EoF") Then Exit Do
        
      '    intC = 0
         intJ = intJ + 1
        
     
      Loop
      intC = intJ - 1
      wD.Name = strT
      wT.Range("A1:V6").Copy wD.Range("A1")
      wS.Range("A7").Offset(intI + 1, 0).Resize(intC, 1).Copy wD.Range("B7")
      wS.Range("I7").Offset(intI + 1, 0).Resize(intC, 1).Copy wD.Range("A7")
      wS.Range("B7").Offset(intI + 1, 0).Resize(intC, 1).Copy wD.Range("C7")
      wS.Range("C7").Offset(intI + 1, 0).Resize(intC, 1).Copy wD.Range("D7")
      'wS.Range("L7").Offset(intI + 1, 0).Resize(intC, 1).Copy wD.Range("F7")
     
      wS.Range("L7").Offset(intI + 1, 0).Resize(intC, 1).Copy
      wD.Range("F7").PasteSpecial xlPasteValues

      'wD.Range("F7").Resize(intC).Value = wS.Range("L7").Offset(intI + 1, 0).Resize(intC, 1).Value
     
      'wS.Range("V7").Offset(intI + 1, 0).Resize(intC, 1).Copy wD.Range("G7")
     
      wS.Range("V7").Offset(intI + 1, 0).Resize(intC, 1).Copy
      wD.Range("G7").PasteSpecial xlPasteValues

      'wD.Range("G7").Resize(intC).Value = wS.Range("V7").Offset(intI + 1, 0).Resize(intC, 1).Value

     
      intX = 0
      Do
         If wD.Range("A7").Offset(intX, 0).Value = "" Then Exit Do
        
         For Each rngCell In rngDB
           
            If VBA.InStr(1, rngCell.Value, wD.Range("A7").Offset(intX, 0).Value) Then
              
               intR = rngCell.Row
               intY = rngCell.MergeArea.Cells.Count
            
               Exit For
            End If
        
         Next rngCell
        
        
         wD.Range("A7").Offset(intX + 1, 0).Resize(intY - 1, 26).Insert Shift:=xlDown
        
         wD.Range("H7").Offset(intX, 0).Resize(intY, 1).Value = wDB.Cells(intR, 4).Resize(intY, 1).Value
        
         wD.Range("I7").Offset(intX, 0).Resize(intY, 1).Value = wDB.Cells(intR, 5).Resize(intY, 1).Value
        
        
          'strFormulea = "=$G$7*$I7*L7"
         'wD.Range("O7").Offset(intX, 0).Resize(intY, 3).FormulaR1C1 = strFormulea
        
         strFormulea = "=R" & 7 + intX & "C7*RC9*RC[-3]"
         wD.Range("O7").Offset(intX, 0).Resize(intY, 3).FormulaR1C1 = strFormulea
        
                '=IF(R7<>"",VLOOKUP(R7,'07_Safety_Measures'!$B$18:$C$40,2,FALSE),0)
                
                    strFormulea = "=IF(RC[-1]<>" & """""" & ",VLOOKUP(RC[-1],'07_Safety_Measures'!R18C2:R40C3,2,FALSE),0)"
         wD.Range("S7").Offset(intX, 0).Resize(intY, 1).FormulaR1C1 = strFormulea


       
            'strFormulea = "=RC[-5]*(100%-intX & RC[-1])"
       
            'wD.Range("T7").Offset(intX, 0).Resize(intY, 2).FormulaR1C1 = strFormulea
     
        
            strFormulea = "=RC[-5]*(100%-RC[-1])"
        
            wD.Range("T7").Offset(intX, 0).Resize(intY, 1).FormulaR1C1 = strFormulea
           
            strFormulea = "=RC[-5]*(100%-RC[-2])"
           
            wD.Range("U7").Offset(intX, 0).Resize(intY, 1).FormulaR1C1 = strFormulea

        
         intX = intX + intY
        
      Loop
     
      intX = 0
     
      Do
     
         If wD.Range("H7").Offset(intX, 0).Value = "" Then Exit Do
        
         intY = 1
        
         Do
           
            If wD.Cells(7 + intX + intY, 1).Value <> "" Or wD.Range("H7").Offset(intX + intY, 0).Value = "" Then Exit Do
            intY = intY + 1
           
        
         Loop
        
         wD.Cells(7 + intX, 1).Resize(intY, 1).Merge
         wD.Cells(7 + intX, 1).Resize(intY, 1).HorizontalAlignment = xlCenter
         wD.Cells(7 + intX, 1).Resize(intY, 1).VerticalAlignment = xlCenter

         wD.Cells(7 + intX, 2).Resize(intY, 1).Merge
         wD.Cells(7 + intX, 2).Resize(intY, 1).HorizontalAlignment = xlCenter
         wD.Cells(7 + intX, 2).Resize(intY, 1).VerticalAlignment = xlCenter
        
         wD.Cells(7 + intX, 3).Resize(intY, 1).Merge
         wD.Cells(7 + intX, 3).Resize(intY, 1).HorizontalAlignment = xlCenter
         wD.Cells(7 + intX, 3).Resize(intY, 1).VerticalAlignment = xlCenter
        
         wD.Cells(7 + intX, 4).Resize(intY, 1).Merge
         wD.Cells(7 + intX, 4).Resize(intY, 1).HorizontalAlignment = xlCenter
         wD.Cells(7 + intX, 4).Resize(intY, 1).VerticalAlignment = xlCenter
        
         wD.Cells(7 + intX, 5).Resize(intY, 1).Merge
         wD.Cells(7 + intX, 5).Resize(intY, 1).HorizontalAlignment = xlCenter
         wD.Cells(7 + intX, 5).Resize(intY, 1).VerticalAlignment = xlCenter
        
         wD.Cells(7 + intX, 6).Resize(intY, 1).Merge
         wD.Cells(7 + intX, 6).Resize(intY, 1).HorizontalAlignment = xlCenter
         wD.Cells(7 + intX, 6).Resize(intY, 1).VerticalAlignment = xlCenter
        
         wD.Cells(7 + intX, 7).Resize(intY, 1).Merge
         wD.Cells(7 + intX, 7).Resize(intY, 1).HorizontalAlignment = xlCenter
         wD.Cells(7 + intX, 7).Resize(intY, 1).VerticalAlignment = xlCenter
         intX = intX + intY '+ 1
     
      Loop


     
   End If
  
   intI = intI + intJ
Loop
Application.ScreenUpdating = True
MsgBox "created Functional Group from BOM sheet"

End Sub
 
Last edited by a moderator:
Upvote 0
Then why not use the code I suggested?
 
Upvote 0

Forum statistics

Threads
1,215,338
Messages
6,124,358
Members
449,155
Latest member
ravioli44

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