Copy data based on cell value

Declanscully

New Member
Joined
Mar 5, 2013
Messages
12
I was helped out with and subsequentially modified a bit. This works but I was wondering if there is a way to modify it so that the BOLDED section can be more dynamic.

Essentially I want to try is:
Sequentially search cells C15 of the opened wb2. Search would be looking for the letters OT. If no OT was found then the Cell B15 would be entered into Cell I5 ofwb1 (.Offset(, 8).Value = wb2.Worksheets(i).Range("B15").MergeArea.Value). If OT was found, the value would be entered into Cell I6 of wb1.

Then it would search Cell B16 etc. until it hits an empty cell at which point it would move on to the next worksheet as it currently does.

I think I can figure out a way to brute force it, but was wondering if there is an elegant solution.


VBA Code:
Sub IMPORT_DATA()
Application.ScreenUpdating = False
Application.Calculation = False
    Const FirstRow As Long = 4
    Const InitialFileName = "c:\"
    Dim FileName As String
    FileName = getFileName(InitialFileName)
    If Len(FileName) = 0 Then Exit Sub 'exits sub if no file name is selected
    Dim wb2 As Workbook
    Dim sh As Worksheets
    Dim lr As Long, i As Long
    lr = FirstRow

If Len(FileName) > 0 Then
Set wb2 = Workbooks.Open(FileName)
End If

    For i = 3 To wb2.Worksheets.Count 'Starts workbook search after rate sheets
        lr = lr + 1 'sets start row as 5
        With ThisWorkbook.Worksheets("JOB NUMBER").Range("A" & lr)    '<----- Change as required
            .Value = CStr(wb2.Worksheets(i).Name)
            .Offset(, 1).Value = wb2.Worksheets(i).Range("J61").MergeArea.Value
            .Offset(, 2).Value = wb2.Worksheets(i).Range("J27").MergeArea.Value
            .Offset(, 4).Value = wb2.Worksheets(i).Range("J39").MergeArea.Value
            .Offset(, 6).Value = wb2.Worksheets(i).Range("J50").MergeArea.Value
            .Offset(, 7).Value = wb2.Worksheets(i).Range("J60").MergeArea.Value
        [B].Offset(, 8).Value = wb2.Worksheets(i).Range("B15").MergeArea.Value
            .Offset(, 9).Value = wb2.Worksheets(i).Range("B16").MergeArea.Value
            .Offset(, 10).Value = wb2.Worksheets(i).Range("B17").MergeArea.Value
            .Offset(, 11).Value = wb2.Worksheets(i).Range("B18").MergeArea.Value
            .Offset(, 12).Value = wb2.Worksheets(i).Range("B19").MergeArea.Value
            .Offset(, 13).Value = wb2.Worksheets(i).Range("B20").MergeArea.Value[/B]
            
            End With
    Next i
    wb2.Close False
    Application.ScreenUpdating = True
    Application.Calculation = True
End Sub

Public Function getFileName(Optional InitialFileName As String) As String
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .Title = "Select the a File"
        .InitialFileName = InitialFileName
        .Filters.Clear
        .Filters.Add "Excel files", "*.xls*"
        '.Filters.Add "All files", "*.csv"
        '.Filters.Add "All files", "*.*"

        If .Show = -1 Then
           getFileName = .SelectedItems(1)
        End If
        
    End With
End Function
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I am pretty sure I have found the way to brute force it using INSTR and IF statements. Just have to find a way to be able to cycle through it using an FOR statement.

VBA Code:
If InStr(wb2.Worksheets(3).Range("C54"), "Hotel") > 0 Then
    ThisWorkbook.Worksheets("ADMIN").Range("E10").Value = wb2.Worksheets(3).Range("J54").MergeArea.Value
        End If
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Copy data based on cell value
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,214,825
Messages
6,121,788
Members
449,049
Latest member
greyangel23

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