Populating Column "A" after copy & paste

Anthony86

Board Regular
Joined
Jan 31, 2018
Messages
62
Office Version
  1. 365
Platform
  1. Windows
Hi Again,

Can anyone help me with copying/pasting using a userform.

I have a master spreadsheet that I'm copying data from an pasting to a different workbook. I've got this bit working fine but once the pasting over has happened, I need to populate column "A" from a textbox value all the way down until the last used cell in column "B"

This is what I'm using to copy and paste data across.

Code:
With wsSource.Range("A2:D" & wsSource.Range("A" & Rows.Count).End(xlUp).Row)        .AutoFilter Field:=1, Criteria1:=txtFileNumber.Value
        
        .Offset(1).Columns(1).SpecialCells(xlVisible).Copy
        Range("A" & Rows.Count).End(xlUp).Offset(1, 1).PasteSpecial xlPasteValues
        .Offset(1).Columns(4).SpecialCells(xlVisible).Copy
        Range("A" & Rows.Count).End(xlUp).Offset(1, 2).PasteSpecial xlPasteValues
        .Offset(1).Columns(5).SpecialCells(xlVisible).Copy
        Range("A" & Rows.Count).End(xlUp).Offset(1, 3).PasteSpecial xlPasteValues
        Range("A" & Rows.Count).End(xlUp).Offset(1) = txtBoxNumber.Value
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Untested, but try
Code:
   Dim NxtRw As Long
   NxtRw = Range("A" & Rows.Count).End(xlUp).Offset(1).Row
   With wsSource.Range("A2:D" & wsSource.Range("A" & Rows.Count).End(xlUp).Row)
      .AutoFilter Field:=1, Criteria1:=txtFileNumber.Value
      .Offset(1).Columns(1).SpecialCells(xlVisible).Copy
      Range("B" & NxtRw).PasteSpecial xlPasteValues
      .Offset(1).Columns(4).SpecialCells(xlVisible).Copy
      Range("C" & NxtRw).PasteSpecial xlPasteValues
      .Offset(1).Columns(5).SpecialCells(xlVisible).Copy
      Range("D" & NxtRw).PasteSpecial xlPasteValues
      Range(Range("A" & NxtRw), Range("B" & Rows.Count).End(xlUp).Offset(, -1)) = txtBoxNumber.Value
   End With
 
Upvote 0
Untested, but try
Code:
   Dim NxtRw As Long
   NxtRw = Range("A" & Rows.Count).End(xlUp).Offset(1).Row
   With wsSource.Range("A2:D" & wsSource.Range("A" & Rows.Count).End(xlUp).Row)
      .AutoFilter Field:=1, Criteria1:=txtFileNumber.Value
      .Offset(1).Columns(1).SpecialCells(xlVisible).Copy
      Range("B" & NxtRw).PasteSpecial xlPasteValues
      .Offset(1).Columns(4).SpecialCells(xlVisible).Copy
      Range("C" & NxtRw).PasteSpecial xlPasteValues
      .Offset(1).Columns(5).SpecialCells(xlVisible).Copy
      Range("D" & NxtRw).PasteSpecial xlPasteValues
      Range(Range("A" & NxtRw), Range("B" & Rows.Count).End(xlUp).Offset(, -1)) = txtBoxNumber.Value
   End With

Wow! You sir are a genius!

This worked great, thank you.
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
Glad to help & thanks for the feedback

Another question. How can I edit the code so, if a file isn't found in the auto filter then add the textbox value to the new workbook that opened and then continue as normal.
 
Upvote 0
Try
Code:
   Dim NxtRw As Long
   Dim Cnt As Long
   
   NxtRw = Range("A" & Rows.Count).End(xlUp).Offset(1).Row
   Cnt = WorksheetFunction.CountIf(wsSource.Range("A:A"), txtFileNumber.Value)
   If Cnt = 0 Then
      Range("A" & NxtRw).Value = txtBoxNumber.Value
   Else
      With wsSource.Range("A2:D" & wsSource.Range("A" & Rows.Count).End(xlUp).Row)
         .AutoFilter Field:=1, Criteria1:=txtFileNumber.Value
         .Offset(1).Columns(1).SpecialCells(xlVisible).Copy
         Range("B" & NxtRw).PasteSpecial xlPasteValues
         .Offset(1).Columns(4).SpecialCells(xlVisible).Copy
         Range("C" & NxtRw).PasteSpecial xlPasteValues
         .Offset(1).Columns(5).SpecialCells(xlVisible).Copy
         Range("D" & NxtRw).PasteSpecial xlPasteValues
         Range(Range("A" & NxtRw), Range("B" & Rows.Count).End(xlUp).Offset(, -1)) = txtBoxNumber.Value
      End With
   End With
 
