更改活动单元格
Changing the active cell
我想创建一个程序来检查 excel 中的一列文本并提取包含货币的第一行。货币为加元,格式为 "C $##.##",上限未知,但不太可能达到 10,000 美元。我希望将此操作执行 500 次,并将结果保存在 Master sheet 中。
我是 excel 中 VBA 的新手,非常感谢对以下代码的任何帮助。我 运行 遇到的问题是无法更改活动 sheet。脚本 returns #Value!并且不会超过 'SaSh.Range("A1").Select' 行。
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
,而是使用 Range
或 Cells
设置对范围的引用:
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
我想创建一个程序来检查 excel 中的一列文本并提取包含货币的第一行。货币为加元,格式为 "C $##.##",上限未知,但不太可能达到 10,000 美元。我希望将此操作执行 500 次,并将结果保存在 Master sheet 中。
我是 excel 中 VBA 的新手,非常感谢对以下代码的任何帮助。我 运行 遇到的问题是无法更改活动 sheet。脚本 returns #Value!并且不会超过 'SaSh.Range("A1").Select' 行。
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
,而是使用 Range
或 Cells
设置对范围的引用:
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