Excel将单元格值插入函数并返回新值(而不是公式)

时间:2018-12-04 19:27:44

标签: excel vba excel-vba

我正在尝试编写一个宏,该宏将通过将它们插入到函数中然后粘贴结果来覆盖列中的所有值。当我尝试此操作时,我的函数认为插入的变量是文本,而不是变量。我不确定如何使其识别为变量。 该单元格的格式始终为12345-6789,我试图使其返回值12345。但是,该单元格最终以值= Clean_Zip(cell.Value)结束,而不是插入公式,运行它并拉出返回值。

Sub FixItUp()

Dim LastRow As Integer
Dim rng As Range, cell As Range


LastRow = ActiveSheet.Range("F" & ActiveSheet.Rows.Count).End(xlUp).Row

Set rng = ActiveSheet.Range("F5:F" & LastRow)

For Each cell In rng
    cell.Value = "=Clean_Zip(cell.Value)"
Next cell

End Sub




Public Function Clean_Zip(ZipCode)

'Function formats all zipcodes to a 5 digit number & converts text values to 
 number values

ZipCode = Trim(ZipCode)
Select Case countNumbers(ZipCode)

    Case Is <= 99
        Clean_Zip = "Error"
    Case Is <= 999
        Clean_Zip = "00" & countNumbers(ZipCode)
    Case Is <= 9999
        Clean_Zip = "0" & countNumbers(ZipCode)
    Case Is <= 999999
        Clean_Zip = Left(ZipCode, 5)
    Case Is <= 99999999
        Clean_Zip = "0" & Left(ZipCode, 4)
    Case Is <= 999999999
        Clean_Zip = Left(ZipCode, 5)

    Case countNumbers(ZipCode)
       Clean_Zip = countNumbers(ZipCode)

    If InStr(5, ZipCode, " ") Then
        Clean_Zip = Left(ZipCode, 5)
    End If

End Select
End Function




Public Function countNumbers(Cell)

If Left(Cell, 3) = 0 Then
        countNumbers = Mid(Cell, 4, 2)
    ElseIf Left(Cell, 2) = 0 Then
        countNumbers = Mid(Cell, 3, 3)
    ElseIf Left(Cell, 1) = 0 Then
        countNumbers = Mid(Cell, 2, 4)
    ElseIf IsNumeric(Left(Cell, 1)) And InStr(Left(Cell, 3), "-") Then
        countNumbers = "000" & Left(Cell, 2)
    ElseIf IsNumeric(Left(Cell, 1)) And InStr(Left(Cell, 4), "-") Then
        countNumbers = "00" & Left(Cell, 3)
    ElseIf IsNumeric(Left(Cell, 1)) And InStr(Left(Cell, 5), "-") Then
        countNumbers = "0" & Left(Cell, 4)
    ElseIf IsNumeric(Left(Cell, 1)) And InStr(Left(Cell, 6), "-") Then
        countNumbers = "0" & Left(Cell, 5)
    ElseIf IsNumeric(Left(Cell, 1)) Then
        countNumbers = Left(Cell, 10)
    Else
        countNumbers = Trim(Left(Cell, 3) & " " & Right(Cell, 3))
End If

End Function

2 个答案:

答案 0 :(得分:1)

使用TextToColumns可能会有更好的方法。

import numpy as np
from scipy import integrate
import pandas as pd

class F1:
    def __init__(self):
        pass
    def __call__(self, state):
        return 2 - state[0]**2 * state[1]

class F2:
    def __init__(self):
        pass
    def __call__(self, state):
        return 3 - state[1]*state[0]

fs = np.array([F1(), F2()])
state0 = np.ones(2)*4

def change(t, state):
    """
    This is the function I would like to change for something better!

    Ideally there would be something like: dstate = map(input_to_apply, list_of_functions)
    """
    dstate = np.zeros(2)
    for i,f in enumerate(fs):
        dstate[i] = f(state = state)
    return dstate

sol = integrate.solve_ivp(fun = change, t_span = (0, 15), y0 = state0)
res = pd.DataFrame(sol.y.T, columns = ['A', 'B'])
res.index = sol.t
ax = res.plot()

格式错误的zip会保留其原始值,同时在红色背景上显示“错误”。

答案 1 :(得分:0)

在您的FixItUp子中,更改以下行:

cell.Value = "=Clean_Zip(cell.Value)"

对此:

Cell.Formula = "=Clean_Zip(""" & Cell.Value & """)"

或者,最好在一个简单的子程序中处理数据并覆盖所有值,而不是使用自定义函数……这是一个示例:

Sub FixItUpV2()

    Dim LastRow As Integer
    Dim rng As Range, ZipCode As Range

    LastRow = ActiveSheet.Range("F" & ActiveSheet.Rows.Count).End(xlUp).Row

    Set rng = ActiveSheet.Range("F5:F" & LastRow)

    For Each ZipCode In rng
        'Change cell format to text:
        ZipCode.NumberFormat = "@"
        'Split original value on its hyphen, keep the first segment, and format with a 5 digit mask:
        ZipCode = Format(Split(Trim(ZipCode), "-")(0), "00000")
    Next ZipCode

End Sub