Upvote 0
Try
Code:
   Dim NxtRw As Long
   Dim Cnt As Long
   
   NxtRw = Range("A" & Rows.Count).End(xlUp).Offset(1).Row
   Cnt = WorksheetFunction.CountIf(wsSource.Range("A:A"), txtFileNumber.Value)
   If Cnt = 0 Then
      Range("A" & NxtRw).Value = txtBoxNumber.Value
   Else
      With wsSource.Range("A2:D" & wsSource.Range("A" & Rows.Count).End(xlUp).Row)
         .AutoFilter Field:=1, Criteria1:=txtFileNumber.Value
         .Offset(1).Columns(1).SpecialCells(xlVisible).Copy
         Range("B" & NxtRw).PasteSpecial xlPasteValues
         .Offset(1).Columns(4).SpecialCells(xlVisible).Copy
         Range("C" & NxtRw).PasteSpecial xlPasteValues
         .Offset(1).Columns(5).SpecialCells(xlVisible).Copy
         Range("D" & NxtRw).PasteSpecial xlPasteValues
         Range(Range("A" & NxtRw), Range("B" & Rows.Count).End(xlUp).Offset(, -1)) = txtBoxNumber.Value
      End With
   End With

Hmm I'm getting an error with this other line now.

'wsSource.ShowAllData' - I was using this to remove the filter each time.
 
Upvote 0
Very difficult to help when that line of code doesn't appear in any of the code on this thread.
 
Upvote 0
Very difficult to help when that line of code doesn't appear in any of the code on this thread.

Code:
Dim wsSource As Worksheet    Dim wsTarget As Worksheet
    Dim nextRow As Long
    Dim NxtRw As Long
    Dim Cnt As Long


  
    Workbooks.Open (ThisWorkbook.Path & "\*****.xls")


    
    'worksheets and workbooks
    Set wsSource = Workbooks("******.xlsm").Sheets("Control")
    Set wsTarget = Workbooks("***.xls").Sheets("Log")


    'filter and copy
    NxtRw = Range("A" & Rows.Count).End(xlUp).Offset(1).Row
   Cnt = WorksheetFunction.CountIf(wsSource.Range("A:A"), txtFileNumber.Value)
   If Cnt = 0 Then
      Range("A" & NxtRw).Value = txtBoxNumber.Value
      Range("B" & NxtRw).Value = txtFileNumber.Value
   Else
      With wsSource.Range("A2:D" & wsSource.Range("A" & Rows.Count).End(xlUp).Row)
         .AutoFilter Field:=1, Criteria1:=txtFileNumber.Value
         .Offset(1).Columns(1).SpecialCells(xlVisible).Copy
         Range("B" & NxtRw).PasteSpecial xlPasteValues
         .Offset(1).Columns(4).SpecialCells(xlVisible).Copy
         Range("C" & NxtRw).PasteSpecial xlPasteValues
         .Offset(1).Columns(5).SpecialCells(xlVisible).Copy
         Range("D" & NxtRw).PasteSpecial xlPasteValues
         Range(Range("A" & NxtRw), Range("B" & Rows.Count).End(xlUp).Offset(, -1)) = txtBoxNumber.Value
      End With
      End If


[COLOR=#ff0000]      wsSource.ShowAllData[/COLOR]
      txtFileNumber.Value = Null
      txtFileNumber.SetFocus
    
    End Sub

I use the line in red to turn the filter off in the main spreadsheet.
 
Last edited:
Upvote 0
Not sure why that would start failing, as the previous code shouldn't affect it.
What happens if you change it to
Code:
   wsSource.AutoFilterMode = False
 
Upvote 0

Forum statistics

Threads
1,215,335
Messages
6,124,327
Members
449,155
Latest member
ravioli44

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