VB Define Dynamic Ranges, use the range for find or Vlookup

arkusM

Well-known Member
Joined
Apr 12, 2007
Messages
560
I have an worksheet that I import a csv into, each day a new csv is added to the bottom of the previous csv data. I have some code that extracts the date and month # from the cell and places them in helper columns. The code find the last used cell in the helper column and the imported data column to find the first and last row of the new day.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
This part works fine. However, I assigned a variable name to the first and last variables and would like to uses these row number to define a range in order to use the range for a vlookup or find operation. This is where I get stuck. I want to use column x and row (variable from first bit of code) to column y and row (variable from first bit of code). Then use a vlookup/find whatever works to find the text I need and get the data. Tips on looping the code would be welcome as well.

I have searched for answers to the problem, on the board and web, but have not found a solution that works for me. (at least that I could get to work!!)
<o:p></o:p>
Below is the code as I have it <o:p></o:p>
(I do realize there may be/are more efficient ways to do this and any comments are much appreciated, but not necessarily solicited for the purposes of this question as I realize people are helping out of the goodness of their heart.)<o:p></o:p>
<o:p> </o:p>
Thanks in advance.<o:p></o:p>
<o:p> </o:p>

Rich (BB code):
Public Sub Enter_Date()
    
    Dim DateA As Date
    Dim DateB As Date
    Dim Cnt As Integer
    Dim End_of, Beg_of As Integer
    Dim Count As Integer
    Dim NumtoFill As Integer
    ' To get the inventory sales number
    Dim InventoryVol As Range
    'Dim Inventory As String
    Dim Inventory As Integer
On Error GoTo Handler
        ' Grabs the first line
    Inventory = Range("A65536").End(xlUp).Offset(1, 4).Row()
        'Puts the Date from the GSO into the first cell
    DateA = Mid(Range("a65536").End(xlUp).Offset(6, 4), 14, 10)
    Range("A65536").End(xlUp).Offset(1, 0).Value = DateA
        ' Add Error Checking for Blank Line to modify where the date appears
    
        'Copies the cell down
    End_of = Range("E65536").End(xlUp).Row()
    Beg_of = Range("A65536").End(xlUp).Row()
    NumtoFill = End_of - Beg_of
        ''MsgBox NumtoFill
    DateB = Range("A65536").End(xlUp).Activate
   
    For Cnt = 0 To NumtoFill
        ActiveCell.Offset(Cnt, 0).Value = DateA
        ActiveCell.Offset(Cnt, 1).Value = Month(DateA)
    Next Cnt

    'Get the total Inventory sales for the day
    MsgBox Inventory
    MsgBox NumtoFill
    
    On Error GoTo Handler2
    'Define range This is where I am Stuck
    Set InventoryVol = Range(Cells(Inventory, 5), Cells(NumtoFill, 12))
    MsgBox InventoryVol
    
    Dim varFind As Variant
    
    On Error GoTo Handler3
    
    With Worksheets("GSO DATA").Range("InventoryVol")
        Set varFind = .Cells.Find(What:="Total Inventory Sales:", LookIn:=xlValues)
        If Not varFind Is Nothing Then Range("A1").Value = .Cells(varFind.Row, varFind.Column + 1).Value
    End With
    
    'Application.WorksheetFunction.VLookup("Total Inventory sales",InventoryVol, 6,0)
    
    Exit Sub
Handler:
        MsgBox "No New Data to Assign Date to"
        Exit Sub
Handler2:
        MsgBox "Range Failure"
        Exit Sub
Handler3:
        MsgBox "Find Failure"
        Exit Sub
End Sub
 
Ok not true. I thought that I could just nest another
Code:
 With InventoryVol
        Set varFind = .Find(What:="Total Inventory Sales:", LookIn:=xlValues)
        If Not varFind Is Nothing Then Range("A1").Value = .Cells(varFind.Row, varFind.Column + 1).Value
    End With
in the "with" to do another find on another value and place it one column over with:
Code:
            With InventoryVol
                Set varFind = .Find(What:="Total Inventory Sales:", LookIn:=xlValues)
                If Not varFind Is Nothing Then Sheets("Summary").Range("A" & NextDay).Value = varFind.Offset(0, 5).Value
 
                Set varFind = .Find(What:="Total Inventory Purchases:", LookIn:=xlValues)
                If Not varFind Is Nothing Then Sheets("Summary").Range("A" & NextDay).Value = varFind.Offset(1, 5).Value
            End With
but it does not work. I am a bit shakey with looping and the example I can find don't do what I am looking for because they are assuming a counting method, but I don;t want to count I just want to find a few values ans place them in a table... DO you have any ideas?
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,215,248
Messages
6,123,867
Members
449,130
Latest member
lolasmith

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