简体   繁体   中英

Copy cells from one Workbook (non-specific) to another (specific)

Okay, so I'm trying to create an order log for a business (employee usage).

I have a form set out where multiple users can access/input all the information needed. It prints into an Excel spreadsheet for one person to open and place orders from. Sometimes a user might want to copy an order line from a previous year's log (there are 4 so far), so I'm trying to incorporate that into the current log.

I'm having trouble with referencing another workbook that the user has opened and copying the information into a userform opened in the current log. I have this:

Private Sub cmdCopy_Click()

Dim rowRef As Integer, colItem As Integer, colSup As Integer, colCatNum As Integer, colQty As Integer, colUnit As Integer, colCat As Integer

'stores row of selected order from old order log successfully
rowRef = ActiveCell.row

'I'm trying to find which column has which headers as the logs aren't consistent
For x = 2 To 9
    If ActiveWorkbook.Sheet1.Cells(2, x).Text = "Item" Then
        colItem = x
    ElseIf ActiveWorkbook.Sheet1.Cells(2, x).Text = "Supplier" Then
        colSup = x
    ElseIf ActiveWorkbook.Sheet1.Cells(2, x).Text = "Catalogue #" Then
        colCatNum = x
    ElseIf ActiveWorkbook.Sheet1.Cells(2, x).Text = "Qty" Then
        colQty = x
    ElseIf ActiveWorkbook.Sheet1.Cells(2, x).Text = "Unit" Then
        colUnit = x
    ElseIf ActiveWorkbook.Sheet1.Cells(2, x).Text = "Category" Then
        colCat = x
    End If
Next x

'fills information into userform
txtItem.Text = Sheet1.Cells(rowRef, colItem).Text
txtSup.Text = Sheet1.Cells(rowRef, colSup).Value
txtCatNum.Text = Sheet1.Cells(rowRef, colCatNum).Value
txtQty.Text = Sheet1.Cells(rowRef, colQty).Value
cboUnit.Text = Sheet1.Cells(rowRef, colUnit).Value
cboCat.Text = Sheet1.Cells(rowRef, colCat).Value

End Sub

So what I want to happen is:

  1. User opens current order log
  2. User presses ActiveX button to open frmOrderLog
  3. a) User enters information manually
    b) User copies information from current year's log
    c) User minimizes window, opens previous year's log, selects a cell, and in the userform presses "Copy Order" button which then copies the information from corresponding cells from the previous year's order log into the corresponding text boxes/combo boxes in the form within the current year's order log

If there is any requirement for clarification, let me know. I'm sorry if this is in the forums already, but using my search terms I could not find answers that helped.

Seems like a simple syntax issue to me:

you want to change:

If ActiveWorkbook.Sheet1.Cells(2, x).Text = "Item" Then

To:

If ActiveWorkbook.Sheets(1).Cells(2, x).Text = "Item" Then

The Sheet1 syntax didn't work for me.

And obviously you want to make the same change wherever else Sheet1 occurs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM