我打算创建一个程序来检查excel中的一列文本并提取包含货币的第一行。该货币为加元,格式为“C $ ##。##”,没有已知的上限,但不太可能达到10,000美元。我希望这次操作500次,并将结果保存在Master表中。
我是excel的VBA新手,非常感谢以下代码的任何帮助。我遇到的问题是无法更改活动表。该脚本返回#Value!并且没有越过'SaSh.Range(“A1”)这一行。选择'。
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
答案 0 :(得分:0)
@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