英文:
Function not using the right range in VBA
问题
以下是翻译的部分:
"我使用了从Excel技巧中获取的函数来获取名称的第一个部分匹配的行。"
"所有这些的想法是为了跟踪假期。我有一个特殊的电子表格被用作数据库,我在其中存储所有的数据。每一次的更改都存储在数据电子表格中,该电子表格位于我们的云中。这用于将数据传播到其他计算机,使用复制粘贴到每个工作空间的表格中。"
"我的问题是有时使用的函数会在错误的范围内搜索员工。"
"这是函数代码:"
"以下是我们使用的代码来通过表单添加假期:"
"问题出现在以下if部分:"
"我想知道我的错误在哪里,因为我在整个代码中都没有引用代码电子表格或表,只是用它来显示从我的其他文件中镜像的数据。"
"如您所请求的open_wb_onedrive代码:"
英文:
I am using a function I got from Excel tips to get the row of the first partial match of a name.
The idea of all this is to track holidays.
I have one special spreadsheet being used as a database where I store all the data. Every change made is stored in the data spreadsheet which in in our cloud. That is used to spread the data into other computers using copy paste to every workspaces sheet.
My issue is sometimes the function being used searches for the employee using the wrong range.
This is the function code:
Function FirstPartMatch(str As String, rng As Range)
Dim tmp As Long
Dim position As Long
Dim cll As Range
position = 0
tmp = 0
For Each cll In rng
tmp = tmp + 1
If InStr(1, LCase(cll.Value2), LCase(str)) > 0 Then
position = tmp
Exit For
End If
Next cll
If position Then
FirstPartMatch = position
Else
FirstPartMatch = "#NA"
End If
End Function
Here is my code that we use to add holidays via a form:
Private Sub cmdOk_Click()
Dim name As String
Dim ws As Worksheet
Dim vdate As Date
Dim agency As String
Dim code As String
Dim lastrow As Long
Dim Rlastrow As Long
Dim holiday As Double
Application.ScreenUpdating = False
open_wb_onedrive 'open data file
Set ws = openwb.Worksheets("data") 'assign sheet of data file to variable
With ws
.Unprotect Password:=pass
lastrow = .Cells(Rows.Count, 22).End(xlUp).Row + 1 'finds first empty row of the holidays column
Rlastrow = .Cells(Rows.Count, 1).End(xlUp).Row + 1 'finds the first empty row of the employee column
'assign the imput data to variables
name = Me.cmbName.value
agency = Me.cmbPosition.value
vdate = CDate(Me.cmbDate.value)
'Checks if name is already in holiday column, and skips the code if it is
If FirstPartMatch(name, .Range("V1:V" & lastrow)) > 0 Then
MsgBox ("Holiday already exists")
GoTo Skip
Else
'Adds data if new name on holiday list
.Cells(lastrow, 22).value = name
.Cells(lastrow, 23).value = vdate
holiday = FirstPartMatch(name, .Range("A1:A" & Rlastrow)) 'finds in which row the employee is
.Cells(holiday, 1).value = name & " Holiday until " & vdate 'ads the holiday info
End If
.Protect Password:=pass
End With
'end of sub code to close the data file
Skip:
get_data ws
Unload Me
End Sub
The issue on the code happens on this if part:
If FirstPartMatch(name, .Range("V1:V" & lastrow)) > 0 Then
Instead of searching on the data file in column V, the code does the search on my original file and sheet on column A. (where the code is being run)
I wonder where is my mistake as I don't reference the code spreadsheet or sheet in the whole code and just use it to display my data mirrored from my other file.
Code for open_wb_onedrive as requested:
Sub open_wb_onedrive()
Dim wbFullName
Set objLogExcel = CreateObject("Excel.Application")
objLogExcel.Visible = True
wbFullName = "https://onedrive_hidden_url/data.xlsx"
Set openwb = objLogExcel.Workbooks.Open(wbFullName)
End Sub
答案1
得分: 0
找到解决方法:(仍然不明白为什么先前的代码部分有效而部分无效)
'检查名称是否已经在假期列中,如果是,则跳过代码
If InStr(1, LCase(name), LCase("holiday")) <> 0 Then
MsgBox ("假期已存在")
GoTo Skip
Else
'如果不是,则将数据添加到表格中
.Cells(lastrow, 22).Value = name
.Cells(lastrow, 23).Value = vdate
holiday = FirstPartMatch(name, .Range("A1:A" & Rlastrow)) '找出员工所在的行
.Cells(holiday, 1).Value = name & " 假期至 " & vdate '添加假期信息
请注意,代码部分不进行翻译。
英文:
Workaround found: (Still baffled why part of the previous code works and part doesn't)
'Checks if name is already in holiday column, and skips the code if it is
If InStr(1, LCase(name), LCase("holiday")) <> 0 Then
MsgBox ("Holiday already exists")
GoTo Skip
Else
'In the case it's not adds the data to the table
.Cells(lastrow, 22).value = name
.Cells(lastrow, 23).value = vdate
holiday = FirstPartMatch(name, .Range("A1:A" & Rlastrow)) 'finds in which row the employee is
.Cells(holiday, 1).value = name & " Holiday until " & vdate 'ads the holiday info
答案2
得分: 0
你的 openwb
对象在 cmdOK_Click
子程序中不可用。将以下代码放在 open_wb_onedrive
子程序所在模块的声明部分:
Dim openwb as Workbook
英文:
Your openwb
object is not available in the cmdOK_Click
Sub. Place a
Dim openwb as Workbook
in the Declaration section of the module where the open_wb_onedrive
Sub is.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论