Vba 更改活动单元
我想创建一个程序,检查excel中的一列文本,并提取包含货币的第一行。货币单位为加元,格式为加元。没有已知的上限,但不太可能达到10000美元。我希望做这个操作500次,并将结果保存在一张母版纸上 我是excel中VBA的新手,希望能在以下代码方面得到帮助。我遇到的问题是无法更改活动工作表。脚本返回值!并且没有通过行“SaSh.RangeA1.Select”Vba 更改活动单元,vba,excel,excel-2007,Vba,Excel,Excel 2007,我想创建一个程序,检查excel中的一列文本,并提取包含货币的第一行。货币单位为加元,格式为加元。没有已知的上限,但不太可能达到10000美元。我希望做这个操作500次,并将结果保存在一张母版纸上 我是excel中VBA的新手,希望能在以下代码方面得到帮助。我遇到的问题是无法更改活动工作表。脚本返回值!并且没有通过行“SaSh.RangeA1.Select” Option Explicit Public NewValue As Integer 'Amount of Money current i
Option Explicit
Public NewValue As Integer 'Amount of Money current item is being sold for
Function PriceOfGoods(SaleString As String)
Dim SaleSheet As Worksheet
Set SaleSheet = Worksheets(SaleString)
NewValue = -1
Call PriceSearch(SaleSheet)
PriceOfGoods = NewValue
End Function
Public Sub PriceSearch(SaSh As Worksheet)
Dim StartNumber As Integer
Dim EndNumber As Integer
Dim CurrentCell As String
EndNumber = 1000
'Activating the Query Sheet and starting search at the top left corner of the sheet
SaSh.Range("A1").Select
'Keep searching the A column until you come across the Canadian Currency post
For StartNumber = 1 To EndNumber
CurrentCell = ActiveCell.Value
'Checking to see if the current cell is Canadian Currency
If WorksheetFunction.IsNumber(CurrencyValuation(CurrentCell)) Then
NewValue = CurrencyValuation(ActiveCell.Value)
Exit For
End If
'Continue search in the next row
ActiveCell.Offset(1, 0).Select
Next StartNumber
End Sub
Function CurrencyValuation(CurrencyInput As String)
Dim NewCurrency As Integer
NewCurrency = WorksheetFunction.Substitute(CurrencyInput, "C", "")
CurrencyValuation = NewCurrency
End Function
@Paradox和@Tim的评论都是有效的 为了明确回答您的问题,您不能从代码更改ActiveCell,而是使用一个或多个单元格设置对该范围的引用:
Public Sub PriceSearch(SaSh As Worksheet)
Dim StartNumber As Integer
Dim EndNumber As Integer
Dim CellToCheck As Range
EndNumber = 1000
'Search the Query Sheet and starting search at the top left corner of the sheet
'Keep searching the A column until you come across the Canadian Currency post
For StartNumber = 1 To EndNumber
Set CellToCheck = SaSh.Cells(RowIndex:=StartNumber, ColumnIndex:=1)
'Checking to see if the current cell is Canadian Currency
If WorksheetFunction.IsNumber(CurrencyValuation(CellToCheck.Value)) Then
NewValue = CurrencyValuation(CellToCheck.Value)
Exit For
End If
Next StartNumber
End Sub
在将SaSh传递给PriceSearch函数时,您是否考虑过它可能存在的任何问题?从工作表单元格调用的函数的功能受到限制:您可以重写PriceSearch子函数以避免使用ActiveCell,然后选择“尝试更具体”。i、 例如,工作簿、工作表、范围A1。选择Select和ActiveSheet/Cell会让人头疼。避免谢谢你的帮助。