Excel VBA: Refer Cell Value and Offset just specific Columns from another Sheet

ShumsFaruk

Board Regular
Joined
Jul 24, 2009
Messages
93
Good Day All Excel MVPs,

I know its sounds repeated subject, but I have been trying to get bits and pieces from different threads and post, but failing to get results as per my need. I am in a situation, where I am tired of searching further.

I need to print Invoice before that I would require Cells to be filled with a cell reference.

Cell reference is Worksheets("Invoice_Template").Range("K8").Value

Once VBA finds the cell reference value in Worksheets("WBEntryDetails") Column B then I would like to offset to Worksheets("Invoice_Template")

Offset range should be:
Worksheets("Invoice_Template").Range("D24").value = Worksheets("WBEntryDetails") Column G ( cell reference value would possibly be repeated few times, so we need to loop), but I would need to leave 3 rows for next i which means next offset should start .Range("D28")
Worksheets("Invoice_Template").Range("F24").value = Worksheets("WBEntryDetails") Column H ( cell reference value would possibly be repeated few times, so we need to loop), but I would need to leave 3 rows for next i which means next offset should start .Range("F28")
Worksheets("Invoice_Template").Range("D25").value = Worksheets("WBEntryDetails") Column I ( cell reference value would possibly be repeated few times, so we need to loop), but I would need to leave 3 rows for next i which means next offset should start .Range("D29")
Worksheets("Invoice_Template").Range("F25").value = Worksheets("WBEntryDetails") Column J ( cell reference value would possibly be repeated few times, so we need to loop), but I would need to leave 3 rows for next i which means next offset should start .Range("F29")
Worksheets("Invoice_Template").Range("H26").value = Worksheets("WBEntryDetails") Column M ( cell reference value would possibly be repeated few times, so we need to loop), but I would need to leave 3 rows for next i which means next offset should start .Range("H30")
Worksheets("Invoice_Template").Range("I24").value = Worksheets("WBEntryDetails") Column T ( cell reference value would possibly be repeated few times, so we need to loop), but I would need to leave 3 rows for next i which means next offset should start .Range("I28")

I tried to get one offset for Range("D24") as per below code, but I am failing to get desired result.

Code:
Option Explicit
Sub Update_InvoiceTemplate()
Dim ws As Worksheet, ws1 As Worksheet
Dim row As Integer
Dim LR As Long
Set ws = Worksheets("WBEntryDetails")
Set ws1 = Worksheets("Invoice_Template")
LR = WorksheetFunction.Max(24, ws1.Range("D" & Rows.Count).End(xlUp).row + 1)
row = 3

Do While (ws.Range("B" & row).Value <> "")

    If ws.Range("B" & row).Value = ws1.Range("K8").Value Then
        ws1.Cells(LR, row).Value = ws.Range("G" & row).Value
        
    End If

row = row + 1

Loop

End Sub

Please help.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Upvote 0
Thanks Rich,

No problem. I wanted to present this morning to my management. But I will wait until tomorrow.
 
Upvote 0
LOL.

I don't wanna push you. You can do whenever you have time. I am also still searching.

I can just say, thank you so much for your consideration.

Regards,
Shums
 
Upvote 0
Hi Rich,

I found below code, it does the somehow work which I wanted.
Code:
Sub Update_InvoiceTemplate()
Dim ws As Worksheet, ws1 As Worksheet
Dim LSearchRow As Integer, LCopyToRow As Integer

Set ws = Worksheets("WBEntryDetails")
Set ws1 = Worksheets("Invoice_Template")
Application.ScreenUpdating = False
'Start search in row 2
LSearchRow = 2

'Start copying data to row 24 in InvoiceTemplate (row counter variable)
LCopyToRow = 24

While Len(ws.Range("B" & CStr(LSearchRow)).Value <> "")

    'If value in Cell K8 = Searching Column in WBEntryDetails, copy entire row to InvoiceTemplate
    If ws.Range("B" & CStr(LSearchRow)).Value = ws1.Range("K8").Value Then
        
        'Select row in WBEntryDetails to copy
        ws.Rows(CStr(LSearchRow)).Copy
        
        'Paste row into InvoiceTemplate in next 4th row
        ws1.Rows(CStr(LCopyToRow)).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
         False, Transpose:=False
        
        'Move counter to next row
        LCopyToRow = LCopyToRow + 4
        
    End If
    
    'Go back to WBEntryDetails to continue searching
    ws.Select
    LSearchRow = LSearchRow + 1
      
   Wend
   
   'Position on cell A1
   Application.CutCopyMode = False
   ws.Range("A1").Select
   
   MsgBox "All matching data has been copied."
   
   Exit Sub
   
