I still think you haven't shared enough information.
There are a lot of select's and activate's in your code. It's difficult to see
what's going on (for me, at least).
I started rewriting your code into something I like (a common problem for me!),
but I got to a point where I didn't know enough to go any further.
Anyway, I did this:
Option Explicit
Sub testme()
Dim CurrDate As Variant 'could be boolean
Dim DBName As String
Dim DBSheet As String
Dim Query As Long
Dim Dropper As Variant
Dim BklDate As Double
Dim Latest As Double
Dim DestCell As Range
'this is one more than you need
'date is in column A???
Dim NumberOfColsInDLFile As Long
Dim DLLookupRng As Range
CurrDate = Application.InputBox(prompt:="Enter number of Duetsche Bank
download." _
& " Please enter number only, e.g., if filename: MW123456, then enter:
123456", _
Title:="MW file number", Default:=1184775073313#, Type:=2)
If CurrDate = False Then
Exit Sub
End If
DBName = "MW" & CurrDate & ".xls"
DBSheet = "MW" & CurrDate
With Workbooks(DBName).Worksheets(DBSheet)
Latest = .Range("a2").Value
'if you hit ctrl-end in that dl file,
'do you go to the last column?
NumberOfColsInDLFile = .Cells.SpecialCells(xlCellTypeLastCell).Column
'if it over shoots,
'can you trust row 1 to get the number of columns used
NumberOfColsInDLFile = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set DLLookupRng _
= .Range("b1", .Cells(1, NumberOfColsInDLFile)).EntireColumn
End With
With Workbooks("Model.xls").Worksheets("daily balances")
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Value
BklDate = DestCell.Value
If Latest <= BklDate Then
Query = MsgBox(prompt:="Do you want to overwrite existing values?",
_
Buttons:=vbYesNo + vbQuestion)
If Query = vbYes Then
'If you wanted an exact match, you'd need that 3rd parm = 0
Dropper = Application.Match(Latest, .Range("box"))
If IsError(Dropper) Then
MsgBox "No match for: " & Latest & " in the Box column"
Exit Sub
End If
Set DestCell = .Cells(Dropper, 1)
Else
Exit Sub
End If
End If
If Latest + 1 = BklDate Then
Set DestCell = DestCell.Offset(1, 0)
DestCell.Value = Latest
End If
'go to the right one column
Set DestCell = DestCell.Offset(0, 1)
'and this is where I got too confused.
destcell.FormulaR1C1 = _
"=VLOOKUP(R[" & -(Dropper - 1) & "]C,MW" & _
CurrDate & ".xls!C2:C9,8,FALSE)"
ActiveCell.Copy
Range(Selection, ActiveCell.Offset(, 6)).Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
End Sub
I expected to see a workbook name and worksheet name in the lookup table--and
that would mean it would look like:
=vlookup(zzzz,'[book3.xls]Sheet1'!$B:$i,9,FALSE)
But I didn't see that.
I wanted to use that range variable: DLLookupRng in the formula and let excel
figure out the syntax.
I'm not sure why the first column with the formula gets the value in the 8th
column in the table.
Maybe you can share an example of where the formula will go--a real address.
And where the data is being picked up from (r[-dropper+1]).
And what happens in those other columns.
But there is a variable named NumberOfColsInDLFile that you can use in the
..offset.
Range(Selection, ActiveCell.Offset(, NumberOfColsInDLFile -1)).Select
But I don't think you'll end up with something that works.
Dave Peterson said:
You may need to give more description to get more help.
hi dave, in original post i was trying to figure way to copy to a range that
would be changing over time. let me try to explain: we download an excel
file daily from bank which lists account balances. we have a model which
needs to be updated daily with the latest balances. code below, "box" is
named range which is column of dates. hope it's not too confusing . .
currdate = Application.InputBox("Enter number of Duetsche Bank download.
Please enter number only, e.g., if filename: MW123456, then enter: 123456",
"MW file number", 1184775073313#, , , , , 2)
Dim dbname As String
dbname = "MW" & currdate & ".xls"
Dim dbsheet As String
dbsheet = "MW" & currdate
Windows(dbname).Activate
Range("a2").Select
Dim latest As Single
latest = ActiveCell.Value
Windows("model.xls").Activate
Sheets("daily balances").Select
Cells(Rows.Count, "a").End(xlUp).Select
Dim bkldate As Single
bkldate = ActiveCell.Value
If latest <= bkldate Then
msg = "Do you want to overwrite existing values?"
Style = 3 + 32 + 0
query = MsgBox(msg, Style)
If query = vbYes Then
Dim dropper As Integer
dropper = Application.Match(latest, Range("box"))
Cells(dropper, 1).Activate
Else: Stop
End If
End If
If latest + 1 = bkldate Then
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = latest
End If
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(R[" & -(dropper - 1) & "]C,MW" & currdate &
".xls!C2:C9,8,FALSE)"
ActiveCell.Copy
Range(Selection, ActiveCell.Offset(, 6)).Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
i'm trying to figure out way to make fourth line up be a variable which
would copy over the same number of columns in download file