这是我第一次在这里发帖
我有一个错误:
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,我还是个新手
我想要它做的是:
- 打开外部工作簿,其中包含完整的工作表
- 在meterID上找到与内部工作表的meterID匹配的匹配项,
- 如果找到在列上的计费周期(日期)上找到匹配项,如果
- 找到复制meterID和计费周期匹配的数据
- 重复 1-4 直到它到达表的末尾
如果您想查看源代码,请在此处获取:www.mediafire.com/ ?9z924s7wtrb5md3
这是我要导入的工作表:www.mediafire.com/view/ ?i7td9gm336wg6cg
我还不能发布图片和链接,所以如果 mods 或 mod 类用户可以清理它,那么我很感激
任何建议、更正、提示都会有帮助