这是我第一次在这里发帖
我有错误:
error 91 Object variable or With block variable not set
在r2Val = activSheet.Columns(1).Find
行...
我已经这样做了一个月了,并且陷入了导入部分
这是工作表更新列表 www.mediafire.com/view/?av8skl7e3ry93p3 上面打开一个文件浏览器,用于选择要导入的工作表的工作簿
这是我要填写的表格 http://www.mediafire.com/view/?r7y2xfa2s7kc9wx
这是从中获取数据的地方 http://www.mediafire.com/view/?6wp8ywme1kgehqn
我目前的代码是:
' 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
对于excel vba来说,我仍然是新手
我想要它做的是:
如果你想看到来源 得到它:www.mediafire.com/?9z924s7wtrb5md3
这是我要导入的工作表:www.mediafire.com/view/?i7td9gm336wg6cg
我无法发布图片和链接,所以如果mods或mod类用户可以清理它,那么我很感激
任何建议,更正,提示都会有所帮助
答案 0 :(得分:0)
第91行错误的原因一目了然,您尝试仅使用=
设置范围,而应使用Set
,即:
Set r2Val = activSheet.Columns(1).Find(What:=rVal, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
以下c2Val
也是如此。首先尝试纠正这些。