This is my first time posting here
I have an error:
error 91 Object variable or With block variable not set
on the line r2Val = activSheet.Columns(1).Find…
I have been doing this for a month now and got stuck to the import part
This is the sheet update list
http://www.mediafire.com/view/?av8skl7e3ry93p3
The above opens a file browser to pick a workbook with the sheet to import
This is the sheet that I want filled
http://www.mediafire.com/view/?r7y2xfa2s7kc9wx
This is where it will get the data from
http://www.mediafire.com/view/?6wp8ywme1kgehqn
My current code for this to work:
' updates data based on excel or csv file uploaded
' This version uses "find" to find similar meterID and billing period between 2 worksheets
Sub Push2Sheets(filePath As String, shtName As String)
On Error GoTo ErrorHandler
If filePath = "False" Then Exit Sub
Dim targetWorkbook As Workbook 'workbook to get data from
Dim MyWorkbook As Workbook 'this workbook to merge
Set MyWorkbook = Application.ActiveWorkbook 'sets this workbook for merging
Set targetWorkbook = Application.Workbooks.Open(filePath) 'copies source workbook to memory
Dim activSheet As Worksheet
Set activSheet = MyWorkbook.Worksheets(shtName) 'selects the worksheet to merge with source sheet
Dim sourceSheet As Worksheet
If targetWorkbook.Sheets.Count > 1 Then 'checks first if the target workbook has one or many sheets to draw data
Set sourceSheet = targetWorkbook.Worksheets(1)
Else
Set sourceSheet = targetWorkbook.Worksheets(shtName)
End If
Dim rw As Long 'used as a counter for reading the first column of the source sheet
Dim Col As Long 'used as a counter for reading the first row of the source sheet
Dim rVal As String, r2Val As Range 'row value
Dim cVal As String, c2Val As Range 'cell value
For rw = 2 To sourceSheet.Rows.Count
rVal = sourceSheet.Cells(rw, 1).Value
Debug.Print rVal
'this finds if there is a similar meterID in the target sheet (This Workbook)
r2Val = activSheet.Columns(1).Find(What:=rVal, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not r2Val Is Nothing Then
For Col = 2 To sourceSheet.Columns.Count
cVal = sourceSheet.Cells(1, Col).Value
Debug.Print cVal
'uses the table headers to find a match and copies the content of source to target sheet if found
c2Val = activSheet.Rows(1).Find(What:=cVal, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not c2Val Is Nothing Then
sourceSheet.Cells(rw, Col).Copy Destination:=activSheet.Cells(r2Val.Row, c2Val.Column)
End If
Next
Else
Call UniAutoFiller 'adds a new row at the end of the table if there is a new MeterID
[addrow].Offset(-1, 0).Value = rVal
End If
Next
targetWorkbook.Close SaveChanges:=False
Exit Sub
ErrorHandler:
If Not err.Number = 0 Then Call LogInformation(ThisWorkbook.Name & " has error " & err.Number & " " & err.Description & " on " & Format(Now, "yyyy-mm-dd hh:mm"))
MsgBox "Something went wrong :?" & vbNewLine & vbNewLine & "Make sure the worksheet name you" & _
vbNewLine & "are importing match the internal sheet name"
End Sub
I’m still a novice when it comes to excel vba
What I want it to do is to:
- open the external workbook, with the full accomplished worksheet
- find a match on the meterID with the meterID of the internal sheet,
- if found find a match on the billing period (date) on the column, if
- found copy the data where the meterID and billing period got a match
- repeat 1-4 until it reaches the end of the table
if you want to see the source
get it here: http://www.mediafire.com/?9z924s7wtrb5md3
this is the sheet I’m trying to import: http://www.mediafire.com/view/?i7td9gm336wg6cg
I cant post images and links yet so if mods or mod class user can clean this up then Im grateful
Any advice, corrections, tips will really help
At a glance the cause of line 91 error is that you try to set Range using just
=, whileSetshould be used instead, i.e.:The same is true for
c2Valbelow. Try to correct these first.