Err_Execute:
   MsgBox "An Error Occurred."
Application.ScreenUpdating = True
End Sub


Problem:
1. It copies the whole row, I need to Copy particular column data.
2. It pastes to first column in InvoiceTemplate, where I would like to have data in Column D.
3. It hangs after pasting at line LSearchRow = LSearchRow + 1

Please try to fix this code.

Regards,
Shums
 
Upvote 0
Solved: Re: Excel VBA: Refer Cell Value and Offset just specific Columns from another Sheet

Hi All,

I have successfully achieved the goal with the help of some research.

I have below code for opening this workbook, which clears the cell which I wanted to extract from other sheet:
Code:
Private Sub Workbook_Open()
Sheets("Invoice_Template").Range("D24:D44") = ""
Sheets("Invoice_Template").Range("F24:F25") = ""
Sheets("Invoice_Template").Range("F28:F29") = ""
Sheets("Invoice_Template").Range("F32:F33") = ""
Sheets("Invoice_Template").Range("F36:F37") = ""
Sheets("Invoice_Template").Range("F40:F41") = ""
Sheets("Invoice_Template").Range("H26") = ""
Sheets("Invoice_Template").Range("H30") = ""
Sheets("Invoice_Template").Range("H34") = ""
Sheets("Invoice_Template").Range("H38") = ""
Sheets("Invoice_Template").Range("H42") = ""
Sheets("Invoice_Template").Range("I26") = ""
Sheets("Invoice_Template").Range("I30") = ""
Sheets("Invoice_Template").Range("I34") = ""
Sheets("Invoice_Template").Range("I38") = ""
Sheets("Invoice_Template").Range("I42") = ""
Sheets("Invoice_Template").Range("K26") = ""
Sheets("Invoice_Template").Range("K30") = ""
Sheets("Invoice_Template").Range("K34") = ""
Sheets("Invoice_Template").Range("K38") = ""
Sheets("Invoice_Template").Range("K42") = ""

Sheets("Cabgoc").Range("D24:D44") = ""
Sheets("Cabgoc").Range("F24:F25") = ""
Sheets("Cabgoc").Range("F28:F29") = ""
Sheets("Cabgoc").Range("F32:F33") = ""
Sheets("Cabgoc").Range("F36:F37") = ""
Sheets("Cabgoc").Range("F40:F41") = ""
Sheets("Cabgoc").Range("H26") = ""
Sheets("Cabgoc").Range("H30") = ""
Sheets("Cabgoc").Range("H34") = ""
Sheets("Cabgoc").Range("H38") = ""
Sheets("Cabgoc").Range("H42") = ""
Sheets("Cabgoc").Range("I26") = ""
Sheets("Cabgoc").Range("I30") = ""
Sheets("Cabgoc").Range("I34") = ""
Sheets("Cabgoc").Range("I38") = ""
Sheets("Cabgoc").Range("I42") = ""
Sheets("Cabgoc").Range("K26") = ""
Sheets("Cabgoc").Range("K30") = ""
Sheets("Cabgoc").Range("K34") = ""
Sheets("Cabgoc").Range("K38") = ""
Sheets("Cabgoc").Range("K42") = ""
Worksheets("Home").Activate
Range("B12").Select
ActiveWorkbook.Save
End Sub

Following below code to get desired result, which I initially asked:
Code:
Sub Update_InvoiceTemplate()
Dim ws As Worksheet, ws1 As Worksheet
Dim LSearchRow As Integer, LCopyToRow1 As Integer, LCopyToRow1a As Integer, LCopyToRow2 As Integer, LCopyToRow2a As Integer, LCopyToRow3 As Integer, LCopyToRow3a As Integer, LCopyToRow3b As Integer

