Copy Text Between Workbooks Not Working Right

rodl66

New Member
Joined
Mar 8, 2023
Messages
26
Office Version
  1. 365
Platform
  1. Windows
To give context, I have checkboxes in the worksheet, when a “NO” checkbox is ticked, it opens a message box to input text, which then copies that input text to the next empty cell B14-B22.

For this code, I have a button for each row 14-22, when I click it, it will copy the text from the row’s cell B to a different workbook’s next empty cell D (starting at D5). Text from B14 gets copied to D5 like it should; however, instead of copying text in B15 to D6, it copies B14 text again. This happens for B16, B17, etc.

Sub AddEntryToIRTrackerA()

Dim irTracker As Workbook
Dim was As Worksheet
Dim lastRow As Long
Dim newNumber As String
Dim entryType As String
Dim sourceText As String


'Set a reference to the "IR_Tracker" workbook
Set irTracker = Workbooks("IR_TRACKER.xlsm")

' Set a reference to the "MPF IR" worksheet in the "IR_Tracker" workbook
Set ws = irTracker.Sheets("MPF IR")

'Find the last used row in column A of the "MPF IR" worksheet
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

'Generate the new number
newNumber = "MPF-" & Year(Now) & "-" & Format(lastRow - 3, "0000")

'Add the new number to column A
ws.Cells(lastRow + 1, 1).Value = newNumber

'Add the current date to column B
ws.Cells(lastRow + 1, 2).Value = Format(Now, "yyyy-mm-dd")

'Add the entry type to column C
ws.Cells(lastRow + 1, 3).Value = "A"

'Get the text from column B of the active sheet in the "Wall Enclosures" workbook
sourceText = ThisWorkbook.Sheets("TYPE A").Cells(ActiveCell.Row, 2).Value

'Add the source text to column D in the MPF IR worksheet
ws.Cells(lastRow + 1, 4).Value = sourceText

End Sub

Thank you in advance.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
If the idea is to copy the Col B data for whatever row the ThisWorkbook.Sheets("TYPE A") button is on, then you have to find a way to communicate that row information to your Sub AddEntryToIRTrackerA.
sourceText = ThisWorkbook.Sheets("TYPE A").Cells(ActiveCell.Row, 2).Value won't work reliably because which worksheet ActiveCell.Row is referencing is uncertain.

One example
VBA Code:
Sub AddEntryToIRTrackerA()
    Dim irTracker As Workbook, SaveWB As Workbook
    Dim ws As Worksheet, SaveWS As Worksheet
    Dim lastRow As Long
    Dim newNumber As String
    Dim sourceText As String
    Dim sourceRow As Long
    
    Set SaveWB = ActiveWorkbook
    Set SaveWS = ActiveSheet
    
    Application.ScreenUpdating = False
    With ThisWorkbook
        .Activate
        .Sheets("TYPE A").Activate
        sourceRow = ActiveCell.Row
    End With
    
    SaveWB.Activate
    SaveWS.Activate
    Application.ScreenUpdating = True
    
    'Set a reference to the "IR_Tracker" workbook
    Set irTracker = Workbooks("IR_TRACKER.xlsm")
    
    ' Set a reference to the "MPF IR" worksheet in the "IR_Tracker" workbook
    Set ws = irTracker.Sheets("MPF IR")
    
    'Find the last used row in column A of the "MPF IR" worksheet
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    'Generate the new number
    newNumber = "MPF-" & Year(Now) & "-" & Format(lastRow - 3, "0000")
    
    'Add the new number to column A
    ws.Cells(lastRow + 1, 1).Value = newNumber
    
    'Add the current date to column B
    ws.Cells(lastRow + 1, 2).Value = Format(Now, "yyyy-mm-dd")
    
    'Add the entry type to column C
    ws.Cells(lastRow + 1, 3).Value = "A"
    
    'Get the text from column B of the active sheet in the "Wall Enclosures" workbook
    sourceText = ThisWorkbook.Sheets("TYPE A").Cells(sourceRow, 2).Value
    
    'Add the source text to column D in the MPF IR worksheet
    ws.Cells(lastRow + 1, 4).Value = sourceText
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top