VBA to find text in a column and copy a range of adjacent cells in the same row to another sheet

JKK22

New Member
Joined
Oct 12, 2022
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Hello,
I am a beginner at Macros.
I want to find the text "T+1" in column C , and in that same row , copy values of cells in columns I-L to another spreadsheet .
So if it finds "T+1" in cell C20, i want to copy the values of cells I20:L20 into another workbook.
I found a way to do it one by one, but I have to believe there is a way to do it using a range.

Here is what i have that works, but I am wondering if there is a better way. I'd appreciate any suggestions.
Thank You

Sub Get Data
Dim nPath As String
Dim fileNameString As String
Dim fileName As String
nPath = "\\Swib\root\ShrInvOper\Fund Accounting\Reporting\InvestOne\"
fileNameString = Format(Date, "dd-mmm-yyyy") & " 1 SIF Holding Detail by Maturity Date" & ".xls"
fileName = Dir(nPath & fileNameString)

Workbooks.Open nPath & fileName
Sheets("Main").Select
Dim a As Long
For a = 2 To ActiveSheet.UsedRange.Rows.Count
If Cells(a, "C").Value = "T+1" Then
Cells(a, "I").Copy
ThisWorkbook.Activate
ActiveSheet.Select
Range("D7").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
End If
Next
Workbooks.Open nPath & fileName
Sheets("Main").Select
Dim b As Long
For b = 2 To ActiveSheet.UsedRange.Rows.Count
If Cells(b, "C").Value = "T+1" Then
Cells(b, "J").Copy
ThisWorkbook.Activate
ActiveSheet.Select
Range("E7").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Application.CutCopyMode = False
End If
Next
Workbooks.Open nPath & fileName
Sheets("Main").Select
Dim c As Long
For c = 2 To ActiveSheet.UsedRange.Rows.Count
If Cells(c, "C").Value = "T+1" Then
Cells(c, "K").Copy
ThisWorkbook.Activate
ActiveSheet.Select
Range("F7").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Application.CutCopyMode = False
End If
Next
Workbooks.Open nPath & fileName
Sheets("Main").Select
Dim d As Long
For d = 2 To ActiveSheet.UsedRange.Rows.Count
If Cells(d, "C").Value = "T+1" Then
Cells(d, "L").Copy
ThisWorkbook.Activate
ActiveSheet.Select
Range("G7").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Application.CutCopyMode = False
End If
Next
Range("D7").Select
Application.CutCopyMode = False
Workbooks(fileName).Close

End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Could you provide a sample of the "Main" sheet using the XL2BB add in so we have some actual data to work with?
Main.SS2.xlsx
ABCDEFGHIJKL
1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2AS OF 10/12/2022
3
4
5
6
7
8DMMDSNDescTLSHTLSHPRYMTLACTLCTLATLAA
9110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0910.0010.0010.0010.00
10110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0910.0010.0010.0010.00
11110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0910.0010.0010.0010.00
12110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0910.0010.0010.0010.00
13110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0910.0010.0010.0010.00
14110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0910.0010.0010.0010.00
15110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0910.0010.0010.0010.00
16110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0910.0010.0010.0010.00
17110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0910.0010.0010.0010.00
18110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0910.0010.0010.0010.00
19110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0910.0010.0010.0010.00
20110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0910.0010.0010.0010.00
21110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0910.0010.0010.0010.00
22110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0910.0010.0010.0010.00
23110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0810.0010.0010.0010.00
24110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0510.0010.0010.0010.00
25110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0710.0010.0010.0010.00
26110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0710.0010.0010.0010.00
27110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0310.0010.0010.0010.00
28110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0010.0010.0010.0010.00
29110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%2.9810.0010.0010.0010.00
30110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0510.0010.0010.0010.00
31110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0510.0010.0010.0010.00
32110/13/2022xxxxxxxxxxxxxxxxxxxxxx10.000.0%3.0510.0010.0010.0010.00
331T+1240.000.0%3.07240.00240.00240.00240.00
34
35110/14/2022xxxxxxxxxxxxxxxxxxxxxx5.000.0%3.345.005.005.005.00
36110/14/2022xxxxxxxxxxxxxxxxxxxxxx5.000.0%3.135.005.005.005.00
37110/14/2022xxxxxxxxxxxxxxxxxxxxxx5.000.0%1.885.005.005.005.00
38110/14/2022xxxxxxxxxxxxxxxxxxxxxx5.000.0%1.885.005.005.005.00
39110/14/2022xxxxxxxxxxxxxxxxxxxxxx5.000.0%1.875.005.005.005.00
40110/14/2022xxxxxxxxxxxxxxxxxxxxxx5.000.0%1.875.005.005.005.00
41210/14/2022xxxxxxxxxxxxxxxxxxxxxx5.000.0%3.045.005.005.005.00
42210/14/2022xxxxxxxxxxxxxxxxxxxxxx5.000.0%2.575.005.005.005.00
43210/14/2022xxxxxxxxxxxxxxxxxxxxxx5.000.0%2.575.005.005.005.00
441T+245.000.0%2.4645.0045.0045.0045.00
45
Sheet1
Cell Formulas
RangeFormula
F33,I33:L33F33=SUM(F9:F32)
F44,I44:L44F44=SUM(F35:F43)
 
Upvote 0
Couple of things: try to avoid using ActiveSheet.Select, if you run the code from the wrong sheet things will go wrong. Along the same lines, be very specific with your sheet names – you’ll avoid much pain in the future.

I can’t fully test your code because I don’t have your folder setup to open the file you’re after, but the code below does what I think you’re looking for. Just change the destination sheet to its proper name.

VBA Code:
Option Explicit
Sub Get_Data()
    
    Dim nPath As String
    Dim fileNameString As String
    Dim fileName As String
    nPath = "\\Swib\root\ShrInvOper\Fund Accounting\Reporting\InvestOne\"
    fileNameString = Format(Date, "dd-mmm-yyyy") & " 1 SIF Holding Detail by Maturity Date" & ".xls"
    fileName = Dir(nPath & fileNameString)
    Workbooks.Open nPath & fileName
    
    Dim wb As Workbook
    Set wb = ActiveWorkbook
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = wb.Worksheets("Main")
    Set ws2 = ThisWorkbook.Worksheets("Sheet2") '<<< change this to your actual sheet name

    Dim LRow As Long
    LRow = ws1.Cells.Find("*", , xlFormulas, , 1, 2).Row
    
    With ws1.Range("B8:L" & LRow)
        .AutoFilter 2, "T+1"
        .Offset(1, 7).Resize(.Rows.Count - 1, 4).Copy
        ws2.Range("D7").PasteSpecial xlPasteValuesAndNumberFormats
        Application.CutCopyMode = 0
        .AutoFilter
    End With

End Sub
 
Upvote 0
Couple of things: try to avoid using ActiveSheet.Select, if you run the code from the wrong sheet things will go wrong. Along the same lines, be very specific with your sheet names – you’ll avoid much pain in the future.

I can’t fully test your code because I don’t have your folder setup to open the file you’re after, but the code below does what I think you’re looking for. Just change the destination sheet to its proper name.

VBA Code:
Option Explicit
Sub Get_Data()
   
    Dim nPath As String
    Dim fileNameString As String
    Dim fileName As String
    nPath = "\\Swib\root\ShrInvOper\Fund Accounting\Reporting\InvestOne\"
    fileNameString = Format(Date, "dd-mmm-yyyy") & " 1 SIF Holding Detail by Maturity Date" & ".xls"
    fileName = Dir(nPath & fileNameString)
    Workbooks.Open nPath & fileName
   
    Dim wb As Workbook
    Set wb = ActiveWorkbook
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = wb.Worksheets("Main")
    Set ws2 = ThisWorkbook.Worksheets("Sheet2") '<<< change this to your actual sheet name

    Dim LRow As Long
    LRow = ws1.Cells.Find("*", , xlFormulas, , 1, 2).Row
   
    With ws1.Range("B8:L" & LRow)
        .AutoFilter 2, "T+1"
        .Offset(1, 7).Resize(.Rows.Count - 1, 4).Copy
        ws2.Range("D7").PasteSpecial xlPasteValuesAndNumberFormats
        Application.CutCopyMode = 0
        .AutoFilter
    End With

End Sub
Thank you for the tips-
I use ActiveSheet because the tab names change each day. Tabs are named to reference dates. (ex: 10.12.22, 10.11.22, 10.10.22.) How would you code this to open the right tab?
 
Upvote 0
To be clear, is "Main" the 'source' data sheet and ActiveSheet the 'destination' sheet?
 
Upvote 0
Thank you for the tips-
I use ActiveSheet because the tab names change each day. Tabs are named to reference dates. (ex: 10.12.22, 10.11.22, 10.10.22.) How would you code this to open the right tab?
The code you provided worked well- thank you!. Now the only question left is how to code it to reference a tab named as a date ( previous reply) .
 
Upvote 0
The code you provided worked well
How did you get the code to 'work well'. In other words, what method did you use to select the correct sheet (tab)? If it changes every day, then you might as well use ActiveSheet - and I'll rejig the code once you answer:
To be clear, is "Main" the 'source' data sheet and ActiveSheet the 'destination' sheet?
 
Upvote 0
How did you get the code to 'work well'. In other words, what method did you use to select the correct sheet (tab)? If it changes every day, then you might as well use ActiveSheet - and I'll rejig the code once you answer:
I used ActiveSheet, and yes, "Main" the 'source' data sheet and ActiveSheet the 'destination' sheet?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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