Excel vba:根据唯一ID找到一行,拉出,编辑它,然后将其导回到修订版的同一位置

时间:2016-12-10 15:34:16

标签: excel excel-vba macros vba

我正在使用excel中的大型数据表,我正在尝试创建一个新工作表(Sheet2),它使用vba /宏来允许在sheet1中处理数据。我已经创建了一个功能,可以在表格的底部添加一个新行(见下文),但现在我正试图找出一种方法来根据它的' ID(Sheet1中的A列),然后让用户编辑或修改它,然后将其导出回Sheet1中的'原始位置(我还附上了一张图片)。

interface IService
{
    public function interfaceFunction();
}

final class ServiceWithOtherFunction implements IService
{
    public function interfaceFunction() { echo "ServiceWithOtherFunction interfaceFunction\n"; }

    public function otherFunction() { echo "ServiceWithOtherFunction otherFunction\n"; }
}

final class Controller
{
    private $service;

    public function __construct(IService $service)
    {
        $this->service = $service;
    }

    public function indexAction()
    {
        $this->service->interfaceFunction();

        $this->service->otherFunction();
    }
}

$controllerWithOtherFunction = new Controller(new ServiceWithOtherFunction);

$controllerWithOtherFunction->indexAction();

excel_macro_format

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您需要添加一个功能来查找ID。一旦你有了ID,剩下的就是从sheet3复制到sheet2,然后再复制到表3.我已经实现了查找ID和复制数据的函数。导入和导出的结构基本相同,但从函数的参数复制和粘贴到函数的互换

Sub Import()
    Dim copySheet As Worksheet
    Dim pasteSheet As Worksheet

    Set copySheet = Worksheets("Sheet3")
    Set pasteSheet = Worksheets("Sheet2")

    Dim idRow As Integer
    idRow = findId(pasteSheet.Range("C3"))

    If (idRow = 0) Then
        MsgBox ("ID not found")
        End
    End If

    'implement a function that finds the ID and saves the row number in idRow

    Call CopyPaste(copySheet.Range("B" & idRow & ":R" & idRow), _
        pasteSheet.Range("A" & Rows.Count).End(xlUp).Offset(1, 0))

End Sub

Sub Export()
    Dim copySheet As Worksheet
    Dim pasteSheet As Worksheet

    Set copySheet = Worksheets("Sheet3")
    Set pasteSheet = Worksheets("Sheet2")

    Dim idRow As Integer
    idRow = findId(pasteSheet.Range("C3"))

    If (idRow = 0) Then
        MsgBox ("ID not found")
        End
    End If

    'implement a function that finds the ID and saves the row number in idRow

    Call CopyPaste(pasteSheet.Range("A" & Rows.Count).End(xlUp).Resize(1, 17), _
        copySheet.Range("B" & idRow & ":R" & idRow))

End Sub

Function findId(ids As String) As Integer
    Dim findRow As Range
    Sheets("Sheet3").Select
    'Range("A1:A" & Rows.Count).Select

    Columns("A:A").Select
    Set findRow = Selection.Find(What:=ids, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

    If Not findRow Is Nothing Then
        findId = findRow.Row
        Else
        findId = 0
    End If
End Function

Sub CopyPaste(from As Range, copyTo As Range)
    from.Copy Destination:=copyTo
    Application.CutCopyMode = False
End Sub