Set ws = Worksheets("WBEntryDetails")
Set ws1 = Worksheets("Invoice_Template")
Application.ScreenUpdating = False
ws1.Range("D24:D44") = ""
ws1.Range("F24:F25") = ""
ws1.Range("F28:F29") = ""
ws1.Range("F32:F33") = ""
ws1.Range("F36:F37") = ""
ws1.Range("F40:F41") = ""
ws1.Range("H26") = ""
ws1.Range("H30") = ""
ws1.Range("H34") = ""
ws1.Range("H38") = ""
ws1.Range("H42") = ""
ws1.Range("I26") = ""
ws1.Range("I30") = ""
ws1.Range("I34") = ""
ws1.Range("I38") = ""
ws1.Range("I42") = ""
ws1.Range("K26") = ""
ws1.Range("K30") = ""
ws1.Range("K34") = ""
ws1.Range("K38") = ""
ws1.Range("K42") = ""

'Start copying data to row 24 in InvoiceTemplate (row counter variable)
LCopyToRow1 = 24
LCopyToRow1a = 24
LCopyToRow2 = 25
LCopyToRow2a = 25
LCopyToRow3 = 26
LCopyToRow3a = 26
LCopyToRow3b = 26

'Start search in row 2 to 500
For LSearchRow = 2 To 500

    'If value in Cell K8 = Searching Column in WBEntryDetails, copy entire row to InvoiceTemplate
    If ws.Range("B" & CStr(LSearchRow)).Value = ws1.Range("K8").Value Then
        
        'Select row in WBEntryDetails to copy
        ws.Range("G" & CStr(LSearchRow)).Copy
        
        'Paste row into InvoiceTemplate in next 4th row
        ws1.Range("D" & CStr(LCopyToRow1)).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
         False, Transpose:=False
        
        'Move counter to next row
        LCopyToRow1 = LCopyToRow1 + 4
        Application.CutCopyMode = False
        
        'Select row in WBEntryDetails to copy
        ws.Range("H" & CStr(LSearchRow)).Copy
        
        'Paste row into InvoiceTemplate in next 4th row
        ws1.Range("F" & CStr(LCopyToRow1a)).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
         False, Transpose:=False
        
        'Move counter to next row
        LCopyToRow1a = LCopyToRow1a + 4
        Application.CutCopyMode = False
        
        'Select row in WBEntryDetails to copy
        ws.Range("I" & CStr(LSearchRow)).Copy
        
        'Paste row into InvoiceTemplate in next 4th row
        ws1.Range("D" & CStr(LCopyToRow2)).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
         False, Transpose:=False
        
        'Move counter to next row
        LCopyToRow2 = LCopyToRow2 + 4
        Application.CutCopyMode = False
        
        'Select row in WBEntryDetails to copy
        ws.Range("J" & CStr(LSearchRow)).Copy
        
        'Paste row into InvoiceTemplate in next 4th row
        ws1.Range("F" & CStr(LCopyToRow2a)).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
         False, Transpose:=False
        
        'Move counter to next row
        LCopyToRow2a = LCopyToRow2a + 4
        Application.CutCopyMode = False
        
        'Select row in WBEntryDetails to copy
        ws.Range("M" & CStr(LSearchRow)).Copy
        
        'Paste row into InvoiceTemplate in next 4th row
        ws1.Range("H" & CStr(LCopyToRow3)).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
         False, Transpose:=False
        
        'Move counter to next row
        LCopyToRow3 = LCopyToRow3 + 4
        Application.CutCopyMode = False
        
        'Select row in WBEntryDetails to copy
        ws.Range("D" & CStr(LSearchRow)).Copy
        
        'Paste row into InvoiceTemplate in next 4th row
        ws1.Range("I" & CStr(LCopyToRow3a)).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
         False, Transpose:=False
        
        'Move counter to next row
        LCopyToRow3a = LCopyToRow3a + 4
        Application.CutCopyMode = False
        
        'Select row in WBEntryDetails to copy
        ws.Range("N" & CStr(LSearchRow)).Copy
        
        'Paste row into InvoiceTemplate in next 4th row
        ws1.Range("K" & CStr(LCopyToRow3b)).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
         False, Transpose:=False
        
        'Move counter to next row
        LCopyToRow3b = LCopyToRow3b + 4
        Application.CutCopyMode = False
        
    End If
        ws.Visible = xlSheetVisible
        ws.Select
Next LSearchRow
        ws.Visible = xlSheetHidden
Application.ScreenUpdating = True
End Sub

Special thanks to Rich for considering and helping me understanding the process.

Regards,
Shums
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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