0

让我首先说我对使用 excel 和 VBA 非常陌生,但是对 c++ 有一些经验。

情况:

我正在尝试使用在另一个工作簿中找到的数据更新一个工作表。源文件的组织方式使每个新工作票都有一个列。随着越来越多的工单进来,就会创建更多的列,并且垂直列出有关该工单的各种信息。

基本上我想要做的是使用与第一个相同的票号更新第二个文件,但格式不同:

两张表的基本示例

这是我到目前为止所拥有的,虽然对于我想要代码做什么的基本概念来说非常粗糙:

Sub Update_Click() //Button to update destination file

Workbooks.open("C:\Documents\mysourcefile.xlsm")
dim i,j as integer
i=4 //starting column of source file where first ticket is stored
j=2 //starting column of destination file where first ticket is stored

while worksheets("mysourcesheet").Value(i,2)<>0 //all work has customer, but
                                                //may not have a ticket 
                                                //number

if Worksheets("mysourcesheet").value(i,1) = 0 Then

i=i+1          //some columns in the source are blank due to canceled orders
               //this is to go to the next column 

else
if Worksheets("mysourcesheet").value(i,1)=Worksheets("mydestsheet").value(j,1)
then
i=i+1
j-j+2      //go onto the next if already updated
           //J+2 to account for formatting of the cells

Else
Worksheets("mysourcesheet").value(i,1)=Worksheets("mydestsheet").value(j,1)
Worksheets("mysourcesheet").value(i,2)=Worksheets("mydestsheet").value(j,2)
Worksheets("mysourcesheet").value(i,3)=Worksheets("mydestsheet").value(j,4)
Worksheets("mysourcesheet").value(i,4)=Worksheets("mydestsheet").value(j,5)

//copy the data

i=i+1
j=j+2

end if
end if
end sub

我意识到这可能充满了错误/基本错误,但如果有人能伸出援助之手,那就太好了!

4

1 回答 1

1

如果客户不为空,这将复制新票,从源中的 1 列复制到 2 列

Private Sub Update_Click()
    Dim wb As Workbook, ur1 As Range, ur2 As Range, i As Long
    Dim fr1 As Long, fr2 As Long, lr1 As Long, lr2 As Long, lc1 As Long, lc2 As Long

    Application.ScreenUpdating = False

    Set wb = Workbooks.Open("E:\mysourcefile.xlsm")
    Set ur1 = Me.UsedRange
    Set ur2 = wb.Worksheets("mysourcesheet").UsedRange

    fr1 = ur1.Row: lr1 = fr1 + (ur1.Rows.Count - 1) - 1
    fr2 = ur2.Row: lr2 = fr2 + (ur2.Rows.Count - 1) - 1
    lc1 = ur1.Column + ur1.Columns.Count - 2:   lc2 = ur2.Column + ur2.Columns.Count - 1

    If Len(ur2.Cells(fr1 + 1, lc2)) > 0 Then                       'customer not empty
        If ur1.Cells(fr1 + 1, lc1) <> ur2.Cells(fr1 + 1, lc2) Then 'if last cutomer differ
            With ur1
              .Cells(fr1 + 0, lc1 + 2) = ur2.Cells(fr2 + 0, lc2)
              .Cells(fr1 + 1, lc1 + 2) = ur2.Cells(fr2 + 1, lc2)
              .Range(.Cells(fr1 + 0, lc1 + 2), .Cells(fr1 + 0, lc1 + 3)).MergeCells = True
              .Range(.Cells(fr1 + 1, lc1 + 2), .Cells(fr1 + 1, lc1 + 3)).MergeCells = True
              .Cells(fr1 + 2, lc1 + 2) = "Target"
              .Cells(fr1 + 2, lc1 + 3) = "Actual"
              .Cells(fr1 + 2, lc1 + 2).ColumnWidth = .Cells(fr1 + 2, lc1).ColumnWidth
              .Cells(fr1 + 2, lc1 + 3).ColumnWidth = .Cells(fr1 + 2, lc1).ColumnWidth
    .Range(.Cells(fr1, lc1 + 2), .Cells(fr1 + 2, lc1 + 3)).HorizontalAlignment = xlCenter
    .Range(.Cells(fr1, lc1 + 2), .Cells(lr1 + 1, lc1 + 3)).Borders.Weight = xlThin
              For i = fr1 + 3 To lr1 + 1
                  .Cells(i, lc1 + 2) = Now  'Target date
                  .Cells(i, lc1 + 3) = ur2.Cells(i - 1, lc2)
              Next
            End With
        End If
    End If
    wb.Close False
    Application.ScreenUpdating = True
End Sub

在此处输入图像描述

于 2015-10-17T22:57:31.963 回答